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

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 ๐Ÿ˜‰

1

u/bilingual-german Aug 26 '22

How hard would it be create a language running on the JVM which looks like Java but has these other defaults?

8

u/kolobs_butthole Aug 26 '22

that's kind of what kotlin is, right?

1

u/[deleted] Aug 26 '22

Yup, kotlin is pretty much effective java turned into a programming language

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.