Code Sense (basic): A good IF is a balanced IF
Terminology
if condition_a:
# THEN branch
else:
# ELSE branch
Good Code should look like
Typical IF statement:
if condition_a:
# Handle the case where Condition A is met
else:
# Handle the case where Condition A is not met
Early exit is a special form of balanced IF.
if error_condition:
return
# normal condition logic
Ugly Code looks like
if condition_a:
# Handle the case where Condition A is met
# Possible bug: did not handle the case where Condition A is not met
The logic behind a balanced IF statement
Looking at Software Engineering Axiom 2: “Software is correct if and only if it behaves correctly in all permitted input, environment and dependency responses.”
The two branches of an IF statement represents two situations. The THEN branch should produce the desired behavior where the condition is met. The ELSE branch should produce the desired behavior where the condition is NOT met.
Therefore only if you have logic handling both branches in an IF statement, your code could even have a chance of producing correct behavior in all possible situations, and therefore, have a chance of being correct.