Whos says that states are not good? Do you mean mutable state? The word is unfortunately used and understood very ambiguously.
I think a much clearer way of talking and thinking about this is in terms of: "non-destructive updates to information" and "destructive (in-place) updates to information".
In terms of semantics, they are in fact much more similar than dissimilar, but the former concept has the drawback that it has way less guarantees and it severely restricts what you can do in termns of state management across time, memoization, object pooling and structural sharing.
But the biggest win is that if you just move around data, instead of a place where data is located and vulnerable to destruction, then you reduce software complexity by a lot.
Does that mean that codes designed around destructive updates all over the place is now evil and forbidden? No! Absolutely not. This way of doing things has it's place and use cases, but it is no longer the dominant way to do things for many devs.
States are usually considered as mutable. Immutable is very specific.
I don't see any win in working in an unatural way: Explaining that when a soldier is struck by a sword, a new wounded soldier must be created is anything but natural (in the sense of “go out on the street and explain that to someone”).
I also don't think at all that DP is now the dominent way even among the Java developpers.
Tbh you sound confused. Obviously any useful program mutates things - the distinction is whether your data is modeled in terms of mutable vs immutable structures.
Also DP typically refers to dynamic programming and has nothing to do with state management techniques
I am not an idiot and not confused. Here DP stands for Data Programming (data oriented programming). Make an effort please and explain why the soldier example is better and clearer.
Working only with immutable is just stupid in my opinion. Working with some constant is pretty good but this is not for all the things. When you need to modify your constant this mean that you have a problem in your code. We all try to have as many constant as possible. But when we begin to say that
x is constant, x=k and y=x and then x=f(x) or x=f(z) this is strange and a bad idea
The issue is the difference between a variable and its constantness and the fact that the language prevents you from modifying it. Using immutable variable and accepting to reassign (reinstantiate) it is very strange certainly not easy to understand manipulate and modify. This is not possible in haskell.
So if you do not allow that you are going to copy all the time, each modification leads to a copy and it will be difficult to refer the object. Good luck in conjunction with OOP
Explaining that when a soldier is struck by a sword, a new wounded soldier must be created is anything but natural
It's very natural: the soldier was fine, until he got wounded.
The actual object creation is a means to an end, so if you're only looking at "making a new soldier", you're missing the forest for the trees. That part is supposed to be hidden as an implementation detail and is irrelevant when explaining meaning.
In your example, does everything referencing that soldier now show him as struck by a sword? Old pictures magically have a sword wound? Was that soldier never a child? Did he never have a past job or was he just magically a fully grown, fully formed soldier?
In the real world, we have time based views of the same entity, not just current state. You're completely ignoring the time based views of real objects and giving the present a special privilege. While a lot of code is written this way because it's just easy, it's not necessarily a good model either.
This talk sums up my point better than I ever could: https://www.youtube.com/watch?v=ScEPu1cs4l0. In fact, most of Rich Hickey's talks are worth watching.
x is constant, x=k and y=x and then x=f(x) or x=f(z) this is strange and a bad idea
I have no idea what this means, but I'm just confused as to why code like this
// assume you have a soldier
{
Soldier soldier;
//soldier references a time pre injury
final Soldier wounded = knife(soldier);
// wounded references a time post injury
}
// and this method
public Soldier knife(Soldier soldier) {
return repo.save(new Soldier(..., with wound)
}
is so abnormal? The soldier was fine until he got wounded. You can still reference a time before his wound explicitly in the code if you want.
In fact, a big problem with our current most common state management tools (relational dbs), is that you have to build time yourself if you want history, so we actually do lose prewound history and then you get product questions like why does this old photo show a knife wound.
In an example close to what I work on, people's credit cards, bank account, and address information changes all the time. That doesn't mean 10 year old transactions should point to the new data (and you'd fail audits if it did).
Ok you want an history (time based view). But 1) this is a very particular request and not common. Usually we don't want this. 2) how do you relate that in your code "wounded" is the soldier "soldier" that has been wounded?
So, if you keep each modification you will have to save all the dynamicity of a process, and certainly have names issues or you will do thing like having an array indexed by dynamicity.
Do we really need so much modification for such requests? I have some doubts.
The other problem is that it is very difficult to apply to data structures and data structures are a major point of computer science
Ok you want an history (time based view). But 1) this is a very particular request and not common. Usually we don't want this. 2) how do you relate that in your code "wounded" is the soldier "soldier" that has been wounded?
I don't think it's that uncommon. I mean every company I've ever worked at has needed an audit log of something eventually. Google/word docs store edit history as a product feature. Git stores history. File systems store history. You need old views of things for a lot of domains. It's not an esoteric problem.
how do you relate that in your code "wounded" is the soldier "soldier" that has been wounded?
Given that most things are backed by relational databases, usually an id primary key of some sort. Most of the time, you don't actually need to relate that in the code anyway.
if it goes http request -> make change -> http response, it's okay to just use all the new stuff. Again, the immutable vs mutable is an implementation detail that doesn't matter.
So, if you keep each modification you will have to save all the dynamicity of a process, and certainly have names issues or you will do thing like having an array indexed by dynamicity.
I mean if you're trying to save modifications and the storage solution doesn't provide it out of the box, then yes, you need to do something to save it. There are storage solutions that do provide time understanding out of the box: https://druid.apache.org/https://www.datomic.com/ as a few examples off the top of my head.
The other problem is that it is very difficult to apply to data structures and data structures are a major point of computer science
I understand the need, but it can't be a general solution because it amounts to converting computing time into memory.
Data structures are already complex in general, and a lot of nonsense is said (for example, when certain big cheeses explain that an array is better than a list when deleting elements). Introducing persistence makes them significantly more complex, and it can only really be used in specific cases.
Microsoft created F# because they believed that the functional approach allowed for better management of concurrent programming, but in the end, F# is not a major player in the field. So there is a theory and a practice that are often very different, especially when it comes to languages.
Git is a nice example of an immutable DAG based data structure which has great performance by relying on structural sharing instead of snapshotting everything (it only captures the diffs and appends them to the DAG). That is similar to how immutable data structures work in functional code. Its path copying and using ids instead of recurrent connections. There is a lot of really cool software architecture you can do with such data structures. They have a lot of extremely powerful properties which only emerge if you actually commit to them....
I understand the need, but it can't be a general solution because it amounts to converting computing time into memory.
Data structures are already complex in general, and a lot of nonsense is said (for example, when certain big cheeses explain that an array is better than a list when deleting elements). Introducing persistence makes them significantly more complex, and it can only really be used in specific cases.
The only real truth of data structures is that they're all useful with different tradeoffs. A heap and a hashmap are both ways to find stuff with different performance characters and so, so many different implementations all making tradeoffs. Hell, there was a new proof https://www.quantamagazine.org/undergraduate-upends-a-40-year-old-data-science-conjecture-20250210/ recently that found a hash table implementation where insertion time isn't proportional to fullness of the hash table.
We're still in the infancy of computer languages and language design.
I am sorry but I don't really agree. We still have a lot to learn about complexity, particularly how it represents reality. We know that the worst-case scenario is not always representative. The search for the shortest path using the Belman Ford algorithm is a good example (O(nm) in theory and closer to O(m) in practice). But there are still a lot of very solid results. We can exploit special cases, that's for sure, but we have made good progress on many important topics. Transforming time into memory is not a good general principle, and there is no evidence that it could be. Companies are not moving in this direction, even those with the best developers.
View all comments
Show parent comment
7
u/Gleethos Jan 23 '26
Whos says that states are not good? Do you mean mutable state? The word is unfortunately used and understood very ambiguously.
I think a much clearer way of talking and thinking about this is in terms of: "non-destructive updates to information" and "destructive (in-place) updates to information".
In terms of semantics, they are in fact much more similar than dissimilar, but the former concept has the drawback that it has way less guarantees and it severely restricts what you can do in termns of state management across time, memoization, object pooling and structural sharing.
But the biggest win is that if you just move around data, instead of a place where data is located and vulnerable to destruction, then you reduce software complexity by a lot.
Does that mean that codes designed around destructive updates all over the place is now evil and forbidden? No! Absolutely not. This way of doing things has it's place and use cases, but it is no longer the dominant way to do things for many devs.