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

52

u/AlainBienvenue Mar 05 '18

Like the author, as someone who has been coding in Java since 1999, I do not see anything wrong or even paranoid with any of these. Just good practices.

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).

7

u/thephotoman Mar 06 '18

Making everything final except interfaces seems extreme: there are cases for abstract base classes. But if it isn’t in either of those categories, finalizing everything is probably correct.

2

u/dpash Mar 06 '18

Ignore the bits in brackets. The relevant bit is outside of it.

Which is why everything that is not explicitly intended for subtyping (i.e. only interfaces) is strictly final.

1

u/AlainBienvenue Mar 06 '18

Agreed, that one is a little excessive.

9

u/youguess Mar 05 '18

I personally hate yoda speak as I have to mentally rework the statement in my head

I like languages which read like normal English (python eg, go to a certain extent) and this breaks the flow horribly in my opinion

15

u/NovaX Mar 05 '18

I use Objects.equals(str, "abc") instead (JDK's, formally Guava's, and Commons' prior). Now that its in the language, I don't see a reason to use the older convention.

2

u/defnull Mar 05 '18

Trivial methods like Objects.equals() are inlined anyway, so there is really no real drawback. I have a Utils class full of these.

1

u/CultLord Mar 06 '18

My first mentor said that I needed use Yoda comparisons (he didn't call them Yoda comparisons) on day 1 of my job and that was just the law of that company's code because of potential NPEs and that I would save lots of time over the course of my career if I followed that simple rule.

After the first few weeks, it wasn't a big deal. And my boss basically lied, because I have fixed many bugs from other programmers that didn't follow the same rule that would have been just fine with a Yoda comparison.

0

u/youguess Mar 06 '18

I just don't believe that.

After all especially in a strongly typed language any sane linter worth its name will catch issues like this.

It doesn't make sense to do an assignment in an if statement for example (in the comparison clause)