The problem with this in the OP is the first ‘if’ checks if the object exists and the second gets a property of said object only if the original object exists.
I’m not saying the OP is good code, but chaining them like this would result in exceptions.
The language is python and it has short circuiting aka in an and condition, if the first block isn’t fulfilled the second one isn’t tested because it’s unnecessary.
There are two types of programmers.
// comment if(condition) { // comment1 if(condition1) { // comment2 if(condition2) { printf("hello, world\\n"); } } }
and
// comment if(!condition) { return; } // comment1 if(!condition1) { return; } // comment2 if(!condition2) { return; } printf("hello, world\\n");
And one is objectively correct.
The problem with this in the OP is the first ‘if’ checks if the object exists and the second gets a property of said object only if the original object exists.
I’m not saying the OP is good code, but chaining them like this would result in exceptions.
The language is python and it has short circuiting aka in an and condition, if the first block isn’t fulfilled the second one isn’t tested because it’s unnecessary.
Same with or and the reverse.
Not in a language with short circuiting.
Could’ve sworn I’ve had this issue before! Maybe not with python
Yeah not all languages do it. I find it rather convenient though
Add the
else
branches to the nested version and log the failed conditions (to make it more obvious).// comment if(x < 10) { // comment1 if(x < 20) { // comment2 if(x < 30) { printf("hello, world\\n"); } } }
“Yeah x might be less than 10 but just in case check if it’s less than 30.”
This is the cursed case when you case the forbidden scroll of the ancients: switch (true) { }
edit: on second thought I’m not sure now I’ll have to think about how fall through cases work
I’m so the latter. The former drives me fucking crazy.
Inefficient
if(!condition) {return;}: If(!condition1){return;}; if(!condition2) {return;};