Looking at questions such as these
- If immutable objects are good, why do people keep creating mutable objects?
- What are the drawbacks of immutable types?
- At what point do immutable classes become a burden?
- When is it inappropriate to make objects immutable?
it looks to me that people are asking “why not immutable?” and thought I’d ask another kind of question.
Let’s say immutable objects are (mostly) good – even if you disagree, humor me for a sec.
As of now, there seem to be more than 6k Java projects on freecode.com. There’s probably a lot that are mis-tagged, but also many more that are not listed. So let’s say there at least a thousand of these. And we can safely count other ones, such as C#, that are equally relevant.
So do you have any examples of relatively successful big projects that use immutability to any reasonable degree? I mean, everyone can write baby examples showing how good everything is when it’s immutable, but if there are 10k+ LOC, then that’s a different story.
I can understand old projects – OK, historical burden, it would be a huge undertaking to rewrite e.g. Firefox to be mostly immutable. I can understand specialized things, e.g. Guava collections, which are designed like that from scratch. Are there examples of real-life applications, preferably new ones, that are like this? How much is “mutable Java” better / worse vs. “immutable Java” and in what aspects – but from practical standpoint?
19
You’re asking for examples from the wrong programming language. Java is one of the unfriendliest languages for immutability, both from a development culture and from a language feature perspective. To work with immutable objects on a large scale without going insane, you need functional-style language features, which Java didn’t even start to consider until Java 8, and even there are still quite limited. That’s why in Java you mostly see immutability in Strings
and other relatively small data transfer objects.
Languages where you would find large projects with ubiquitous immutability include, but are not limited to, Scala, used at LinkedIn, Twitter, and FourSquare, and Erlang, used at Amazon, Facebook, and Ericsson. The reason these are the most prominent examples is immutability becomes pretty much a requirement when dealing with massive parallelization and distributed computing.
In other words, contrary to your assumptions, it’s mutable data structures that become most problematic as you scale up. Don’t get me wrong, immutability is a difficult constraint for programmers to become accustomed to at first, especially making it efficient, but once those problems are solved in small programs, they don’t get inherently worse in larger programs. That’s because immutable data structures tend to engender algorithms that are much easier to decouple.
1
You are asking about Java, but in your question you also mention C#, so I will answer with a significant project in that language: Project Roslyn, Microsoft’s from-scratch rewrite of the C# and VB.NET compilers in C# and VB.NET uses immutable datastructures throughout. In particular, all the parse trees and semantic trees are immutable, and every operation and transformation of that tree, whether that be optimizations done by the compiler, annotations done by some static analysis tool, refactorings or code fixes done by the IDE, macros done by some user plugin or whatever else always produces a completely new tree.
So, this is a project that is commercial, large, industry-strength, performance-sensitive (AFAIK, the Roslyn C# compiler is currently within a few percent of the old C# compiler written in C++) and will very likely be at the very core of not only the next version of .NET, but also the next version of Visual Studio.
Oh, and it is also fully Open Source under the Apache Software License 2.0, so you can see how they did it: https://roslyn.codeplex.com/
8
Without limiting the scope to Java, I’d say Cocoa is one of the big projects out there. It’s used by iOS and OS X applications and I bet many of the programmers would stumble on how NSString (the basic string class) is immutable and learn to use NSMutableString. Same with data structures (NSArray, NSDictionary, etc.)
I’d also say many are using them (immutable classes) just because how they learn Cocoa without understanding why immutability is good.