Which is a better practice? (I'm coding in .Net if that makes a difference)
IF condition = true THEN
...true action--even if rare...
ELSE
...action
END IF
or
IF condition = [most common condition] THEN
...most common action....
ELSE
...least common action
END IF
According to Steve McConnell, author of Code Complete, you should
"Put the case you normally expect to process first. This is in line with the general principle of putting code that results from a decision as close as possible to the decision...[putting the normal case after the if] puts the focus on reading the main flow rather than on wading through the exceptional cases, so the code is easier to read overall."
Code Complete, 2nd Edition, pages 356-357.
Go with the most readable version for your specific case, and by the way, don't compare a boolean expression to true and false. Use condition
and Not condition
(!condition
in C#.)
if (condition == true) // bad
if (condition) // better
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With