In most cases, returning early reduces the complexity and makes the code more readable.
It’s also one of the techniques applied in Spartan programming:
Minimal use of Control
- Minimizing the use of conditionals by using specialized
constructs such ternarization,
inheritance, and classes such as Class
Defaults, Class Once and Class
Separator- Simplifying conditionals with early
return
.- Minimizing the use of looping constructs, by using action applicator
classes such as Class Separate and
Class FileSystemVisitor.- Simplifying logic of iteration with early exits (via
return
,
continue
andbreak
statements).
In your example, I would choose option 2, as it makes the code more readable. I use the same technique when checking function parameters.