"Mutable stateful objects are the new spaghetti code" --Rich Hickey
In Java most classes are mutable and that's the default, so that’s the way that most Java developers write code. In this talk you’ll see why mutable code fails: though easy and fast in the short term, you end up with a complex mess that's hard to understand, test or reason about, and is a concurrency nightmare to boot. Finally, we'll challenge the prevailing paradigm, exploring how simple immutable objects excel in these same cases.
1. Make Your Life Better With
Immutable Objects
Maxim Novak, Wix
https://github.com/maximn@maximnovakmaximn@wix.com
2. I’m Maxim Novak
▪ Avid Scala advocate
▪ Team leader at Wix
▪ Building core services
▪ 10 years developing software
▪ TDD and clean code enthusiast
Lithuania
Vilnius
Kyiv
Dnipro
Wix Engineering Locations
Israel
Tel-Aviv
Be’er Sheva
Ukraine
3. What’s an immutable class?
An object is considered immutable if its state
cannot change after it is constructed.
Maximum reliance on immutable objects is
widely accepted as a sound strategy for
creating simple, reliable code.
...
- Oracle Java documentation
https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
4. GOOD QUESTION
But they made the most important one
immutable
java.lang.String
So why are most Java classes mutable?
5. What do you mean can’t change it ?!
So how do I work with it?
Create a new instance with the modified state
9. “I think that large objected-oriented programs struggle with increasing complexity
as you build this large object graph of mutable objects. You know, trying to
understand and keep in your mind what will happen when you call a method and
what will the side effects be.”
- Rich Hickey (2010)
Easier to reason about
https://pixabay.com/en/fly-swatter-flyswatter-fly-flap-bug-149265/
10. void foo(SomeInterface func) {
Data data = new Data("initial state");
func.apply(data);
log(data);
}
Easier to reason about
Side-effect free (share safely)
21. Set<Date> set = new HashSet<>();
Date date = new Date(2);
set.add(date);
date.setTime(4);
System.out.println(set.contains(date));
HashCode Object
1
2
3
4
...
...
n
date(2)
date
Avoiding Identity Mutability
25. Advantages
(Why do you want it)
● Easier to reason about
● Side-effect free (share safely, no defensive copies)
● Easier to test
● Easier to debug
● Avoid temporal coupling
● Avoiding Identity Mutability
● Always have failure atomicity / Prevent NULL references
● Always Thread safe
26. Disadvantages
(What you should beware of )
● Performance under certain conditions.
● Brings some difficulties when working in Java
27. So how do I start?
(some rules of thumb)
● Make all fields final & private
● Create methods that create a copy
of the class with a modified state
● Disallow inheritance (final class)
28. So how do I start?
(some rules of thumb)
● If you’re using mutable classes inside
○ Copy them on construction
○ Don’t expose references to internal fields
(copy defensively)
○ Don’t provide any method that changes
the state (setters)
29. So how do I start?
(How can I make it easier?)
https://immutables.github.io/
Lombok, AutoValue, and Immutables
30. Project sample?
https://github.com/yegor256/takes
- Yegor Bugayenko (http://www.yegor256.com/)
1. not a single null (why NULL is bad?)
2. not a single public static method (why they are bad?)
3. not a single mutable class (why they are bad?)
4. not a single instanceof keyword, type casting, or reflection (why?)
31. ״Simplicity is hard work. But, there's a huge payoff. The person
who has a genuinely simpler system - a system made out of
genuinely simple parts, is going to be able to affect the greatest
change with the least work. He's going to kick your ass. He's
gonna spend more time simplifying things up front and in the long
haul he's gonna wipe the plate with you because he'll have that
ability to change things when you're struggling to push elephants
around.״
— Rich Hickey, Creator of the Clojure programming language
32. Make Your Life Better With
Immutable Objects
Maxim Novak, Wix
https://github.com/maximn@maximnovakmaximn@wix.com