←back to thread

498 points azhenley | 3 comments | | HN request time: 0.671s | 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 #
jimbokun ◴[] No.45773343[source]
The concept is actually pretty simple: instead of changing existing values, you create new values.

The classic example is a list or array. You don't add a value to an existing list. You create a new list which consists of the old list plus the new value. [1]

This is a subtle but important difference. It means any part of your program with a reference to the original list will not have it change unexpectedly. This eliminates a large class of subtle bugs you no longer have to worry about.

[1] Whether the new list has completely new copy of the existing data, or references it from the old list, is an important optimization detail, but either way the guarantee is the same. It's important to get these optimizations right to make the efficiency of the language practical, but while using the data structure you don't have to worry about those details.

replies(2): >>45775639 #>>45788870 #
ErroneousBosh ◴[] No.45775639[source]
> It means any part of your program with a reference to the original list will not have it change unexpectedly.

I don't get why that would be useful. The old array of floats is incorrect. Nothing should be using it.

That's the bit I don't really understand. If I have a list and I do something to it that gives me another updated list, why would I ever want anything to have the old incorrect list?

replies(2): >>45775956 #>>45776082 #
jimbokun ◴[] No.45776082[source]
You pass in an array to a function meant to perform a transformation on each item of the array and return the result.

You pass in an array of 10 values.

While the function is executing, some other thread adds two more values to the array.

How many values should the result of the function call have? 10 or 12? How do you guarantee that is the case?

replies(1): >>45777557 #
ErroneousBosh ◴[] No.45777557[source]
> While the function is executing, some other thread adds two more values to the array.

This is not something that can happen.

replies(1): >>45779279 #
jimbokun ◴[] No.45779279[source]
Why not? Which language?
replies(1): >>45780221 #
1. ErroneousBosh ◴[] No.45780221[source]
Well, the stuff I'm writing is in C, but in general it would make no sense for anything to attempt to add items to a fixed-sized buffer.

If you have something so fundamentally broken as to attempt that, you'd probably want to look at mutexes.

Why one earth would you have something attempt to expand a fixed-sized buffer while something else is working on it?

replies(1): >>45781905 #
2. filoeleven ◴[] No.45781905[source]
There’s a mismatch between your assumptions coming from C and GP’s assumptions coming from a language where arrays are not fixed-length. Having a garbage collector manage memory for you is pretty fundamental to immutable-first languages.

Rich Hickey asked once in a talk, “who here misses working with mutable strings?” If you would answer “I do,” or if you haven’t worked much in languages where strings are always immutable and treated as values, it makes describing the benefits of immutability more challenging.

Von Neumann famously thought Assembly and higher-level language compilers were a waste of time. How much that opinion was based on his facility with machine code I don’t know, but compilers certainly helped other programmers to write more closely to the problem they want to solve instead of tracking registers in their heads. Immutable state is a similar offloading-of-incidental-complexity to the machine.

replies(1): >>45784734 #
3. ErroneousBosh ◴[] No.45784734[source]
I must admit I do regard assembly language with some suspicion, because the assembler can make some quite surprising choices. Ultra-high-level languages like C are worse, though, because they can often end up doing things like allocating really wacky bits of memory for variables and then having to get up to all sorts of stunts to index into your array.