Hacker News new | ask | show | jobs
by devrob 901 days ago
Side tangent: I love Ruby, but strongly dislike and discourage `unless` in the code base. It still trips me up having written Ruby 5 years at this stage. Maybe its just me but I still pause every time for myself when looking at `unless` with a multi conditional. Example:

def do_the_thing

  return do_not_do_it unless first_conditional || second_conditional

  okay_actually_do_it
end

Glad that Rubocop is shipping with Rails though!

4 comments

Maybe a decade of doing ruby has rotted my brain, but I love `unless`, and use it often.

Yes, every `unless` could be rewritten with an `if` instead, certainly. But I like the implication of weight these keywords carry in English language. `unless` implies it's a minor condition, that the majority of runs should fall on the opposite side of the conditional, where as `if` does not -- both weights of the condition feel potentially equivilant-ish.

I've known a handful of people who can handle conditionals inside an unless, and dozens who can't. I'm in the latter camp. Banning conditionals in an unless is a very reasonable rule to follow
Same. I encourage the use of unless iff it reads like english and I’ve found that to be a pretty good rule of thumb for the majority of my team
`unless` is great, but best used sparingly. Only use it if the conditional is single, and simple. Never use it with `&&` or `||`, or `!`.

`unless admin?` is fine

`unless !admin?` is annoying `unless user.admin? && account.support?` leads to madness