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
r/java • u/psychologicalX • Mar 05 '18
4
u/istarian Mar 05 '18 edited Mar 05 '18
Not sure what you're getting at there. Most of the time the equals method should be used instead of the '==' operator.
Given the knowledge that Java uses nulls you should:
always document when null can be returned and what it means
know when something could reasonably be null (you or someone else haaid it might be)
use null checks when appropriate.
Frankly if the return is some kind of meta information you may as well wrap it in a class or enum. indexOf is kind of a special case because you want the index of a character, but if the character isn't present then what? Returning a Result object containing the index (possibly -1) and a method/data field indicating that the character wasn't found is an option, but a clunky one at best compared to -1 always meaning the character wasn't found.
P.S.
The moment you start acting like the language should dumb things down with those null conditional operators of yours you may as well insist on the removal of null entirely or use a different programming language.