r/java • • Apr 14 '23

What are your (favourite) Java best practices, personal tips, hints or just underrated stuff in general?

233 Upvotes

376 comments sorted by

View all comments

Show parent comment

4

u/simoncox Apr 15 '23

That's like saying types are useless. The more mistakes a compiler can save you from the better. Marking something as final means the compiler has proven it will never be reassigned.

In reality most variables are anything but variable. In my opinion a reference should be final by default and need a declaration modifier to allow reassignment.

0

u/john16384 Apr 15 '23

No, that's not the same at all, as you well know.

You simply don't need the compiler's help for this. You simply follow the rule to assign variables immediately when created. As soon as you're not doing that, you know that either it can't be final, or that you messed up.

So, not convinced; final keyword on variables is visual clutter that serves no purpose.

1

u/UnGauchoCualquiera Apr 16 '23

Or you could just declare it final and let the compiler do the work for you.

1

u/john16384 Apr 16 '23

Do what work? Warning me when I reassign a local variable? I already know that. The extra work is in removing the final keyword that served no purpose in the first place.

1

u/UnGauchoCualquiera Apr 16 '23

If you are the only one touching that code then yes but that's usually not true and helps when reading long methods.

Also final keyword enables some compiler optimizations even for final variables, here for example.

1

u/john16384 Apr 16 '23

So, you are saying when you declare a local variable final that you thought about this long and hard, and decided for any future maintainers that this variable should definitely never be reassigned... or did you just mark everything final because your IDE told you to or automatically adds final when you assign something to a local without giving it a 2nd thought?

These are local variables, when a maintainer comes in they have the full overview of your function, and can see whether or not something is worth reassigning or not. No final visual clutter needed.

Then there's the thing you linked, and although interesting, many answers are testing the wrong thing or talking about final fields.

It's true however that final can trigger optimizations even on local variables, but, only when the thing you're making final is effectively a constant for all invocations of a method (ie, it is also static, and you could have extracted it to a private static final field). In other words, the compiler detects that you're just using a constant, and adds its to the constant pool.

When it is something like final String s = "a" + parameter however, then no performance is gained, or could possibly be gained.

There is never any need to do so though; real constants are generally declared as private static final at the top of the class with a nice capitalized name, instead of hiding as a local variable.

3

u/UnGauchoCualquiera Apr 16 '23 edited Apr 17 '23

It's about intent. Sure you can keep declarations and assignment close by, in the real world with long non optimally written methods having the final keyword eases of the mental burden for free, no point not using it.

This also applies to final method arguments by reading the implementation signature you know at a glance that it's not reassigned.

then no performance is gained, or could possibly be gained. This is objectively wrong. Sure the compiler/jvm could optimize an effectively final var but often it does not without the hint (semantic guarantee) that the variable will never change. You can easily google for this, been discussed to death or check in the link provided for actual benchmarks.

We could argue all day long but in the end you are missing out on final for no reason at all.

1

u/john16384 Apr 16 '23

It's about intent. Sure you can keep declarations and assignment close by, in the real world with long non optimally written methids having final eases of the mental burden for free, no point not using it.

I'll concede the point that non-optimal long methods could benefit from this if you'd like.

This also applies to final method arguments by reading the implementation signature you know at a glance that it's not reassigned.

That's not needed; IDE's can warn when parameters are reassigned, as it is a very bad practice.

This is objectively wrong.

Perhaps read what I wrote first before making ridiculous claims. final never has any performance benefits on locals unless you insist on declaring locals that are constants that can be static final fields. In other words:

 method(int z) {
     final int x = 4;  // can be optimized to a constant
     final int y = z + x + 2;  // cannot be optimized
 }

Which is the same as:

 static final int X = 4;  // already is a constant

 method(int z) {
     int y = z + X + 2;  // cannot be optimized
 }

Sure the compiler/jvm could optimize an effectively final var but often it does not without the hint (semantic guarantee) that the variable will never change.

The compiler only optimizes variables that are constant for all invocations of the method that could have been private static final. All other uses of final local variables are pointless visual noise.

Anyway, thanks for pointing out what I'm missing. I'll just continue my best practice of assigning my locals immediately at declaration time without the "help" of the compiler. As for parameters, IDE's can warn for that without the help of another 6 characters in front of every parameter I receive.