4 ms·
> > `do_thing if other_thing` > Yeah I don't get why you would deliberately make the flow control confusing and backwards like that. I like to use this as fun
by greenpeas 4y ago
> > `do_thing if other_thing`
> Yeah I don't get why you would deliberately make the flow control confusing and backwards like that.
I like to use this as function guards, where the first lines in the body of a function can be:
return xxx if some_edge_condition?
example: https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/activerecord/lib/active_record/relation/merger.rb#L85 https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5b...
- cycomachead 4y agoGuard clauses are great, and when used appropriately clean things up. This is pretty much my only use of the pattern. (Ending boolean-returning methods with a ? is also a great convention in ruby.)
- bobbylarrybobby 4y agoif some_edge_condition: return x
- msbarnett 4y agoI’ve written both professionally and personally found Python makes it harder to see the early out, which makes it less valuable/explicit as a guard. The Ruby pattern is clear up front on what it is rather than being a bag of conditions that you have to get to the end of to discover what they trigger.