getify / Functional-Light-JS

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

Mutation explanation - Negation needed #145

Closed nitinja closed 6 years ago

nitinja commented 6 years ago

Here is a paragraph from chapter 6 (Value Immutability)

Imagine the [1,2,3] array represents a sequence of data from some previous operations and we stored in some variable. It is our current state. If we want to compute what the next state of our application is, we call addValue(..). But we want that act of next-state computation to be direct and explicit. So the addValue(..) operation takes a direct input, returns a direct output, and avoids creating a side effect by mutating the original array that arr references.

I think this should be negated (by NOT mutating the original array).

getify commented 6 years ago

Heh. This issue is the grammatical equivalent of De Morgan's Law about boolean complements, and comes down to where you implicitly draw the grouping boundary in the sentence structure.

The sentence could be read in two different ways... the currently intended-reading is:

X = "creating a side effect by mutating the original array that arr references." "...avoids (X)" OR: "...avoids (creating a side effect by mutating the original array that arr references)."

Clearly this is correct. The negation of "avoids" also implicitly inserts the "NOT" in front of the "mutating" term.

However, the other way, the way you apparently read it, is:

X = "creating a side effect" "...avoids (X) by mutating the original array that arr references." OR: "...avoids (creating a side effect) by mutating the original array that arr references."

Clearly this is incorrect because the "avoid" doesn't apply to "mutating", doesn't negate it, and thus it needs a "NOT" inserted.


Making this observation, I'm not sure which way would be correct or more likely for the reader. So my instinct is it needs to be rewritten so no such ambiguity is present.

beardedtim commented 6 years ago

Would the phrasing 'avoids creating the side effect of mutating the original' make it easier to understand that 'addValue' is immutable?

nitinja commented 6 years ago

Nice explanation, @getify ! It now makes sense to me. Also @beardedtim your sentence is much clearer - @getify may want to use it.