I love guard clauses like these! Thanks for the article
They're especially helpful when multiple conditions should be checked (aka a "preflight checklist")
Compare:
function doSomething(a,b) { if(a && a.type == "correct" && b && b.type == "correct" && a.timestamp >= b.timestamp) { /* ... */ } }
to the guard version:
function doSomething(a, b) { if (!a) return; if (a.type != "correct") return; if (!b) return; if (b.type != "correct") return; if (a.timestamp < b.timestamp) return; /* ... */ }
The guard version:
(and in a language like Ruby, something I particularly like is if(!a) return guard clauses can be rewritten as return unless a)
if(!a) return
return unless a
Optional Channing can use reduce the lines i believe 🤔
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
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 love guard clauses like these! Thanks for the article
They're especially helpful when multiple conditions should be checked (aka a "preflight checklist")
Compare:
to the guard version:
The guard version:
(and in a language like Ruby, something I particularly like is
if(!a) return
guard clauses can be rewritten asreturn unless a
)Optional Channing can use reduce the lines i believe 🤔