Hacker News new | ask | show | jobs
by calebmpeterson 5069 days ago
> However, few practical applications benefit from persistence in practice so this is often not advantageous.

This statement is questionable.

What is true is this: many professional software developers are comfortable with imperative/OOP/mutability. This is easier (not simpler) because it's familiar. The benefits of immutability are not obvious/familiar.

Persistent data structures enable immutable data with cheap(ish) operations that can produce updated versions of that data. This means I can expose data to multiple threads, calling functions, etc... without worrying about that data being placed into an inconsistent state or viewed in an inconsistent state by callers.

Back when much of our profession malloc'ed their memory, we always had to answer the question "who is going to free() this chunk of the heap?" And when we answered wrong, or forgot to answer, we leaked memory. Garbage collectors, while imperfect from a controlability and performance standpoint, are much better at this than humans.

An excellent analogy exists with mutable data. When I expose a piece of mutable data via any public interface, I have to ask the question "who can change this data and how will consistency be enforced?" I'm left with the answer "I don't know, I hope no one gets it wrong" or even worse: "they better lock the right semaphore(s) in the correct order". Immutable data (implemented via persistent data structures) enables me to instead say "here's the data, if any caller would like to update, they can a) pass me an updated version which I can validate, or b) call into some part of my public interface and instruct me what update to make"

Or, in the case of Clojure (my blub at the moment), put the runtime in charge of maintaining consistency during mutation using refs and the STM.

3 comments

When you expose a piece of mutable data, usually it's wrapped up in an interface that enforces the access and consistence of that piece of data so that you have control rather than let it up to the callers. If you don't want to have the synchronization wrapper, it's ok. Just document it clearly and let the callers deal with it.
Yes!!! I've made similar points at http://common-lisp.net/project/fset/Site/index.html
There are many benefits to immutability. However, there are only a few smaller additional benefits of functional purity after we're already able to enforce mutability and immutability. Sometimes I really do want to modify some data internally before returning it, and baked-in purity keeps me from "cooking" my data properly.