3 ms·
A proposed solution from the article: > Use "if (condition()) { ifBlock(); } if (!condition()) { elseBlock(); }" -- assuming that ifBlock() can't possibly chan
by thatguymike 2y ago
A proposed solution from the article:
> Use "if (condition()) { ifBlock(); } if (!condition()) { elseBlock(); }" -- assuming that ifBlock() can't possibly change the result of condition().
What?? That seems terrible to me - just asking for `condition` to be updated to be modifiable by `ifBlock`, or for some completely undebuggable race condition to occur.
My team has an ongoing style war about guard clauses:
if foo:
bar()
baz()
vs
if foo:
bar()
else:
baz()
Consensus seems to favor the first option but I prefer the second because it helps me keep in mind the conditions that lead me to `baz()`. Otherwise I have to scan up the whole function to figure out the restriction `!foo`.
- fingerlocks 2y agoThat’s not a style issue, those are two different things. If foo is true, bar() and baz() are both executed in your first scenario. Only bar() is executed in the second.
- thatguymike 2y agoOh whoops you're right, Friday brain. The first conditional should have a `return` in it.