But I think that OO code localizes changes to instance variables. Variables can be written as "final" if they are only set to once. Sometimes stuff can be "frozen" to make them immutable. A lot of data is constant by default, therefore it's as though they were immutable to some extent.
The problem with that is in added complexity as you have to actually plan for what should be mutable or final. You also still have the problem of having to deep copy things declared as final if you do need to make changes. With immutable data you simply don't worry about this. That's more time you can spend thinking about the actual problem you're solving.
Another problem with OO is that it ties the logic to a specific domain via classes. This makes it more difficult to reuse code. If you solve one problem and then have another similar problem you often have to write things like wrappers and adapters.
With a functional language you have a small number of data types that all functions operate on. Problems are typically solved by simply composing those in a specific order. This means that when you write a particular data transformation you can now reuse it any time without any additional effort.
Part of the experience users notice when using applications is how fast they can interact with them. Does the application launch fast? Does it keep up with the using? Sometimes when we add a lot of flexibility to the language it can cost in those regards and somewhat doom languages to the server-side.
The overhead of immutable data structures doesn't affect performance in most cases. Note that Haskell is one of the fastest languages out there and it has pervasive immutability. It comes down to paying the price of O(log32n) vs O(1), unless your data sizes get huge it's not a problem.
On top of that Clojure provides support for localized mutation with transients. For example, we could write:
here we use a mutable data structure within the scope of let, and we persist it when we return. The compiler will give an error if we try to return the mutable result back. So, we can have localized mutation with the function and be guaranteed that we don't leak mutable state globally.
1
u/yogthos Jul 20 '13
The problem with that is in added complexity as you have to actually plan for what should be mutable or final. You also still have the problem of having to deep copy things declared as final if you do need to make changes. With immutable data you simply don't worry about this. That's more time you can spend thinking about the actual problem you're solving.
Another problem with OO is that it ties the logic to a specific domain via classes. This makes it more difficult to reuse code. If you solve one problem and then have another similar problem you often have to write things like wrappers and adapters.
With a functional language you have a small number of data types that all functions operate on. Problems are typically solved by simply composing those in a specific order. This means that when you write a particular data transformation you can now reuse it any time without any additional effort.
The overhead of immutable data structures doesn't affect performance in most cases. Note that Haskell is one of the fastest languages out there and it has pervasive immutability. It comes down to paying the price of O(log32n) vs O(1), unless your data sizes get huge it's not a problem.
On top of that Clojure provides support for localized mutation with transients. For example, we could write:
here we use a mutable data structure within the scope of
let, and we persist it when we return. The compiler will give an error if we try to return the mutable result back. So, we can have localized mutation with the function and be guaranteed that we don't leak mutable state globally.What's more, other language features can be a lot more important for performance. For example, take a look at ClojureScript templating performance compared to jQuery from Prismatic.