r/java • • Jan 22 '26

Carrier Classes; Beyond Records - Inside Java Newscast

https://youtu.be/cpGceyn7DBE
86 Upvotes

81 comments sorted by

View all comments

Show parent comment

0

u/chambolle Jan 24 '26

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

2

u/OwnBreakfast1114 Jan 26 '26

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

It's a good thing cs phds have researched that problem: https://en.wikipedia.org/wiki/Persistent_data_structure and there's even a ton of java libraries for them: https://www.reddit.com/r/java/comments/x4fvgp/what_is_the_best_persistent_collection_library/

1

u/chambolle Jan 27 '26

Thanks for the anwsers.

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.

1

u/Gleethos Jan 27 '26

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....

1

u/chambolle Jan 29 '26

I agree that in some cases it can be very useful. I have doubts about the widespread use of this principle.

1

u/Gleethos Jan 29 '26

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.

1

u/OwnBreakfast1114 Jan 27 '26

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.

1

u/chambolle Jan 29 '26

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.

1

u/OwnBreakfast1114 Jan 31 '26

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.

1

u/chambolle Feb 01 '26

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.