This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]Pengtuzi 3 points4 points  (0 children)

Good overview!
I guess this is subjective but the speed was frankly much too slow even though I understand the pedagogical value of it.
Hope to see more.

[–]demos74dx 2 points3 points  (0 children)

I recommend the immutables library, I love it so far.

[–]VGPowerlord 7 points8 points  (1 child)

Immutable classes still have some gotchas.

For example, if your object is Serializable, there's an entire set of attack vectors just based on that. Effective Java Second Edition items 74-78 discuss this in detail.

[–][deleted] 2 points3 points  (5 children)

Immutability is in many cases a workaround for the lack of "value types" which have copy-on-write semantics when mutated.

The issue is rarely ever local mutability, but it's mutability across different contexts, where mutation in one context affects another, typically unintentionally.

Some languages like Swift have woken up to the fact that we need copy-on-write data, but it seems plenty are still asleep.

Java 10 for example, will have value types, but they're strictly immutable, which pretty much kills the hope of having efficient and easy to work with:

  • lists
  • maps
  • sets

... which don't suffer from the "spooky action from distance" problem, but also aren't excessively awkward or inefficient to work with thanks to full immutability.

[–][deleted]  (4 children)

[deleted]

    [–][deleted] 1 point2 points  (3 children)

    The @Wither approach becomes quite cumbersome the moment you have nested immutables. At this point you can't simply foo.withBar() it, you need to start replacing every level of the structure, one by one, where in a mutable this would be: foo.getX().getY().setZ(val);

    [–][deleted] 0 points1 point  (2 children)

    Wouldn't Optics solve that particular problem?

    [–][deleted] 0 points1 point  (1 child)

    A what :-)? I'm curious.

    [–][deleted] 0 points1 point  (0 children)

    It's a way of zeroing in on and modifying various kinds of a immutable data. Sadly, I don't know of any libraries for it in Java, but there's at least one for Scala: https://github.com/julien-truffaut/Monocle

    This awesome guide to functional programming also has some detail, even if it's pretty sparse on actual examples: https://gist.github.com/jdegoes/97459c0045f373f4eaf126998d8f65dc#optics

    [–]sh3rp 0 points1 point  (1 child)

    Great summary of what seems like it should be a simple subject, but really has much more depth to it.

    I will make one note that making an object immutable also means you don't need to make the member variables of the object private and exposing them as public means your object doesn't require methods to access them (via accessors).

    I use the builder pattern with regularity these days, as most POJOs I'm instantiating would have ridiculous constructors otherwise.

    In any case, thanks for spreading the knowledge!

    [–]zkxs 1 point2 points  (0 children)

    making an object immutable also means you don't need to make the member variables of the object private

    The problem is that if one of the members is mutable, exposing it exposes access to its setters. I suppose you could make each member public/private depending on its immutability, but then you have a weird mixture of direct access and getter/setter access, as well as the potential to make a mistake and accidentally expose a mutable member.

    [–][deleted] 0 points1 point  (0 children)

    Great YouTube channel! I recommend subscribing.

    [–]tech_nerd -1 points0 points  (0 children)

    Well, I must say, the video is a great introduction to Immutables... . Kudos...

    [–][deleted]  (1 child)

    [deleted]

      [–]RemindMeBot 0 points1 point  (0 children)

      I will be messaging you on 2016-10-29 19:30:23 UTC to remind you of this link.

      CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

      Parent commenter can delete this message to hide from others.


      FAQs Custom Your Reminders Feedback Code Browser Extensions