ImmutabilityInProgrammingLanguages

ThoughtStorms Wiki

Conext : FunctionalProgramming, ProgrammingStuff

See also : ImmutabilityBeatsDataHiding

Quora Answer : In your experience, are immutable data structures and paradigms easier or harder to understand in general (and especially for newcomers) than regular imperative mutable programming paradigms? Why?

Sep 15

In my experience, immutable data structures and immutability in languages in general are an absolutely solid win. They are f-ing brilliant.

I didn't expect this. I'm generally against my programming languages trying to force me to "do the right thing" through restrictions. I've never been that keen on languages enforcing strict type checking. Or data-hiding with de-facto privacy.

I've used languages that have them. And languages that don't. And I'm ambivalent about them. Yeah, there are benefits. And costs. And I don't tend to miss these constraints when I don't have them.

But immutability.

That is wonderful.

Yes. It's more work to figure out how to write your algorithm when you first write your function. It can take 15 minutes instead of 5. You have to think ... how the hell am I going to do this when I can't just store x in a temporary variable as I'm going along?

And yes, you need a powerful language with a bunch of the usual FP style map / filter / take-while / drop-while etc. type functions in the standard library, so you can build your algorithms out of these existing components. Trying to implement them without mutability is probably hard.

BUT ...

once you've figured it out, and implemented the algorithm like this. The algorithm is incredibly solid. You almost never have to revisit this code again!

There are no sneaky bugs hiding there. No off-by-one errors that get thrown up in obscure cases. The code just sits there working. Of course you might have to revisit when you want to extend the functionality. But when you do, the code is incredibly easy to understand. Because you don't have to read the code and try to run it in your head to grok what it does. It does exactly what it says it does. If there's a line that says "map this function to the collection" you know that that happens.

You never have to worry what else has been done to an object or collection after it was created. If you can see the line that created it, you know exactly what is in it.

Coming back and re-reading, and working on, immutable code is a joy compared to revisiting my code in mutable languages.

Seriously. Immutability is one of the best things I've found in programming languages. Certainly the best "weird restriction". If I have to choose between a language with immutability and a language with mutability, I will pick immutability every time.

And like I say, I am not usually a fan of "restrictions that help you program better". I don't like static typing. I never bother to make fields private. I've always been happy with Python where that wasn't even a thing.

But I am totally sold on immutability.

Quora Answer : Why are some objects in some programming languages immutable?

Feb 17, 2014

If you have immutability it means that the only values in a function are constants and those passed in as parameters.

That, in turn, means that every time you call the function with the same parameters it will produce that same output.

Which in turn means that :

a) it's easier to unit test the functions. (Apart from the arguments, there's no extra state you have to worry about setting-up to have the right values)

b) it's easier to prove that the function does what it's meant to.

c) you can cache the results of calling functions on particular argument sets for speed. (Memoization)

d) if there's no "state" ie. extra information in your functions beyond your parameters, then there's certainly no "shared state" which means it's much easier for a compiler or virtual machine to distribute your program across multiple processors or machines.

The more your program is organized around functions and immutable data-structures like this, the more amenable it is to these benefits.

Obviously, some FP languages (Haskell) pretty much force this on you. Some like Clojure strongly encourage it. And many support it.