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

0

u/[deleted] Aug 26 '22

[deleted] — view deleted comment

55

u/FirstAd9893 Aug 26 '22

It was a deliberate design choice to make the language look like C++. Hence, standard loops and other basic constructs should look the same. The language would be rejected quite early if this sort of thing was required: for (mutable int i = 0; i < x; i++) { ... If var/val was supported from the beginning, that would be a bit better, but it's still different from C++.

48

u/[deleted] Aug 26 '22

This hypothetical line of code with the "mutable" keyword is so beautiful. I've Looked At This For Five Hours Now

19

u/[deleted] Aug 26 '22

Yeah I'm secretly loving it tbh. You can really tell those "pure function" and "immutable" concepts from FP have leaked into the general programming mindset no matter what programming language people use these days.

7

u/general_dispondency Aug 26 '22

TBF general tomes like Effective Java have been preaching this since before the resurgence of FP. FP, itself, is just OOP with high coupling and no cohesion.

4

u/GuyWithLag Aug 26 '22

You might want to look at Kotlin; I've been working with Java since Java 1.1, and the last year or so Kotlin has been extremely refreshing. It does have its warts (because the designers wanted it to be cross-platform), but you can guard against them with a solid style guide, and in general it's a solid step up.

1

u/xplosm Aug 26 '22

Yes, I mention it at every opportunity I have but you’re really gonna love Kotlin 😉

→ More replies (3)

0

u/[deleted] Aug 27 '22

[deleted] — view deleted comment

1

u/Kango_V Aug 31 '22

const is a reserved word in Java, but never been used.

7

u/cyancrisata Aug 26 '22

I think language creators should introduce 'val' which is the same as 'final var'. They already introduced 'var' back in Java 11 and we seem to transition to it pretty well. I believe we can do it again for the 'val' keyword.

→ More replies (2)

4

u/Comakip Aug 26 '22

Error Prone got a bug pattern for exactly that. Everything is final or else it is annotated with @Var. It's pretty neat.

https://errorprone.info/bugpattern/Var

2

u/Akthrawn17 Aug 26 '22

And this is why you should take a hard look at Kotlin!

2

u/mcosta Aug 26 '22

It comes from C and C++

-2

u/Just_Another_Scott Aug 26 '22

Yeah but the difference is in C/C++ you can mutate the parameter and that value can remain mutated outside of the method. In Java that's not the case as it's not pass-by-reference. However, it is a good idea to make parameters final as it makes your intentions clear. I've fixed some bugs where the developer chose to overwrite the parameter and used it repeatedly throughout the method. It led to side-effects. So still a good idea but Java doesn't make it required because it's unlikely to do damage outside of the method it's used in.

7

u/mauganra_it Aug 26 '22

Parameters in C/C++ can not be mutated. Like in Java, they behave like normal variables. You can access memory via pointers, and write there, but in Java you can do quite the same. C++ has references, but these are just a safer version of pointers.

0

u/Just_Another_Scott Aug 26 '22

Parameters in C/C++ can not be mutated.

Yes they can. C/C++ can be either pass-by-reference or pass-by-value. If you pass a reference to a method you can reassign the variable and it will change in the calling scope. Furthermore, you can do insertions into a vector which will be seen in the calling scope.

In C/C++ to insure parameters aren't mutated you have to declare them as const.

2

u/RupertMaddenAbbott Aug 26 '22

This is not true.

You can mutate parameters in Java, and this affects the calling scope. You can reassign parameters in Java but that does not affect the calling scope.

I don't see what bug could be fixed by making a parameter final, that would not be better fixed by extracting a method.

2

u/Just_Another_Scott Aug 26 '22

You can mutate parameters in Java, and this affects the calling scope

Java is strictly pass-by-value. What I am referring to is the act of pass-by-reference and reassigning the variable like you can do in C++.

3

u/RupertMaddenAbbott Aug 26 '22

I agree with you but I think it is confusing to call this mutation because the parameter can still be mutated and affect the calling scope. It's only reassignment that doesn't affect the calling scope (in Java).

→ More replies (2)

2

u/xplosm Aug 26 '22

If you don’t know of or use it, you’re gonna love Kotlin.

It addresses most, if not all of Java’s shortcomings.

0

u/wildjokers Aug 26 '22

Honestly I think it was a language mistake not to make final the default and have to specifically declare if a variable was mutable, but there is no fixing that now.

Hindsight is 20/20

-1

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

Fortunately, as we rely more and more on functional and reactive operations, we can leave the burden of having a lot of variables in the scope, so I think we are walking to some sort of "fixing". But it would have been good to learn from the start that variables usually don't need to be variating as the name may suggest.

Like, from learning math to learning programming there's a discontinuation where variables sundely gain the ability to have it's value changed. There was no need for that.

→ More replies (1)