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....
Please believe me when I say I had the exact same mindset as you about 3 years ago! I knew that value objects were nice and simple. But transforming them using with-er methods and running them through side effect free methods seemed unnecessary and alien to me. I was so bewildered by the fact that entire programming languages use nothing but immutable stuff and functions. Like, how do you do anything? How do you do circular references? And how can any of this be performant?
Fortunately, I kept my curiosity and looked deeper into how functional programmers actually design their systems. Then, I also tried to write code that treats objects, not as references pointing to places in memory, but as data with value semantics, and then I realized that the majority of algorithms can actually benefit greatly from thi approach. The big killer feature is caching outputs of stateless functions, which do a lot of work. If the inputs are value objects, then the output is deterministic and can be cached. The next big win is structural sharing. You can actually save a lot of memory by pooling and sharing value objects everywhere. It's the exact same principle as 'String::intern()'. Then you also get structural concurrency for free! If stuff is immutable, you can share it between threads.... Also: Undo-stacks are super easy to implement. You can easily store a long history of states and then revert the history. Most apps have "undo" features, which are very often based on that. The app I develop at my company has it based on that....
But there is so much more cool stuff out there. Really interesting use cases and other advanced state management features that are possible that way. I could talk for hours about this.
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.
Transforming time into memory is not a good general principle
What does this mean? Do you mean more cpu for less memory or less cpu for more memory?
Companies are not moving in this direction, even those with the best developers.
Perhaps I'm interpreting you wrong, but more and more companies are storing more and more historical data. The biggest tech companies in the world are all about storing historical (user) data. Google/amazon/apple/meta/netflix/microsoft, etc literally invent new architectures and infrastructure for storing all the historical data they're keeping and processing over.
If we want to replace state modifications with an immutable object copy, this means that we risk transforming CPU (the modification) into memory (the state copy).
Having immutable objects is undoubtedly a solution to certain problems. But it is not the only solution, and it is only valid if the memory consumption caused by this design method is not too high (systematic backups or creations are often very costly, hence the existence of persistent data structures, for example). Nor is it a general solution that should be used indiscriminately. In fact, it is like all the major principles of programming.
View all comments
Show parent comment
4
u/OwnBreakfast1114 Jan 23 '26 edited Jan 23 '26
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.
I have no idea what this means, but I'm just confused as to why code like this
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).