Immutability
Classes
This depends on design decisions
By using "final" in class signature, you cannot sub class it
Make constructor private, use static factory methods
If constructor is non default, create a new default private constructor and throw an
AssertionException("should not instantiate this class")
. This avoids reflection being used to create an instance
https://stackoverflow.com/questions/12306651/why-would-one-declare-an-immutable-class-final-in-java
https://www.infoworld.com/article/2073003/is-a-java-immutable-class-always-final-.html#:~:text=By%20declaring%20immutable%20classes%20final,immutability%2C%20are%20not%20a%20concern.
Fields
Use of private final fields
No setter methods
Methods that change the state, should be avoided
instead return a new object with the new state in constructor
But if you have access to the fields via a getter, then the object can stil be mutated internally
Thus getter has to either return a copy (deep or shallow copy depending on the object graph)
Arguments
Either by convention (to avoid cluttering code), keep all method parameters final.
Can use the keyword "final" in method parameters signature
Even better would be to make any argument passed into a method be immutable
This can be hard, and also lead to trade off in creating less readable code
Variables
Assign once
Bad
Better, use ternary and assign to variable
Better, wrap it in a function
If used once, can inline, and get rid of variable (as long naming of method is good)
For complex conditions, like switches, can wrap it up in a method
Localise scope
Distance between assigning variable and use is far apart
Better, use private method
Better use stream
Last updated