(Draft) Else is a Code Smell
A single exit point for a function can lead to increased complexity.
1. Code with Traditional If/Else
This function checks the system if armed, or reschedules a check at a later time.
This code is nearly fifty percent boilerplate code.
2. Code with If/Else and Early Return
The logical flow of this is identical to Example 1.
You must use a certain amount of mental math to invert the condition to determine when the alarm is recheduled rather than checked.
3. Code Without Else
The logical flow of this is identical to Example 1 and 2. However, there is now an explicit return statement, and we’ve reducded the function by two lines.
While we have greatly reduced the amount of boilerplate from example 1, we’ve ended up with a function where the primary goal of the function is nested within an
if. In refactoring with guard clauses, we want to be able to skip reading the guards and see the primary goal of the method. In this case, the primary goal of the function is to run the
systemCheck, and so we need to clarify the function a bit.
4. Code Without Else with an Inverted Control
The logical flow of this is the opposite of example 3 in that we’re now using a guard statement. We have moved the primary goal of the function into the main body of the function.
By moving our primary goal of the method to the main body of the function, we’ve greatly simplified the method by both reducing the cyclomatic complexity, and by making it very clear under what conditions we don’t do our main goal.
Actual Code Example
While this is a fictitious example, this code is somewhat believable. With a little reasoning, you can examine this code and see how it could have grown into being in the current state that it’s in.
In the following example a post is editable if:
- The user is an editor or an administrator
- The user is the author and the post was written in the last thirty days.
Refactored Code, Step One.
(Draft, discussion to come.)