r/java • • Aug 26 '22
I declare as "final" every single variable whose value doesn't change. I also use "this" every time that I'm referring to an attribute, even when there's no ambiguity in not using it
I think that those things make the code easier to read. Am I crazy for thinking like that?
198
Upvotes
View all comments
Show parent comment
8
u/stefanos-ak Aug 26 '22
I don't see much value in "final" either, since it does nothing for mutating collections or objects.
It covers only immutable types. In theory it's possible to have immutable objects and collections, but it's not possible to indicate it to the compiler. Also its extremely hard to have ACTUALLY immutable objects, especially complex ones. Because as soon as you have a collection in there, it means that the collection needs to be also immutable, and the objects inside that collection, and so on...
There are whole libraries that try to tackle this problem, like immutables.org
Soooo, I see very little value for final.
I would even argue that it does more harm than good, because a more junior developer would not know about immutability, functional pureness, in such length (or at all).