

Ask HN: Advantages of immutable variables besides concurrency - boundlessdreamz

Some languages, notably erlang has immutable variables. Are there any advantages to this besides
making concurrent programming easier ?
======
eru
Yes. It makes composing larger programs out of smaller programs easier.
Especially if functions have to be real mathematical functions, and can't have
any side-effects.

"Composability means that you can put two bits of code together and important
correctness properties will be preserved automatically. This does not mean, of
course, that the composition is automatically correct in a wider sense, but it
does mean that you don’t introduce new bugs merely by sticking two things
together."
([http://paulspontifications.blogspot.com/2007/09/composabilit...](http://paulspontifications.blogspot.com/2007/09/composability-
and-productivity.html))

------
jganetsk
Allows for laziness, in languages like Haskell.

Also, some data structures are simply _incorrect_ when the data is mutable. A
hash table with mutable keys comes to mind. Why? Once you insert the key-value
pair, and mutate the key, you may not be able to find that key anymore.

------
mooism2
It means you know where the variable's value came from.

It also makes sharing common parts between different data structures easier.

------
catch23
depending on your language, modifying an immutable variable may throw a
runtime/compiletime exception, giving you insight into your bug (potentially)

