←back to thread

498 points azhenley | 2 comments | | HN request time: 0s | source
Show context
EastLondonCoder ◴[] No.45770007[source]
After a 2 year Clojure stint I find it very hard to explain the clarity that comes with immutability for programmers used to trigger effects with a mutation.

I think it may be one of those things you have to see in order to understand.

replies(17): >>45770035 #>>45770426 #>>45770485 #>>45770884 #>>45770924 #>>45771438 #>>45771558 #>>45771722 #>>45772048 #>>45772446 #>>45773479 #>>45775905 #>>45777189 #>>45779458 #>>45780612 #>>45780778 #>>45781186 #
rendaw ◴[] No.45770924[source]
I think the explanation is: When you mutate variables it implicitly creates an ordering dependency - later uses of the variable rely on previous mutations. However, this is an implicit dependency that isn't modeled by the language so reordering won't cause any errors.

With a very basic concrete example:

x = 7

x = x + 3

x = x / 2

Vs

x = 7

x1 = x + 3

x2 = x1 / 2

Reordering the first will have no error, but you'll get the wrong result. The second will produce an error if you try to reorder the statements.

Another way to look at it is that in the first example, the 3rd calculation doesn't have "x" as a dependency but rather "x in the state where addition has already been completed" (i.e. it's 3 different x's that all share the same name). Doing single assignment is just making this explicit.

replies(10): >>45770972 #>>45771110 #>>45771163 #>>45771234 #>>45771937 #>>45772126 #>>45773250 #>>45776504 #>>45777296 #>>45778328 #
ape4 ◴[] No.45771937[source]
I would be nicer if you gave x1 and x2 meaningful names
replies(1): >>45772547 #
catlifeonmars ◴[] No.45772547{3}[source]
What would those names be in this example?
replies(1): >>45775677 #
ape4 ◴[] No.45775677{4}[source]
In a real application meaningful names are nearly always possible, eg:

    const pi = 3.1415926
    const 2pi = 2 * pi
    const circumference = 2pi * radius
replies(2): >>45776064 #>>45777010 #
tmtvl ◴[] No.45776064{5}[source]
Calling tau 2pi is the most cursed thing I've seen all day. Appropriate for Halloween.
replies(1): >>45777049 #
1. smrq ◴[] No.45777049{6}[source]
If you call a variable tau in production code then you're being overly cute. I know what it means, because I watch math YouTube for fun, but $future_maintainer in all likelihood won't.
replies(1): >>45780431 #
2. lock1 ◴[] No.45780431[source]
Where do you draw the line then? Stopping at `tau` just because `$future_maintainer` might get confused feels like an arbitrary limit to me.

What about something like `gamma`? Lorentz factor? Luminance multiplier? Factorial generalization?

Why not just use the full sentence rather than assign it to an arbitrary name/symbol `gamma` and leave it dependent on the context?

And it's not that hard to add an inline comment to dispel the confusion

  const tau = 2*pi; // Alternate name for 2pi is "tau"