r/java • • Aug 11 '15

Top 10 Useful, Yet Paranoid Java Programming Techniques

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

76 comments sorted by

View all comments

6

u/PaulRivers10 Aug 11 '15 edited Aug 25 '15

Some of these are fine but 6 and 7 make me cringe.

It "solves" a non-existent problem I never run into. I've never run into a problem with someone extending my class when I didn't want them to, or frankly even reassigning my variable (except for the ones I actually want non-changeable but those are static final already).

I does make fixing bugs a gigantic pain in the ass, as well as adding new functionality, as well as just cluttering up trying to read the code.

Now the code starts to read like: It final "solves" a non-existant final problem I final never run final into.

It's messy, without solving anything that I've found to be any sort of regular problem. If someone is messing in my code that I don't want to be they just remove "final"'s they don't like anyways.

5

u/CubsThisYear Aug 11 '15

Adding final is a no-brainer in my opinion. It can easily be done by the IDE, enables some compiler optimizations and gets you closer to single-assignment semantics, which is WAY easier to understand. There just aren't very many good reason to reassign the same variable. Don't worry we've got lots of names, I promise you won't use them all up.

Final methods are even more important. If you have a non-final method that modifies state, you've basically completely blown any chance at encapsulation. Methods should be designed for extension (when you intend for that) and provide specific extension points. This is again a no-brainier if you control all of the code. If you don't (ie you are shipping a library) that just means you need to think a lot harder about your API and provide for extension by composition whenever possible.

1

u/PaulRivers10 Aug 12 '15 edited Aug 12 '15

There just aren't very many good reason to reassign the same variable.

1. for loop
2. while loop
3. Any algorithm at all that calculates or adds anything

I take it you don't actually know how to code? This doesn't work:
for(int i = 0; i < collection.size(); i++) {

"Adding final is a no-brainer" is for ivory tower people who don't write any code. It doesn't solve any actually common problem, all it does it clutter up your code with more unecessary statements that make it harder to read.

2

u/thekab Aug 12 '15

These are examples of cases where you clearly want a mutable variable. Nobody is claiming that never happens. Aside from typical loops however the vast majority of parameters and variables have no reason to be mutable.

1

u/PaulRivers10 Aug 12 '15

The post I was responding to said:

There just aren't very many good reason to reassign the same variable. Don't worry we've got lots of names, I promise you won't use them all up.

Other poster is suggesting whenever you need a new value, create a new variable.

That's not how most coding works.

For the areas where you don't need to reassign, you gain little or nothing by making the variable final because no code was going to try to reassign to it anyways. It's like putting a car cover on a car that's already in a garage every night - it's just a waste against a problem that doesn't happen.

But there are a ton of places where you need to modify the variable value when you write code.

3

u/thekab Aug 12 '15

Other poster is suggesting whenever you need a new value, create a new variable.

I disagree with your interpretation. I believe the comment is saying that very few of your field, variable or parameter declarations need to be re-assigned. In Java this is usually true.

I picked a random service in one of our enterprise applications. The class is 414 lines long. There are 67 field, variable and parameter declarations. One of those is mutable: the "i" in a for loop, counting optimistic lock failures essentially. The other 66 declarations are not mutable and have no reason to be mutable. In other words only 1.5% of my declarations are intended to be re-assigned. If I continue this process across the code base it will still only get up to ~5%.

For the areas where you don't need to reassign, you gain little or nothing by making the variable final because no code was going to try to reassign to it anyways. It's like putting a car cover on a car that's already in a garage every night - it's just a waste against a problem that doesn't happen.

There's a lot of reasons to prefer immutability. Requiring final can help drive the code in that direction. Even if you already prefer immutability simply adding the final keyword can help with performance, readability and thread-safety. Using the keyword also prevents bugs due to accidental assignment.

My question to you is why not use final if the field, parameter or variable isn't re-assigned? You lose literally nothing but maybe a little typing and seeing the word 'final' in declarations.