[LRUG] The advantage of immutability?

Luke Morton lukemorton.dev at gmail.com
Wed Sep 9 03:42:19 PDT 2015


Great question Jonathon! I'm getting more and more into immutability and
continually find more reasons why it can be beneficial. I think stateless
code is often easier to reason about and this is the primary draw for me.

I put one particular argument for immutable class design in a blog post
recently:
https://www.madetech.com/blog/an-argument-for-immutable-class-design

Let me know what you think :)

On 9 September 2015 at 11:37, Tom Stuart <tom at codon.com> wrote:

> On 9 Sep 2015, at 11:25, Jonathon Horsman <jonathon at arctickiwi.com> wrote:
> > Can you shed some light on why an immutable object is an advantage for a
> web-based Ruby app?
>
> One short answer is that immutable values eliminate a whole class of
> confusing bugs.
>
> For example, when you pass an object into a method, what can happen to it?
> If it’s immutable then the answer is: nothing, you don’t need to worry
> about it. But if it’s mutable, then maybe that method makes some change to
> the object (perhaps accidentally!) that is going to make your life
> difficult later on. You see this sort of thing all over the place in Rails:
> methods that go to great lengths to duplicate their arguments so that they
> can mutate them without leaking side-effects back to the caller.
>
> You’re right that it can sometimes require more programmer work to create
> new values instead of mutating existing ones, and in the naive case it can
> also take more time and memory to do things that way, but the trade-off is
> that your program becomes simpler to reason about and you’re less likely to
> introduce really sneaky bugs caused by shared mutable state.
>
> Cheers,
> -Tom
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20150909/54a368db/attachment-0003.html>


More information about the Chat mailing list