I prefer the second syntax because it acts like a "guard" against further execution, so to speak. The first syntax just encourages more nesting and levels of indentations. To me, it is more readable to have a "guard" condition than to have if-else blocks.
I'm quite surprised actually. Given the imperative nature of if statements, I wouldn't expect Haskell (of all languages) to be the origin of that term.
I call it the Short-circuit or (Fast-fail)[en.m.wikipedia.org/wiki/Fail-fast]. It will help you just worry about less things if it's not followed by else
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I prefer the second syntax because it acts like a "guard" against further execution, so to speak. The first syntax just encourages more nesting and levels of indentations. To me, it is more readable to have a "guard" condition than to have
if-else
blocks.I hadn't encountered the term "guard" for this but I like it a lot 👌
I can't take the credit for it. 😉
Article No Longer Available
I have the same train of thought with it! A senior developer at a previous job introduced it to me
The first time I heard the term "guard" was in haskell. Is it any related?
I'm quite surprised actually. Given the imperative nature of
if
statements, I wouldn't expect Haskell (of all languages) to be the origin of that term.Haskell (among other functional languages, I guess) has guards for pattern matching
I call it the Short-circuit or (Fast-fail)[en.m.wikipedia.org/wiki/Fail-fast]. It will help you just worry about less things if it's not followed by
else