DEV Community

Discussion on: JS Refactoring Combo: Replace Nested If-Else with Guards

Collapse
 
moay profile image
moay • Edited

The term "guard clause" could be somewhat confusing here, because the typical guard clause is not intended to solve the problem of nested logic with different outcomes but to stop the execution of a function in case some condition is not met (so kind of a bouncer or an execution guard).

What you did here is pretty close to the so called "early return pattern" which looks similar and contains guard clauses, but actually solves a different problem and has a wider scope. To read more about early return: medium.com/swlh/return-early-patte...

I am aware of refactoring.com and Martin Fowler, still wanted to point out that in this specific case, most people will probably name the child "early return" instead of "guard".

Collapse
 
lgrammel profile image
Lars Grammel

Good point! I followed the terminology from Fowler's example, which uses guard clause.