3 ms·
Ask HN: Advantages of immutable variables besides concurrency
Some languages, notably erlang has immutable variables. Are there any advantages to this besides
making concurrent programming easier ?
- mooism2 18y agoIt means you know where the variable's value came from. It also makes sharing common parts between different data structures easier.
- catch23 18y agodepending on your language, modifying an immutable variable may throw a runtime/compiletime exception, giving you insight into your bug (potentially)
- eru 18y agoYes. 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/composability-and-productivity.html http://paulspontifications.blogspot.com/2007/09/composabilit...)
- jganetsk 18y agoAllows 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.