Not only nested ifs. It’s not even correct (doesn’t check for activities existing). And it’s not even pythonic (ask for forgiveness, not for permission). Just access the thing, catch the exception and be done with it.
Comment on The Legends is among us
underscores@lemmy.zip 9 months ago
nested lonely ifs?
someone execute this man at once
Tja@programming.dev 9 months ago
REDACTED@infosec.pub 9 months ago
Correct me if I’m wrong, but the script works faster with nested IFs since if it fails at the first one, it won’t bother reading next ones, unless you kill/return from function.
All in all, this feels like readibility argument, not correctness or efficienty argument
Jankatarch@lemmy.world 9 months ago
There are two types of programmers.
// comment1 if(condition1) { // comment2 if(condition2) { // comment3 if(condition3) { printf("hello, world\n"); } } }
and
// comment1 if(!condition!) { return; } // comment2 if(!condition2) { return; } // comment3 if(!condition3) { return; } printf("hello, world\n");
veganpizza69@lemmy.world 9 months ago
Add the
elsebranches to the nested version and log the failed conditions (to make it more obvious).interdimensionalmeme@lemmy.ml 9 months ago
if(!condition) { return;}: If(!condition1) {return;}; if(!condition2) {return;};undefined@lemmy.hogru.ch 9 months ago
I’m so the latter. The former drives me fucking crazy.
ICastFist@programming.dev 9 months ago
// comment if(x < 10) { // comment1 if(x < 20) { // comment2 if(x < 30) { printf("hello, world\\n"); } } }
Jankatarch@lemmy.world 9 months ago
“Yeah x might be less than 10 but just in case check if it’s less than 30.”
underscores@lemmy.zip 9 months ago
This is the cursed case when you case the forbidden scroll of the ancients: switch (true) { }
rothaine@lemm.ee 9 months ago
if (condition && condition1 && condition2)kernelle@0d.gs 9 months ago
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.
fushuan@lemmy.blahaj.zone 9 months ago
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.
rothaine@lemm.ee 9 months ago
Not in a language with short circuiting.
CannedYeet@lemmy.world 9 months ago
I worked with chatgpt to since I’m not a python dev, and this is what I came up with