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

49

u/[deleted] Mar 05 '18
  • 6 & 7 & probably 8 - I greatly dislike the "make everything final" school of thinking. You know what? You don't trust me not to incorrectly override your method, but I grow tired of not being able to overload methods in code (that I don't control, aka, a library) because of a stupid bug. I don't want to copy/paste your whole class just to fix that stupid assumption/error/whatever you made in your method. Just let me override what I want already - I'm the developer. Let me make my own mistakes. . Unless you're making things final because there's some Jr. developer maintaining your code... then, by all means, protect yourself (or your product). But, if you're a library maintainer. Please, just stop. Final is fine for things where it absolutely matters. And if you use insist on using final, make sure there's a way to override whatever it is the final method is doing. Like a configuration bean or something along those lines.

  • 9 - I disagree on always throwing an exception. I'd change this to "handle the default" instead of "throw on default". There's plenty of times to have default: actually be the default result if there's no other match.

17

u/hemenex Mar 05 '18

Totally agree with final methods. This won't make your code fool-proof; fools will just copy-paste, which is hundred times worse.

3

u/lukaseder Mar 05 '18

Why is it worse? At least it's more honest than subtype polymorphism on concrete types.

9

u/hemenex Mar 05 '18

Because then, most importantly, you'll loose all references to original implementation. What if you want to change the original and all the places where it's supposed to be used? You are screwed and all you've got is "Find everywhere" and regex.

From my experience on larger projects, this leads to terrible and unmanageable code. Especially with multiple programmers and a lot of bug-fixing. Typical scenario:

  • one programmer implements some things with copy-pasting

  • some things he did does not work, another programmer fixes it in one place

  • same thing still does not work in some other place, another programmer fixes it there, but differently

  • somebody decides the behavior needs to be somewhat different. You cry because you need to do the same change everywhere but slightly differently. Or spend a few days rewriting the whole thing. This totally did not happen to me.

-2

u/lukaseder Mar 05 '18

And you sincerely blame the presence of final for all this mess? :-) Have you heard of Conway's Law?

4

u/hemenex Mar 05 '18

final would just make much worse.

1

u/lukaseder Mar 05 '18

Any extra 5 letters added to that code base would make it much worse, so I'm not sure if I see the point of this argument.

2

u/hemenex Mar 05 '18

I just needed place to rant. :)

2

u/lukaseder Mar 05 '18

I knew it ;-)