The Truth Speaks for Itself

October 24, 2009

This one isn't just for Ruby — it applies to pretty much every programming language under the sun.

Don't wrap a boolean expression in a control statement just to return true or false:

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

The expression already is a boolean. Return it directly:

def foo
  return some_boolean && other_boolean
end

It is very rare that I ever need to return an explicit true or false. If you find yourself doing it, treat it as a warning sign.

And of course, in Ruby you don't need to return explicitly, so you can simplify further:

def foo
  some_boolean && other_boolean
end
Questions or thoughts? Get in touch.