Among the things that people new (and old!) to Ruby find delightful are the little things it does to make the language feel intuitive. Case in point: unless.

The unless keyword is just if in reverse. It’s a conditional statement that executes only if the condition is false, instead of true. This lets you write little gems like this:

  i += 1 unless i > 10

  unless person.present?
    puts "There's no such person" 

However, as with anything that gives you a little power, it can be abused. The following abomination is but a sample:

  unless !person.present? && !company.present?
    puts "do you even know what you're doing?" 
    puts "and now we're really confused" 

I do not doubt that there are people out there who can decipher the logic above without breaking a sweat. But for most folks, combining negations, multiple conditions, and (gasp!) else clauses with an “unless” statement makes for challenging reading. It also makes it far too easy to introduce bugs into the logic.

Some rules of thumb when using unless:

  1. Avoid using more than a single logical condition. unless foo? is fine. unless foo? && bar? is harder to parse.
  2. Avoid negation. “Unless” is already negative. Piling more on only makes it worse.
  3. Never, ever, ever use an else clause with an unless statement.

Not only will others thank you when they have to read your code, you’ll thank yourself when you have to return to that code a month or two down the road.