getify / Functional-Light-JS

Pragmatic, balanced FP in JavaScript. @FLJSBook on twitter.
http://FLJSBook.com
Other
16.6k stars 1.96k forks source link

Chapter 4: Abstraction Example wrong logic #190

Open vesheff opened 4 years ago

vesheff commented 4 years ago

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] );
    }

Proposal:

getify commented 4 years ago

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.

"property of an object" vs "property of the store"... these are the same thing, I'm not sure what distinction you're trying to make?

You return isUndefined

You're correct, I accidentally reversed the logic here. Not that it really matters all that much, since this whole section of code is what not to do. But it's something that could be cleaner. The simple fix would just be:

function isUndefined(val) { return val === undefined; }

function isPropDefined(val,obj,prop) {
   return !isUndefined( obj[prop] );
}

It's something I'll take a look at for next revisions/editions of the book.

vesheff commented 4 years ago

"property of an object" vs "property of the store"... these are the same thing, I'm not sure what distinction you're trying to make?

You are right. The thing is that in the original example you check if the name of the event is undefined, but in the last example you check if the key in the store is undefined, so this is the wrong logic I'm trying to fix.

const events = {
    'js-conf': {
        name: 'js-conf'
    }
};

trackEvent({});

console.log(events); 
// Example 1: { 'js-conf': { name: 'js-conf' } }
// Example 2: { 'js-conf': { name: 'js-conf' }, undefined: {} }
getify commented 4 years ago

Oh, I see. OK, yes, I'll fix that too.

vesheff commented 4 years ago

If you want I could make a pull request on that.

getify commented 4 years ago

No thanks. I won't be making changes to the book since it's been in print for almost 2 years now. These changes will be addressed when/if I release a second edition.