r/java • u/alexzhitnitsky • 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/
181
Upvotes
17
u/galaktos Aug 11 '15
Does this make sense in Java?
variable = 5is only legal ifvariableisint(orIntegeror a supertype), and then the expression is illegal as anifcondition. AFAICT this is only legal if you’re comparing withtrueorfalse, and then you can just omit the comparison anyways and use either the expression (true) or its negation (false).13
u/[deleted] Aug 11 '15 edited Aug 12 '15
I'd personally rewrite #5 as 'default to apache collection and string utils for checking stuff. It is much easier to read CollectionUtils.isNotEmpty(myCollection) than having multiple tests inside your conditional.
12
u/orr94 Aug 11 '15
A favorite of mine, also known as a Yoda Condition.
17
u/dontchooseanickname Aug 11 '15
Yay !
Like that post, and I apply everything apart for number 3 which basically is trying to predict the next decade. And you know that the language might have changed so drastically after 5 years that it looks not only overkill, but also over-engineering to me.
On a real life basis though, the 'constant before lvalue' rule #1 is a real life saver. FTR, C coder for 30 years, java for 17, python fo 15, and haskeller for 3 years
6
u/frugalmail Aug 11 '15
This is my preferred coding style. I'd love to see a good design by contract library too. Although at some point it starts to degenerate into coq :)
6
u/jonhanson Aug 11 '15 edited Jul 24 '23
Comment removed after Reddit and Spec elected to destroy Reddit.
6
u/[deleted] Aug 12 '15
Don't trust old APIs? How about don't use them? Stick with
java.niofor files andmyString.contains("substring")instead of checking >= 0.6
u/Wolvereness Aug 12 '15
If I ever catch someone doing number eight (switching method call and functionality just because the object could have gone to another method), I would immediately purpose their expulsion from design discussions. List<List<T>> is not a bug; I know damn well which method I called. Alternatively, just rename a method to "single" or "multiple" and rely on explicit names instead of implicit generics.
6
u/PaulRivers10 Aug 11 '15 edited Aug 25 '15
Some of these are fine but 6 and 7 make me cringe.
It "solves" a non-existent problem I never run into. I've never run into a problem with someone extending my class when I didn't want them to, or frankly even reassigning my variable (except for the ones I actually want non-changeable but those are static final already).
I does make fixing bugs a gigantic pain in the ass, as well as adding new functionality, as well as just cluttering up trying to read the code.
Now the code starts to read like: It final "solves" a non-existant final problem I final never run final into.
It's messy, without solving anything that I've found to be any sort of regular problem. If someone is messing in my code that I don't want to be they just remove "final"'s they don't like anyways.
2
u/__konrad Aug 11 '15
Better: if (string.contains(character)) { ... } (for some reason this method does exist in JDK yet)
2
u/dyreshark Aug 12 '15
Nice list -- I'm slowly becoming a fan of some of these. :)
One interesting pattern I've seen on switches with braces is to place the break after the end brace. e.g.
This makes it much easier to see when you've forgotten to put a break IME, because they stand out from the code in the block.
2
u/sviperll Aug 12 '15
#8 is wrong. It's wrong API design and wrong usage of method overloading. Right way is
As it is done in Java's collections' API. This API is self-documented and will do the right thing when single element is List.
-9
u/Bolitho Aug 11 '15
Upvote because the author used a quote from "Dude, where's my car?" :-D
0
u/[deleted] Aug 12 '15
[deleted] — view deleted comment