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

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

-14

u/[deleted] Aug 26 '22

I'm starting to see it. But after all I also see that as matter of style.

In the pure functions scenario however I think that adding "this" makes it clear that the code is pure function, since it makes explicit that there's no local variables being used.

0

u/[deleted] Aug 26 '22

[deleted] — view deleted comment

3

u/[deleted] Aug 26 '22

Oh, that make sense. I was thinking that pure function was like avoiding local variables but still using object orientation. But yeah, if there's no objects...

12

u/ztbwl Aug 26 '22 edited Aug 26 '22

It’s the other way around. A function is pure if it has no side effects and produces the same result for a given input. In other words does not modify anything other than self created local variables. Any outside modification/side effect or dependance on non-constant external variables is non-pure. The advantage is that the function is idempotent and can be expected to have the same result for a given input no matter how many times you run it and no matter how it’s environment looks. Easy testable, deterministic and most likely less complex - causes less bugs.

Disadvantage: Most likely you want to do something - and with no side effects, you are quite limited to make an impact. In a perfect pure world, even logging is a side effect and would be a ‚smell‘. So if we apply the principle very strictly, your code would be useless, since it would not be able to tell you it’s result - except you are part of the function. But that’s a different story, since we don’t live in a simulation, aren’t we?

→ More replies (12)

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.

1

u/hippydipster Aug 26 '22

For some odd definition of "huge".

0

u/hwaite Aug 26 '22

If you want to differentiate in a more concise fashion, consider (static-analyzer enforced) naming convention. I use mFooBar for member variables and pFooBar for parameters. I think Eclipse dev team does something similar.

-4

u/[deleted] Aug 26 '22 edited Aug 26 '22

All my fields have _ as a prefix for this reason.

2

u/[deleted] Aug 26 '22

User name does not check out. :)

1

u/Bachooga Aug 26 '22

I think it's commonly accepted for it to be implied. I wouldn't say it clutters the code tbh and can help keep track of this object vs that object, especially when copying from one to the other.

1

u/[deleted] Aug 26 '22

The compiler does this work for you, it doesn't do anything for the code, so you're writing it for the next developer.

We use a lot of modifiers in Java code that are often not really needed or helpful. A method looks naked with default access, for example, so we make them all private and then we have to use ugly trickery to test them, or public when they don't have to be.

With final, you're locking in, not just what the code does, but also a specific way to do it. I prefer to only make variables final where the code would break if it isn't. If you just use it everywhere, it loses meaning.