4 ms·
Try catching a block that does that is actually less code than a long set of conditionals. Granted it's basically saying: if (foo != null) if (bar.bar() !=
by Retric 17y ago
Try catching a block that does that is actually less code than a long set of conditionals. Granted it's basically saying:
if (foo != null) if (bar.bar() != null) if (foo.bar().baz() != null) {
...
worked = true;
}
if (false == worked ) {/*to do*/}
But, staying on the happy path until you can demo something is often useful. I think the problem is how you transition from demo to production worthy code.
- guelo 17y agoThat's not the same since you are executing the bar method multiple times.
- sukuriant 17y agoIf you really want to get picky, return foo.bar().baz().qux() * 2; Could become: Bar bar = foo.bar(); if(bar == null) handleBarNull(); else { Baz baz = bar.baz(); if(baz == null) handleBazNull(); else { //(assuming qux is a value) return baz.qux() * 2; } }
- alttab 17y agoif (foo!=null && (a = foo.bar()) && a != null && ... a = foo.bar() will always evaluate to true as the assignment shouldn't ever fail. Of course, in compiled languages I'm sure there will need to be a declaration of 'a' but if there is a possibility to reuse temp variables then could work. However, if you are coding in C/C++ (and to a similar extent Java, there really is no way around it. This could be an example of why hybrid functional, imperative, and object oriented languages are becoming popular because the programmer can pick the paradigm that most suits the problem.