Hacker News new | ask | show | jobs
by lucidone 1279 days ago
I'm not a very experienced Ruby developer but RuboCop does a pretty good job at guiding and teaching you to use `unless` as a guard in method definitions, e.g.:

  def my_method(required_thing)
    raise 'required_thing is required' unless required_thing.present?
    
    ...
  end
In practice, this hasn't been as issue as a consequence of the tooling and ecosystem around Ruby.
1 comments

and why

    def my_method(required_thing)
      raise 'required_thing is required' if !required_thing.present?
is a problem ?
I don't think it's a problem necessarily. "unless" is more obvious to me since since it visually stands out more than "if !required_thing" while I'm casually scanning the code. So, in practice, it acts as an indication from the author of the code to the reader that "this line of code is a guard since we're using an unless".