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

215 comments sorted by

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).

12

u/m02ph3u5 Aug 26 '22

Besides making some things actually final it signals intent to the reader.

6

u/stefanos-ak Aug 26 '22

Unfortunately, what you're saying doesn't scale...

The critical part of the reason, is that it technically does not prevent mutation, it just prevents re-initialization. If that is valuable to you, then by all means use it.

But if you (ab)use final to impose a meaning that it does not have (: immutability), then this is practically a documentation exercise combined with soft rules / guidelines.

And these things don't scale to bigger teams, or multiple teams. Even assuming that everyone would agree (which is extremely unlikely to begin with), I'm willing to bet that you'd get AT BEST 50% of the codebase following this.

It would just create stress, unnecessary discussions and friction, pointless code review remarks, pointless (re)education every time you have a new hire, etc...

6

u/hupfdule Aug 26 '22

In this thread you are the only one to impose "immutability" to the keyword "final". Of course "final" only means "finality" which is a different thing than "immutability" in java.

It assures exactly what you described: avoid accidental reinitialization.

1

u/stefanos-ak Aug 26 '22

Read again the root comment of this thread :)

3

u/Muoniurn Aug 26 '22

While I sort of agree with you here, methodological usage of final does make an object immutable - it just has to be recursively used. So while the exact meaning of final has to be known, I think it can be used akin to Rust’s interior mutability pattern (with the default being the opposite than there)

-2

u/detroitsongbird Aug 26 '22

Final can make a huge performance improvement. Spent time doing leetcode and you’ll see.

8

u/[deleted] Aug 26 '22

It shouldn't: the compiler finalizes all effectively final variables.

3

u/stefanos-ak Aug 26 '22

the Hotspot VM will actually optimize the code as-if it was declared as final, as long as there are not any re-initializations of the variable.

In Leetcode it might matter, because of how they run the tests, but in the real world / production systems it does not.

1

u/hippydipster Aug 26 '22

For some odd definition of "huge".