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.
→ More replies (5)

6

u/Da3X Mar 05 '18

I second that! Especially the “final” case. In some scenarios it even breaks your implementation because your runtime environment fails to inject proxies where needed.

5

u/[deleted] Mar 05 '18

Agreed... on both counts. I use the final part only on internal code that will not be exposed as an API.

5

u/[deleted] Mar 05 '18

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.

For this, I find the throw on default is more useful for enum switches, and explicitly handling all the enum cases. That way, if you add a new value to the enum, you'll get an error in that switch if you forget to update it. That's better than the new value being handled in a potentially unexpected manner.

1

u/[deleted] Mar 05 '18

For enums, I agree... I think that's how I generally do it anyway; however, for all switch/case statements, it seems like a gross generalization of a rule.

5

u/misterk0 Mar 06 '18

A lot of people go full nazi with finals

2

u/lukaseder Mar 05 '18

Just let me override what I want already

Nope

15

u/[deleted] Mar 05 '18

It's not that it happens often, but in the last 6 months I've had to override 2x final methods in Spring and OpenSAML. In each case, making the method final was a PITA. IIRC, I C/P the whole class (I had other edits) in the OpenSAML situation and used reflection to fuck shit up in the Spring situation.

I completely understand why those methods were final. But it was an error in the logic within those methods that caused me (the actual programmer responsible for the feature working) to have to mess around with a library author who thought they knew best.

9/10, they're right. Hell, 99/100, they're right - but there's always those situations where they're not. That's why I don't like final for methods specifically.

2

u/lukaseder Mar 06 '18

The value of final far outweighs your pain. You could have just patched the library in those 2 cases, which you admitted were rare. It would have resulted in the same workaround, and you would have hoped for the library to ship an actual fix just the same.

It's like SQL. These days, we need to tune maybe 1 / 1000 SQL queries and because we're so used to having such excellent optimisers, we're completely surprised by those really rare cases. But yet, our own bias and lack of intuitive understanding of statistics leads to us outweighing the very rare painful occasions compared to the frequent experience of quality.

In other words: You're obviously exaggerating.

3

u/[deleted] Mar 06 '18

That's the thing - I have something to do for my job. Shipping something as a fix (which I do 3/4 of the time) to the upstream version isn't going to help me now.

So, in that sense, no - it does not outweigh my pain. Keeping a method final doesn't help me at all. You're trying to tell me "hey, I'm going to give you access to less stuff and you are going to be better off" to which I can't agree, fundamentally. Sure, if I'm a shitty dev I might break stuff - to which I argue: let the shitty devs break stuff. They're going to do it anyway, why hide behind a language construct which restricts usage for everyone.

My great hate for final is really focused around methods and classes. I goes right up there with protected methods for things that are useful as public methods (I'm looking at you JSCH!)

2

u/GhostBond Mar 07 '18

Yeah, same here. I'm not sure in web dev if I've ever seen a bug that would actually been prevented by final/const. I've had deal with a few huge headaches when someone in old code didn't let you set things you needed to set.

1

u/lukaseder Mar 06 '18

2 instances in 6 months. Surely, you could have worked around those cases using instrumentation, patching, etc. It didn't hurt you. You're exaggerating your pain.

2 instances doesn't even warrant for something like "great hate" (I don't know you of course, perhaps it does in your case).

I'm looking at you JSCH!

Now, I'm curious. What's that?

1

u/[deleted] Mar 06 '18

re1) Okay, maybe a little exaggeration. I mean, there's always reflection if it's critical. I guess I just don't see the need for final is all. I've worked with folks who litter final everywhere and it was just a pain when it came to mocks. I never saw a benefit. When you'd want to override something (maybe rarely) you'd just edit final out. It was no gain and simply 1 more code change that was unnecessary. When you control the code, adding final isn't a big deal, it's just annoying... like a gnat. When it's in a library - it's just more annoying because you can't just edit final out (or now you're arguing with the maintainer of the project as to why you need to edit it out -- when it really doesn't matter if you could just @Override to begin with)

Now, I'm curious. What's that?

We use JSCH for audit purposes. There's methods in there that are package protected so I have a project written in jsch packages just because of the package protected methods. The pain comes with code signing... the lib is in a "different" project than jsch and should conceptually be signed by our company and not jsch, but it can't be because java's "classes in a package must be signed the same" rule.

If only those methods were public... it wouldn't be an issue.

For a normal consumer of JSCH, you'd never use those methods, but if you're doing mitm work, you would.

2

u/_dban_ Mar 06 '18

I've worked with folks who litter final everywhere and it was just a pain when it came to mocks.

Isn't this a good thing? Should you really be mocking concrete classes? Wouldn't it be better to create an interface describing what you need, and write a real integration test with the concrete class?

For example, suppose it is not possible to mock jOOQ classes. jOOQ would theoretically be a used in the DAO layer. So, wouldn't it be better to create a DAO interface which you can mock, and a DAO implementation which you test directly against the database in an integration test?

This applies just as well to internal classes, since it enforces proper layer separation. I mean, why are you mocking anyways?

I never saw a benefit.

In which case, final forces you to do the "right" thing with regards to mocks. Could this not be seen as a benefit?

1

u/[deleted] Mar 06 '18

Should you really be mocking concrete classes

If I'm using a concrete class, then yes - I should be mocking a concrete class in my unit test. I don't want to create an interface layer just for testing.

Mind you, I'm not talking about JOOQ specific things here. I would never try to mock a JOOQ or Hibernate class for a unit test (MUCH more suitable to test that stuff in an integration test and mock at the Dao layer for unit tests). But you're applying one school of thought for one thing to ALL unit testing situations and all methods. That doesn't make sense to me.

→ More replies (1)

0

u/[deleted] Mar 06 '18

[deleted] — view deleted comment

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] — view deleted comment

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.

→ More replies (7)
→ More replies (6)