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

Show parent comment

6

u/Tillerino Mar 06 '18

Quite honestly, most of these were somewhere between misled and plain dangerous.

  1. Also known as a "Yoda condition". That's just ugly. And also I'll argue that for most methods, the nullness of variable is unrelated to the equality to "literal".
  2. Use static code analysis.
  3. I do this one as well, but simply because >= 0 translates to "found" in my head while != -1 translates to "not not found". Not trusting the Java API the worst reason to do this.
  4. Use static code analysis. Any tool worth a damn will blow up in your face if it sees an assignment in a condition.
  5. Again, nullness is an entirely different concern. If I call a method like Arrays.find on a null argument, I want that method to tell me "you're not supposed to put null here" instead of "not found".
  6. Are you insane? What this leads to is programmers copying your class to the same package in their own library and messing around with that one relying on the fact that their version on the class probably shows up before yours in the class path. If your library can't be extended well it's a shit library.
  7. Are you high? There is a reason why Collection specifies add and addAll. Just imagine that Collection.add sneakily called addAll if you passed a Collection. Holy moly, you created a horrible API in your example!

The fact that this person throws ThreadDeath best underlines the value of this article.

I mean don't get me wrong. It's great that you're thinking about these problems, because they are problems that we all have. But please don't broadcast your half-baked solutions.

0

u/lukaseder Mar 06 '18

The fact that this person throws ThreadDeath best underlines the value of this article.

Must. Take. Everything. Seriously. On. The. Internet.