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

1

u/zimm3r16 Aug 12 '15

Never been a fan of Java but lots of these seem rather over the top.

  • String literal first? No. Check and log. Provides my info than a silent fail.

  • Don't trust -1. Yes trust it. Have an else catch or 'drop off' where it logs unexpected errors. Stop silently failing.

Disclaimer: I am no friend to Java ever since I had to deal with people using private. (Yes I know internal, may change, i don't care Android, I want access to my Native C View to resize it and use a java GUI easily!).

1

u/ExplosiveNutsack69 Aug 12 '15

I don't see why there's any reason to do anything other than the >= 0 check. There is literally no situation in which that could go wrong, and for people reading the code that don't understand that a null indexOf() will return -1 (God forbid), it will immediately make more sense. Quite honestly, it's easier to read universally, because we don't think like computers do. Ensuring that it's either a) empty or b) not empty and has indices with the >= 0 check is simpler to process. My two cents.

1

u/zimm3r16 Aug 12 '15

Explicit rather than implicit. You truly only expect -1 or a positive number so your code should reflect that.