How would you transfer data between your data structures and databases?

What is the benefit of object-oriented programming with immutable data structures and referentially transparent functions?

  • In some languages like Scala, you can set up an Algebraic Data Type such as a binary tree, which consists of a base case of an empty Nil object and an immutable list pointing to other binary trees. When passing around these objects, you always construct new instances of data types. This maintenance of lots of "copies" of data structures efficiently is a feature built into Scala. How does this solve the problems of modeling the world on the message-passing between objects and change of object state, where time is not explicit or first-class? Is it that we may enumerate copies of data structures via infinite streams, then explicitly change shared data with something like transactions in relational databases? A reference is fine.

  • Answer:

    This is essentially the idea behind the Event Sourcing pattern: http://martinfowler.com/eaaDev/EventSourcing.html By reifying changes to the model as data, you can get auditing for free. It's also very handy for designing synchronization protocols (like DeltaSync), especially if you have domain-dependent rules for conflict resolution. This is also a downside - now you're essentially exposing what your database does internally (transaction log) as part of your own code.

Vladimir Sedach at Quora Visit the source

Was this solution helpful to you?

Other answers

Referential transparency and immutability make programs much easier to reason about. Programs that are easier to reason about can be parallelized more easily, checked more completely at compile time, and are easier to debug, one you are used to it. This is true whether or not the programs are also object oriented - the two things are really orthogonal to one another. The question details raise some interesting additional questions. Firstly, to clear up one confusion, if data is immutable it doesn't matter whether or not it is copied - the difference between pass-by-value and pass-by-reference is an implementation detail if you cannot modify the data anyway. Because it is implemented on the JVM, Scala actually uses pass by reference, so its not true that a lot of data is copied. Message passing is an idea that is associated with object orientation, but not required by it. In message passing, the entities passing messages back and forth must change state - its fundamental to the model. If you are going to do message passing, you must model that state change somehow. You cannot do this if you want to pass messages between plain old Scala objects and you want them to be implemented in a referentially transparent way. Scala does have Actor libraries that use message passing, but obviously they rely on mutable state. You could use a State monad (not common in Scala) to model message passing between immutable objects, but this not look much like the normal object oriented style.

Simon Kinahan

The most important benefit of immutable data structures is that working with them is thread safe; if you want to modify them you will need to create a copy, and work on the copy - a copy that is, in itself, 'thread safe'. Thread safety gives you the advantage of being able to scale fast on multiple threads. Therefore, if you develop with immutability of data structures in mind you will be able to build solutions that scale easy and correctly on multiple processors.

Dorin Lazăr

This is something of my interest. 1. Data Encapsulation and Abstraction 2. Dynamic Binding 3. Classes and Objects 4. Inheritance (probably the most important in long run) 5. Message Communication You can Google these above topics in detail for more information. Please append my answer in comments if you think there are more. (definitely there are more, I could just think of these)

Prateek Shankar

Not exactly an answer to your question, but could be useful: http://www.yegor256.com/2014/06/09/objects-should-be-immutable.html Immutable objects are much more effective in many situations, esp in object oriented programming

Yegor Bugayenko

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.