There's a presentation on AutoValue (a terse way to define immutable objects in Java, with sensible defaults for equals
, hashCode
, etc.): https://docs.google.com/presentation/d/14u_h-lMn7f1rXE1nDiLX0azS3IkgjGl5uxp5jGJ75RE/edit#slide=id.g2a5e9c4a8_047
In the beginning, they go over some alternatives to AutoValue. Obviously, one alternative is a POJO coded by hand. They warn about this POJO being unexpectedly hard to change.
I've transcribed the slide here:
Are changes really that risky?
Suppose a required int field becomes optional. You change
int number;
to
Integer number;
Tests still pass... and everything's fine... until one day, objects put into Sets start mysteriously "disappearing" from the set! Why? (Left as an exercise to the reader.)
I can't figure out this "exercise". What kind of "disappearing"? contains(element)
is unexpectedly false
? size()
decrements without remove()
? iterator()
doesn't include the element anymore? And why would any of these happen?
AFAIK the main thing that would break is
equals
, becausethis.number == obj.number
has unexpected results whennumber
is anInteger
.That would affect
contains
as well asremove
(both unexpectedlyfalse
), I think.I'm still a bit green with Java though, so if anyone has other comments or answers, I'd appreciate it. Mostly, I don't think
size
oriterator
would be affected, so presumably the element would still show up in debuggers that useiterator
or some fancier way of getting the contents. That doesn't sound much like "disappearing" to me so I'd like to know if I'm missing anything.