Looking at questions such as these

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?

closed as too broad by user22815, gnat, rwong, user40980, GlenH7 Oct 16 '14 at 1:07

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    Immutability is a tool. It is not something you either use or do not use throughout a project. It is not a design pattern or framework. It is simply a way of representing specific data that is thread-safe and avoids side effects in situations that warrant it. – user22815 Oct 14 '14 at 21:53
  • 1
    Yes, immutability is just a tool, and one used (at least in most languages) only for some situations. But it remains a fair question: are there any significant usage examples / success stories were immutability was substantively used? Even better if it was used to solve otherwise difficult or intractable problems. – Jonathan Eunice Oct 14 '14 at 22:05
  • 1
  • 1
    I have written pretty complex programs in Scala that do not use mutability at all. I have also written a good amount of code in Java using only immutable data structures. Once you get used to immutability, you see mutability as (1) a way to store state (which you need much much less often than you may think!) and (2) as an optimization (again, much less needed than you may think). As a gut feeling, you can write very good software where at least 80% of the code does not use mutability. The big advantage that you get is that it is much easier to reason about your code. – Giorgio Oct 15 '14 at 5:19
  • 2
    Note that there is an alternate explanation why there are no large complex projects using pervasive immutability other than "nobody uses pervasive immutability". Namely, that pervasive immutability makes programming so much simpler that your projects simply won't get large and complex in the first place! I'm not saying that this is the case, I'm just saying that concluding that immutability isn't used much from the fact that there are no large complex projects, seems logically iffy, especially with the premise of your question that "immutability is good" (e.g. reduces complexity). – Jörg W Mittag Oct 15 '14 at 8:28

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.

  • Thanks Karl. I'm not suggesting immutability doesn't scale - just that I did not see non-baby examples of immutable Java applications. If you have any, I'd like to see them and see how they look like, so I can understand the trade offs. Or are you basically saying "don't use immutability in Java & co., it's not worth it"? – levant pied Oct 15 '14 at 12:07

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/

  • Thanks Jörg - this looks interesting, something to dive into. Me using iffy logic again :) - can I conclude the sparsity of these projects to mean that trade offs are not worth it in many cases? – levant pied Oct 15 '14 at 12:45
  • 1
    @souslesquels - Not really, because the sparsity of projects heavily using immutability is based primarily on language decisions and the available features those decisions provide, not project size. Large projects written in languages that provide features more heavily from paradigms other than OOP, such as Haskell or Lisp/Scheme, will almost certainly have a large amount of immutable structures. The primary languages in use today, such as Java, C#, and Javascript, do not provide the features to make immutability easy. Consequently, large projects in those languages rarely use immutability. – Jack Oct 15 '14 at 22:08
  • @souslesquels: No, I don't think so. You are suffering from heavy selection bias here: most programmers who prefer immutability would never have chosen Java or C# in the first place. If you look for open source projects in Haskell, for example, you will find that close to 100% are purely immutable and even the small numbers that do use mutability will only use it in a very small, well isolated part of the program. The same goes for Erlang, ML, Ocaml, F#, Scala, Scheme, Clojure. – Jörg W Mittag Oct 16 '14 at 7:45
  • @Jack I was not implying the choice is based on project size. I was just saying that there are almost no Java & co. projects that use immutability significantly (apart from specialized things such as Guava). So, it's not practical in Java to use immutable objects, is that the gist of what you are saying? – levant pied Oct 16 '14 at 12:07
  • @JörgWMittag That makes sense. However, the statistics do not favor that view. Is it likely that thousands of Java & co. projects are all made up of mostly "I like mutable" kind of people? Consider a lot of questions here at SE tagged Java & co. asking the same kinds of questions. I would agree the bias definitely exists, but not that it's that big. On the other hand, lack of examples might be proving me wrong :) – levant pied Oct 16 '14 at 12:49

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.