The example you give is a very good demonstration of how the guards are better.
With the guards, you have a very simple and understandable list of conditions and results - very close to how the function will be written by a mathematician.
With if , on the other hand, you have a somewhat complicated (essentially O (n 2 ) reading difficulty) structure of nested expressions with keywords that were selected at irregular intervals.
For simple cases, basically, a toss between if and protection - if can be even more readable in some very simple cases, because it’s easier to write on one line. However, for more complex logic, guards are a much better way to express the same idea.
Chuck
source share