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.

0

u/[deleted] Mar 05 '18

grow tired of not being able to overload methods in code (that I don't control, aka, a library) because of a stupid bug.

final is especially useful for you then, if it "forces" you to actually fix the bug at the right place, i.e. upstream.

But, if you're a library maintainer. Please, just stop.

No, the moment a class is not final, this fact becomes part of the public API. As a library maintainer you want to keep your public API as small as possible. A small public API means more possibilities to change code, less possibilitie to break client's code and you can keep your library longer backward compatible. As a user you do like stable libraries, don't you?

0

u/[deleted] Mar 06 '18

[deleted]

2

u/[deleted] Mar 06 '18

and not 2 weeks later when bugfix is released by the maintainer.

2 weeks would be heaven in most cases. My experience is waiting many months for a maintainer to get around to merging my fix in and releasing a GA version of "whatever lib".

but he's happy because now he see a chance to rewrite everything in ReactJS that he learned over the last weekend.

I lol'd...

1

u/lukaseder Mar 06 '18

Then you quit and tell the junior developer "Good Luck", but he's happy because now he see a chance to rewrite everything in ReactJS that he learned over the last weekend.

At least, there's not final in JavaScript. Au contraire, there's prototypes. We can now even override String.substring. FREEDOM!

0

u/[deleted] Mar 06 '18

Ok, so you githubfork the library and apply only that particular fix. Ah crap, it uses a Gradle

If it is about one particular broken class, you can provide that fixed class in your class path as well. Also make a PR upstream, create a ticket to remove the work-around when upstream is released. Upstream will appreciate your contribution, as will many other library users. That's how OSS works. Sorry, I'm not convinced by your argument against final.

2

u/Bobby_Bonsaimind Mar 06 '18

final is especially useful for you then, if it "forces" you to actually fix the bug at the right place, i.e. upstream.

Assuming that upstream cares about your needs/fixes and actually wants them, or the process of getting them upstream is so tedious that you might as well not do it. Or your use-case does not warrant a change upstream but still all you want to do is make that function return something different for your application.

As a user you do like stable libraries, don't you?

What I also like is libraries that I can actually use...and build upon. And final is the very opposite of that.

If something is designed to be an immutable container, always, then yes, by all means, make it final. But if you make getters/setters/classes final just because you can, I'm going to hate you at some point.