I've just started working with Ruby, and discovered statement modifiers when RubyMine suggested I change this code:
if !VALID_DIRECTIONS.include?(direction)
raise ArgumentError, "Invalid direction"
end
to this:
raise ArgumentError, "Invalid direction" if !VALID_DIRECTIONS.include?(direction)
I like how it makes the code more succinct. However, I can see it potentially misleading at a first glance and imposing a readability issue, because it places the effect before the condition. Then again, maybe that's just because I'm so used to C-style languages.
Has anyone run into trouble as a result of using statement modifiers, or do you feel they have improved your code? Also, does anyone have general guidelines for using the modifiers (i.e., works particularly well for some operations, or not for some others)?