The Truth Speaks for Itself

October 24, 2009 · 1 min read

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

These posts are LLM-aided. Backbone, original writing, and structure by Craig. Research and editing by Craig + LLM. Proof-reading by Craig.