getify / Functional-Light-JS

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

Tech Edit: Chapter 6 #59

Closed DrBoolean closed 7 years ago

DrBoolean commented 7 years ago

Another spot-free chapter tech-wise. Just a few possible suggestions:

In any case, I feel it's worth a mention (somewhere in the book) that reassignment breaks ref transparency (and, therefore, equational reasoning), and adds a substantial obstacle to parallelization, which is why it is not allowed in just about every functional language. I guess I was thinking a note that in FP languages = means equals, not assignment, which is not the case in JS.

I was taken aback by this discussion, though, as it is often cited as improving readability since you don't have to track variable changes as well. As I saw it improved readability was the main aim of this book. I did appreciate and acknowledge, however, that mutation is much less conspicuous than reassignment and priority-wise, should be the focus.

getify commented 7 years ago

I agree re-assignment should be minimized where possible. If I'm using it it's usually because a value is type-changing during the course of a function, and I find it more readable to keep the variable name the same rather than needing some naming convention to keep the "relationship" between two or more variables clear.

I just don't think const adds very much at all to that effort, not enough to outweigh the potential confusion cost.

getify commented 7 years ago

Another quick example of the utility of re-assignment when used sparingly: swapping two variable values.

DrBoolean commented 7 years ago

Gotcha and without variables like f', it does get annoying to rename things. I think that's great to add that point to the chapter somewhere (maybe i missed it?).

I still think it's good to add that quick clear up of why so many FP languages remove reassignment all together. The whole "substitute equals for equals" thing falls apart when variables change, but then again "equals" does not mean the same thing in JS.