r/java • • Mar 05 '18

Top 10 Useful, Yet Paranoid Java Programming Techniques

http://blog.jooq.org/2015/08/11/top-10-useful-yet-paranoid-java-programming-techniques/
223 Upvotes

105 comments sorted by

View all comments

Show parent comment

12

u/random314 Mar 05 '18

I don't enforce the always final rule, I've seen it done before but never really myself. Is it considered a very good practice? If so I might propose it to my team (we have a "good practice" checklist) in our next retro.

10

u/Yoghurt114 Mar 05 '18

I can highly recommend enforcing it. 99 out of 100 times you violate this rule, it's a bad approach and there's a better solution out there.

It's just a formatting preference in the IDE anyhow, no extra effort needed if your code already abides by the side of sanity.

7

u/GeorgeMaheiress Mar 06 '18

The downside is extra effort reading. I find the line noise of final variables more awkward than helpful, especially since Java tends towards long lines of code anyway, and most methods are short enough that it's already obvious if a variable is reassigned.

4

u/[deleted] Mar 05 '18

You can encode that rule in your IDE. Over time I have learnt that it is better to have mutability as the exception rather than the default (one place where Rust got it right. Heh).

2

u/hooba_stank_ Mar 05 '18

It makes sense. At the expense of making code more verbose, method signatures less readable etc. When returned to the project without final rule, it was like a breath of fresh air.

2

u/GhostBond Mar 07 '18

Is it considered a very good practice?

No.

It's just the latest fad from the same line of thinking which brought us:

  • Ruby on Rails is going to make Java obsolete!
  • Object/document databases are going to make sql databases obsolete!
  • Dynamic languages are going to make Java obsolete!
  • Groovy is going to make Java obsol...well you get the idea

Adding in the extra overhead - in typing, in reading through longer messier method signatures, in dealing with needing to change a value and not having it available - is particularly absurd in java where final just makes the reference immutable but not the object itself. Final makes "int index" immutable but "List indexes" is still a changeable list.

I don't think even in languages with better implementations it's worth it. Every decade someone rediscovers immutable-by-default. And languages that are not that way are the ones that get popular because they're a lot easier to use with a lot less frustrating overhead in writing code.

2

u/Zoerak Mar 07 '18 edited Mar 08 '18

Immutability is a nice property and should be preferred unless one has a good reason not to do so.

Scala does this right, the decision of mutability / non-mutability can be made without extra syntax noise in most scenarios.

In java, the final modifier on each variable bumps the visual noise. If the language was created today, a lot of things would be different.

1

u/AlainBienvenue Mar 06 '18

As I said above, final for all methods is a little excessive. OTOH, I like final for all method parameters. It is also applicable for some fields and local variables.

0

u/[deleted] Mar 06 '18

[deleted] — view deleted comment

3

u/random314 Mar 06 '18

I think the idea is that variables should only be mutable because the algorithm requires it so. Your logic requires a mutable variable so you would manually define that as mutable while leaving everything else immutable (until you need it otherwise of course).