r/programming • • 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/
28 Upvotes

35 comments sorted by

View all comments

12

u/[deleted] Aug 11 '15 edited Aug 11 '15

I'm mostly code in C# and I'm no expert in how different C# is from Java, but it seems like what this article recommends would be bad practice in my view.

It's not like in C++ where reading outside of an array could cause undefined behavior and vulnerabilities. I would take a ArgumentException any day over some function silently ignoring the fact that I provide it with weird or even invalid input.

I sometimes see people adding null-checks which makes no sense. They may be calling a method which can never return null, yet at the call-site they perform null-check. Adding null-checks were not necessary, or checking for >= for the potential -2 which will never happen just leads to code which is harder to understand.

Functions should have a clear contract defining what input they expect and what data they return and if that can not be met, then an exception should be thrown. And that exception is a good thing. For example, if your function expects a non-null string it should throw an exception if null is passed.

In my view, what the article writer isn't useful or paranoid code, it's guidance which leads to more buggy code in the long term.

1

u/hu6Bi5To Aug 12 '15 edited Aug 12 '15

I'm mostly code in C# and I'm no expert in how different C# is from Java, but it seems like what this article recommends would be bad practice in my view.

Yeah, a lot of these are bullshit. But some of them are bullshit that have been institutionalised in the warnings of IntelliJ etc... They don't stand to reason.

It’s just never a bad idea to prevent the occasional NullPointerException by putting the String literal on the left side of an equals() comparison as such

Terrible advice. Either the variable is legitimately nullable, in which case you're just hiding that fact, the more verbose if (v != null && v.startsWith("blah")) would show the intention clearer; or, the variable is not null, and there's no need to make the change.

"But what if it is null, due to a bug elsewhere?" Well, in that case, I want it to fail. Silently ignoring bugs and hoping for the best just makes debugging harder.

Don’t trust the early JDK APIs

Then goes on to list the JavaDoc that accurately explains the behaviour. A better lesson would be "read the instructions".

That particular example would have probably been better served by throwing an exception if called on a non-directory. Perhaps.

Don’t trust that “-1”

Paranoia combined with voodoo.

Avoid the accidental assignment

This one is an old classic, but is one of the easier ones for IntelliJ (etc.) to warn about.

Whenever you have a collection, array, etc., make sure it’s present AND not empty

Again this is context specific, in exactly the same way the first one was. If it cannot be legitimately null, then don't just silently carry on if it is. Only check for null if it could, legitimately, be null; in practice you should need to do this very rarely.

You can tell me all you want about your open/closed principles, that’s all bollocks. I don’t trust you (to correctly extend my classes) and I don’t trust myself (to not accidentally extend my classes).

Well maybe. But these things should be used for semantic purposes mostly. Or you could just mark the class as final, which achieves the same thing.

Don’t trust generics when overloading

I know what they're saying, but I don't think the example illustrates the point at all well.

Always throw on switch default

Surprised they didn't add "except if it's null, in which case silently ignore it and carry on like points 1 and 5".

Switch with curly braces

Meh.

In summary: these aren't even paranoid practices, these are purely superstitious practices that can cause as many problems as they solve.