[LRUG] The advantage of immutability?

Tom Armitage tom at infovore.org
Thu Sep 10 02:13:33 PDT 2015


On Thu, Sep 10, 2015 at 8:49 AM, Jonathon Horsman <jonathon at arctickiwi.com>
wrote:

>
> given that having a data store as a shared global state the situation in
> the vast majority of apps? Surely shoe-horning immutable objects onto a
> mutable data store would always be the wrong solution?
>
>
I'm sure someone will correct me if I'm wrong but - that's really a fallacy
of the ActiveRecord pattern, right, where one domain object corresponds
directly to its representation data store?

This is the sort of thing something like a Repository pattern solves, ie:
you have a mutable data store, and a way of communicating to/from it (to
save/update/delete records) - but your domain objects are actually
separate. You retrieve data from the database, and use it to build
immutable objects to pass around the application. Instead of mutating them,
you replace them with new objects as/when - and then, when you come to
saving it, your repository interface goes "oh-ho, this is really an edit to
some rows", and it makes an UPDATE rather than just new rows everywhere -
even though you've been making new objects in the application.

What that also means is you can represent something that might be stored in
a single table as a collection of objects, or vice versa, something that's
correctly distributed across tables with relationships as *fewer* objects.
It breaks the direct link between A Thing and A Table. This is entirely
counter to ActiveRecord, but AR is not the only way.

Disclaimer: I've never used this approach personally.

But: I can see the appeal (especially now I've explained it to myself).

As for "doesn't it make it more complex"; as usual, the answer's often *yes*;
except the number of times I've continued to build on that foundation to a
point where I either need to a) do a big refactoring or b) carry on in a
string-and-glue manner means that, as usual, choosing to make something
more complex is not always the worst idea if it more accurately represents
the shape of the world. As ever, that's the hard part of making software:
choosing when to make the decision to change direction.

I hope I've not totally misunderstood Repositories.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20150910/e58d6ca9/attachment.html>


More information about the Chat mailing list