coding style - Is there a simpler way to derefence nullable references in Java? -
consider following code snippet:
if (foo != null && foo.bar != null && foo.bar.boo != null && foo.bar.boo.far != null) { dosomething (foo.bar.boo.far); }
my question simple: there more simple\shorter way ?
in detail: there more simple way validate each part of chain, i'd imagine similar ..
if (validate("foo.bar.boo.far")) { dosomething (foo.bar.boo.far); }
maybe ?
if (fooutils.isfarnotempty(foo)){ dosomething (foo.bar.boo.far); }
and in fooutils
:
boolean isfarnotempty (foo foo){ return foo != null && foo.bar != null && foo.bar.boo != null && foo.bar.boo.far != null; }
Comments
Post a Comment