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 06 '18

[deleted]

2

u/[deleted] Mar 06 '18

That's because there is no "correct" way to override my method, unless I explicitly designed it to be overriden

That's exactly the presumption I'm talking about. If you write a method in your class called .equalsIgnoreCase and the implementation is incorrect (NPE, for example). I'd rather just extend your otherwise good class and fix the single broken method. If your method is final I can't do that. That's obviously a hypothetical that wouldn't happen, but that's the type of thing I'm talking about here. Why do you care if I @Override your library method?

0

u/[deleted] Mar 06 '18

[deleted]

3

u/[deleted] Mar 06 '18

You ignore the fact that I need the "fix" in this case right now.

I'm the developer of my software. I'm responsible for how it works. That includes all the little bugs that sneak in because of someone else's code. I'm not saying we should all subscribe to the 'not invented here' line of thinking, but ultimately a bug in Apache StringUtils is a bug in RockMeetHardPlaces_APP. I am responsible for fixing it when I ship. Not when Apache decides to release the "fixed" version.

If I override a method and break it, that's MY fault. Let me support my own software please.

0

u/[deleted] Mar 07 '18

[deleted]

1

u/[deleted] Mar 07 '18

I'm the one getting yelled at when your "fix" blows up

Why would my fix even get near your stuff? I feel like you're arguing against something that I am not.

I take backwards compatibility very seriously

As do I. I am not sure how this applies to the conversation at hand.

0

u/[deleted] Mar 07 '18

[deleted]

1

u/[deleted] Mar 07 '18

Back up man. I'm talking about me using a library or some other externalized piece of code that I don't control (aka, not in my organization). If I can fix the actual bug, I'll fix the damned thing and not @Override anything.

The issue I have, specifically with final methods and classes is twofold. 1) testing with mocks -- they're handy and final makes it more difficult (2) if I need to @Override something (which I admit happens rarely, but it does happen) I'm forced to use hacky (aka reflection or C/P) to fix the issue when I could have easily just extended the external library and made it my own via a single @Override method. As I mentioned above -- I've had to do this twice in the last 6 months. So yea, it's uncommon, but it's not exceptionally rare.

How is doing this going to affect anything of "yours" blowing up? Answer: it's not. If I fuck something up, MY software blows up. It's MY bug. Not yours.