2 ms·
I generally find that if statements with multiple tests are a frequent source of bugs; because it's easy for someone to add another term to one test without rea
by overgard 11y ago
I generally find that if statements with multiple tests are a frequent source of bugs; because it's easy for someone to add another term to one test without realizing they need to update the other. (Especially if the function is long. Yeah you should write short functions, but long functions happen.)
Your second example is much harder to reason about if someone comes in and adds:
if ( (a && b) || c)
So then what happens with the second clause? (Or what even should happen with the second clause?)
On a meta note, any time I see multiple tests in an if statement, I try to pull it into local booleans that describe the intent instead. At the very least then if you add more conditions to the if statement, you have to update the names of the boolean for it to make sense, and so the code stays implicitly commented.