Friday, January 27, 2006

Nitpicking "if" structure

So what would be better?
def method
if !event
return nil
end
return Table.find_first_by_name("oak")
end


def method
return nil unless event
return Table.find_first_by_name("oak")
end


def method
return !event ? nil : Table.find_first_by_name("oak")
end


I figured the first is a bit clearer, but longer...and while the third is shorter, I'm not sure that a coverage tool would flag parts of it as uncovered. And the second...well, having two returns seem kinda ugly. What do you think?

1 comment:

  1. unless there are performance gains for very extreme situations (cpu cycles are cheap now), legibility is much more important than brevity.

    ReplyDelete