The truth speaks for itself!

Not just for Ruby this time. This applies to pretty much every programming language under the sun.

Don't use unnecessary control statements to determine whether you want to return true or false.

def foo
  if some_boolean && other_boolean
    return true
  else
    return false
  end
end

You should do this instead:

def foo
  return some_boolean && other_boolean
end

It's very very rare that I ever have to return an explicit true or false. This should be a warning sign.

Of course, in Ruby you don't need to return explicitly so you should do this:

def foo
  some_boolean && other_boolean
end
written by
Craig
published
24 Oct 2009

Disagree? Found a typo? Got a question? If you'd like to have a conversation about this post, email craig@barkingiguana.com. I don't bite.

My opinions are my own and my employer can't have them.