←back to thread

498 points azhenley | 2 comments | | HN request time: 0.415s | 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 #
ErroneousBosh ◴[] No.45771722[source]
I guess I'm not that good a programmer, because I don't really understand why variables that can't be varied are useful, or why you'd use that.

How do you write code that actually works?

replies(4): >>45771824 #>>45772583 #>>45773343 #>>45787597 #
jayd16 ◴[] No.45772583[source]
If you need new values you just make new things.

If you want to do an operation on fooA, you don't mutate fooA. You call fooB = MyFunc(fooA) and use fooB.

The nice thing here is you can pass around pointers to fooA and never worry that anything is going to change it underneath you.

You don't need to protect private variables because your internal workings cannot be mutated. Other code can copy it but not disrupt it.

replies(2): >>45774996 #>>45775589 #
1. MetaWhirledPeas ◴[] No.45774996[source]
> If you need new values you just make new things. > If you want to do an operation on fooA, you don't mutate fooA. You call fooB = MyFunc(fooA) and use fooB.

The beautiful thing about this is you can stop naming things generically, and can start naming them specifically what they are. Comprehension goes through the roof.

replies(1): >>45777602 #
2. ErroneousBosh ◴[] No.45777602[source]
Yes, but you can do that without having loads of stale copies of incorrect data lying around, presumably.