Ruby Journal

Avoid Unless Syntax in a Chain of Conditions Statement

| Comments

Ruby gives us a nice human friendly unless which is equivalent to negation of if. Yet if we abuse using this method in a long complex statements, it could add more confusion for normal readers. In this article, I’ll give you one example to prove that you should not use unless in a chain of conditions statement.

I have been using unless for a long time and to my surprise using this method in a chain of conditions is confusing for human brain as many of my colleagues misinterpret the code all the time. Here is one example:

1
2
3
unless @employee.on_annual_leave? || @employee.on_parental_leave?
  puts "You have no leave yet!"
end

Majority of normal human would read it as “if employee is not on annual leave or employee is on parental leave”. Which is completely wrong, this ambiguous code is supposed to read “if employee is not annual leave and employee is not on parental leave”. And for a complex piece of code, misreading this condition could yield detrimental issues. So I’d suggest not to use unless and instead to use if !condition. Let’s refactor our code:

1
2
3
if !@employee.on_annual_leave? || !@employee.on_parental_leave?
  puts "You have no leave yet!"
end

This code would always read as “if employee is not on annual leave or employee is not on parental leave”, which is precise to the context. So be wise before using unless, in cases negation with if is much more concise.

I hope you enjoy this short article, and please leave comments if you like it.

Comments