4 ms·
In Ruby these are idiomatic enough to have a name and are built into the linter, so it'll yell at you if it sees that you're not using guard clauses. As a furt
by vinceguidry 9y ago
In Ruby these are idiomatic enough to have a name and are built into the linter, so it'll yell at you if it sees that you're not using guard clauses.
As a further refinement, I will often take tricky conditional logic and put them into a method that consists of nothing but guard clauses and a return true or false at the bottom of the method. In Ruby you can use ? and ! at the end of method names, so I'll give the method an intention-revealing question-mark name. "Order.used_credit_card?"
I use this in Javascript whenever I can too, though of course it's not as semantically nice as Ruby.
- amingilani 9y agoLove this in Ruby. Rubocop's Guard Clause cop[1] always flag this for me. def foo fail unless bar return baz if foobar foobaz end [1]: http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/GuardClause http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/Guard...
- brazzledazzle 9y agoI would love Ruby-style guard clauses in JavaScript. Sometimes I wish I’d never written anything in Ruby just so I’d stop thinking about how great it would be if other languages shared some of its features.
- vinceguidry 9y agoI moved to Hyperloop on my personal projects. Give it a shot, I think you might like it. http://ruby-hyperloop.org/ http://ruby-hyperloop.org/ The guys in Gitter chat are quite helpful if you get stuck.
- Raphmedia 9y agoNot exactly the same but my JavaScript linter throws errors when I use "else" when it could be avoided.
- sorenjan 9y agoThat's called a predicate and is used in Scheme as well.
- vinceguidry 9y agoGuard clauses use predicates but not all predicates are guard clauses.