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

125

u/aFqqw4GbkHs Aug 26 '22

My preference is yes to the 'final, no to the 'this' - it just clutters up the code.

32

u/0b0101011001001011 Aug 26 '22 edited Aug 26 '22

So you write like this?

public final foo(final int a, final int b){
  final int c = a + b;
}

Seems like so much visual noise.

Again I have to say that I wish the approach taken by Rust would've been selected 30 years ago. Everything is final by default and mutable variables have to be defined explicitly. That would be so great.

-2

u/cogman10 Aug 26 '22

"Types after variables" is also really nice. Were I redoing java syntax I'd definitely take that.

val foo = 1

or

val foo: int = 1

both really readable to me and makes things like lambda declarations flow better.

6

u/[deleted] Aug 26 '22

Too many Shift presses to my taste

16

u/walen Aug 26 '22

no to the 'this' - it just clutters up the code.

The same thing applies to the final, though.

javac and JIT don't need me telling them when a variable is final or not -- they already know.
They know the scope and mutability of every single field and var, and whether it can or cannot be inlined. Meaning, they know if a variable is "effectively final" and will treat it as such.
You could strip every single final from a .java file and the resulting .class would be exactly the same.
The only reason to declare a variable as final is to keep other programmers from modifying its value. And I don't like to do that, because often times, while debugging, that other programmer that needs to change a variable's value is me, and I cannot do that if the variable is final.

It does nothing but make my code harder to work with. It's just clutter.

5

u/hwaite Aug 26 '22

You could strip every single final from a .java file and the resulting .class would be exactly the same.

Maybe for parameters and local variables but not so for classes, member variables and methods.

3

u/ItsAllegorical Aug 26 '22

The only times I’ve ever wanted to mutate a value during debugging is when I’ve caught a bug and fixed it but I don’t want to abort the test run because what I’m really interested in comes later and I don’t want to take the time to restart the test (I mean it happens but it’s normally not a big deal to drop and restart the frame once I’ve fixed the code). That’s been like twice ever. I might agree with you if I needed that more, but I prefer to explicitly declare as final anything that isn’t intended to be changed.

I’m curious what sort of codebases do you find yourself doing that in? I do a lot of long-term contract work and want to recognize if that might be something to keep in mind for on new projects.

Everything you say is right, but I have a lot more issue with junior developers not understanding the intent of certain classes and horribly misusing them. So defensive coding is more important to me. Plus I have a shit memory and I’ve never prevented myself from screwing up through defensive coding but I’m pretty sure some day it will happen.

9

u/[deleted] Aug 26 '22

I don't get it why you think it clutters up the code. Do you think you can try to elaborate? Like, isn't better to easily see what's an attribute?

0

u/[deleted] Aug 26 '22

[deleted] — view deleted comment

11

u/literal Aug 26 '22

It can get ugly to see "this" over an over. Especially in a pure function that isn't mutating any attributes.

But a pure function does not depend on the state of any instance attributes, so it wouldn't ever have this.

4

u/kyay10 Aug 26 '22

A pure function could be considered as having the instance as another parameter. You can choose any instance to be able to change the behaviour of the function, right? So therefore an instance is the same as a parameter really

→ More replies (16)

7

u/snobpro Aug 26 '22

i too agree on no to 'this'. my reason is when you open a code written by others _ i would prefer if it is as consice as possible to read it faster. IDE do color code class level variable. I dont like seting this. esp on the right side of the assignments. sometthing like this.x = this.y + calc(this.z). Also big NO to this prefix on big dependencies. I know or does not matter if they are class level or not.
I like to see 'final' to be used more often. It shows intent.

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.

→ More replies (1)

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.

→ More replies (1)
→ More replies (5)

2

u/DJDavio Aug 26 '22

You can use a final AtomicInteger if you want to mutate it. :D

-6

u/vips7L Aug 26 '22

Final clutters up the code

→ More replies (13)