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/CubsThisYear Aug 12 '15

Of course the for/while loop is a case, though I would argue that it's a special case because the reassignment is somewhat implicit and it has a well defined (usually small) scope. In any event, I didn't say there were no cases, just that they weren't common.

As for cluttering up the code, I don't see being explicit as clutter. Some people argue that explicit types are clutter - generally those people don't use Java. The point of final is that you can look at a variable and know that wherever it is used, you don't have to think about what the value might be. If your IDE can do all the work for you, why not leave some signposts to guide the next guy reading your code?