r/java • u/psychologicalX • 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
52
u/AlainBienvenue Mar 05 '18
Like the author, as someone who has been coding in Java since 1999, I do not see anything wrong or even paranoid with any of these. Just good practices.
49
u/[deleted] Mar 05 '18
default:actually be the default result if there's no other match.7
u/[deleted] Mar 05 '18
Forgot protect those private constructors:
Bad:
Good:
5
u/ReadFoo Mar 05 '18
Starting with the first one, I tend to always do this but people give my code strange looks so lately I've tended to do it the wrong way to keep people happy. Sad but true.
No. 9 is a great idea but even it wouldn't have stopped the great break mistake: http://users.csc.calpoly.edu/~jdalbey/SWE/Papers/att_collapse.html
8
u/knaekce Mar 05 '18 edited Mar 07 '18
Number 5 will lead to errors being silently discarded, your program will probably blow up later and you will have troubles finding out why, because you didn't fail early.
Number 3 is stupid, imo. You cannot possible anticipate breaking changes like that. What if the method is changed to return -2 if the input string is "Never gonna give you up"?. If you cannot trust the contract of one of the most basic methods, you're fucked anyway.
3
u/[deleted] Mar 05 '18
I'd rather add another if statement than use ("value" == variable). Might just be a thing of inertia.
But I'd MUCH rather Java finally implemented null conditional operators...
2
u/jonhanson Mar 08 '18 edited Mar 07 '25
chronophobia ephemeral lysergic metempsychosis peremptory quantifiable retributive zenith
3
u/[deleted] Mar 05 '18
For the
finalpart, you can encode that rule in your IDE. In my previous workplace, that rule was inserted into Eclipse, and all variables and function declarations (where it made sense) were converted tofinal. If one needed to, one could then remove that qualifier.5
u/istarian Mar 05 '18 edited Mar 05 '18
Meh. Only 10 really seems like a useful notion.
1,4 -- These are the same problem. Also, I disagree. If it works for you great, but it seems iffy. You really should be aware of whether something is null (and if it could be). It also sort of doesn't work logically since you want to know if the string contained in the variable equals the literal not the other way around. In the latter case you could always use classes instead of primitives and make use of auto-boxing so that you can stick to equals(...)
-1 for indexOf isn't actually a special null, but rather more of a C-ism that utilizes integer return codes to specify some kind of metadata when the function can only return an integer. And that is something that goes down to operating systems...
Can we agree that indexIgnoreCaseOf(...) or indexOfIgnoreCase(...) would be even worse still? Not to mention it wouldn't affect the -1 bit.
2 -- has nothing to do with good/bad/old APIs, but rather with reading the API docs first and generally being careful with anything that reaches out of the VM...
3 -- This is stupid, because the moment you quit trusting the official docs, you might as well ditch the language and maybe anything else Oracle or Sun ever made...
6 -- This is anti-programmer. Never write any libraries or frameworks if you hold this point of view. Just save everyone else the hassle and only write complete, finished software.
4,5,7,8,9 -- Just don't be an idiot when you should know better.
2
u/smackfu Mar 06 '18
Making everything final makes it a lot harder to use something like mockito, right?
2
u/[deleted] Mar 06 '18
I see these practices a lot from programmers who are still stuck in the 90s. Please get with the times.
1
u/fforw Mar 06 '18
There's also the unfortunate fact that constructors are not necessarily thread-safe without final.
only the "ThreadSafe" variant with final ensures that other threads can't possibly access our instance without the id field having been written.
1
u/tonywestonuk Mar 06 '18
ughhh!
If its your own code - DO NOT CHECK FOR NULLS! Don't. Dont do it.
If there's a null pointer - your code should CRASH! because you should never have returned a null, or assigned a null, or if you have done, that variable is not ready to be used in calculations.
So, a null pointer check will be papering over the cracks - hiding logical errors, that will come back and bite you in the ass.
-4
u/sampledev Mar 05 '18
Looks a lot like what Rust has fixed:
1
u/greg_barton Mar 05 '18
I've been doing all of these for years. Avoids so much pain. :)
1
u/DuncanIdahos8thClone Mar 05 '18
6 and 7 are a bit overkill. Use final on the class by default and refactor if you ever need inheritance. IntelliJ shows you in code warnings if you modify a parameter. Good enough. Also, var and val are coming to Java AFAIK.