Questions tagged [immutability]
Immutability is about having objects that cannot be changed.
169 questions
4 votes
2 answers
2k views
Should I expose an instance's state with methods?
I've read somewhere about a pattern that is as follows: if it'll change the state of the instance, the method should be named with a verb and return void; and if the method just returns something (...
0 votes
1 answer
300 views
Is immutable objects over POJO in general?
Lombok is used in the legacy project where I am currently working for since last year. The project is legacy with 10+ years, and POJO/JavaBeans, i.e. @Data annotated classes, have been widely used for ...
1 vote
5 answers
398 views
Is storing computed values always bad?
Edit: I'm copying the question but changing the example code. Apparently, I used a bad example earlier that contained an imprue getter. I'm keeping the old example code at the bottom so the first ...
6 votes
2 answers
1k views
Object immutability and persistence
While I was learning functional programming I have encounterd two for me very similar terms: immutability and persistence. Also I have read simular questions from stackoverflow, but I am still ...
10 votes
9 answers
4k views
What is an immutable object anyway?
The most popular answer is - it is an object whose state does not change after creation. What does it actually mean? My understanding is that any method call on the object should give the same result. ...
8 votes
2 answers
2k views
Why does C# List<T>.AsReadOnly() allocate?
I am looking at the List<T>.AsReadOnly() method. Since List<T> itself is a IReadOnlyCollection<T>. (It implements IReadOnlyList<T> and IReadOnlyList<T> implements ...
16 votes
6 answers
7k views
Why can't a mutable interface/class inherit from an immutable one?
I've heard people say things like "B can't inherit from A because A is immutable and B is mutable". My understanding of inheritance in Object-Oriented Programming is that you use it to add ...
1 vote
3 answers
2k views
Const function that change other objects' states
Is it a good idea to mark a function const from clean code perspective if it change other objects' states? I'd like to know what is the experience with this or is it considered a bad practice for any ...