Immutable objects [closed]

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật