[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: the benefits of immutability



Perry E. Metzger wrote:


 > > The finality of the String class is a consequence of its
 > > immutability.
 >
 > That's just plain incorrect. Obvious evidence is that there is also
 > a mutable string type and it is also final -- i.e. cannot be
 > subclassed. Just because the strings themselves are immutable
 > doesn't mean that you can't safely add extra methods etc. to a
 > subclass. The two are unconnected.

Generally speaking, it can be pretty hard to safely add methods to a
subclass without violating the Liskov substitutability principle.  A
classic example of this is the one cited by Joshua Bloch, which can be
found here:

http://courses.dce.harvard.edu/~cscie160/EffectiveJava.htm

(Search for "Don't use inheritance un-skeptically")

The problem is not that you can't add methods safely.  The problem is
that it is too easy to add them unsafely.  One of the String's stated
invariants is its guaranteed immutability.  The only way to make sure
this invariant is *never* violated is to prevent subclassing.  Thus,
true immutability also requires finality.

 > I think you miss the point.

I am sure I do.  I thought the point was that Java sucked.  I am not
going to argue with that.  I am just nitpicking on the specific
arguments brought up to back up the overarching suckiness claim.
Matter of fact, I am very interested to hear good arguments against
Java.  I have a small collection of those:

Java Critique
http://www.peterbecker.de/texts/javacritique.html
http://www.jelovic.com/articles/java_good_bad.htm
Java Is Not a Hairball
http://www.theregister.co.uk/content/4/19474.html
ASP better for Web Than Java
http://www.theregister.co.uk/content/4/18996.html
Java Design Flaws
http://c2.com/cgi/wiki?JavaDesignFlaws
(Checked) exceptions are bad
http://groups.google.com/groups?hl=en&selm=aacf3eb9.0203151012.164c4920%40posting.google.com
http://lambda.weblogs.com/discuss/msgReader$3194?d=1&m=5&mode=topic&y=2002
Java is a LFM
http://www.artima.com/weblogs/viewpost.jsp?thread=5246