Skip to content

Chapter 4: Abstraction Example wrong logic #190

Open
@vesheff

Description

@vesheff

In the last abstraction example (when you took the abstraction too far) the logic in the code is wrong compared with the original example shown earlier. So if this is intentionally, please disregard the comment below and just add clarification in the description of the example.

  1. First mistake

    function isPropUndefined(val,obj,prop) {
        return isUndefined( obj[prop] );
    }
  • The code above is ok to check if a property of an object is undefined, but here you change the logic of the previous example and check if a property of the store is undefined.

    • Original logic:

      if (evt.name !== undefined) {
            storeData( events, evt.name, evt );
      }
  1. Second mistake
  • You return isUndefined result which is true if the property is undefined, so the logic is wrong once again.

Proposal:

  • Just change the name of the function and wrap the function in another one which accepts the property to be checked against undefined. This will lead to the need of invoking the function with an argument name in this case.

    function isPropDefined(prop) {
        return function (obj) {
            return !isUndefined(obj[prop]);
        }
    }
    
    function trackEvent(evt) {
        conditionallyStoreData(events, evt.name, evt, isPropDefined('name'));
    }
  • Now we could change the invokation of checkFn to be with only one argument.

    if (checkFn(value)) {
        store[location] = value;
    }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions