0

Which of the following blocks of code is the better style?

void doSomething(Object foo)
{
    if(foo == null) return;

    .
    .
    .
}

or

void doSomething(Object foo)
{
    if(foo != null) 
    {
       .
       .
       .
    }
}

I know they have the same semantic meaning, but I've seen both styles used around the web and within production code. Are there any big reasons I should use one over the other?

agulen
  • 64
  • 1
  • 6

1 Answers1

0

This might be more of religious question. In one camp there's the return early ideology, in the other is the single return per method crowd. Personally I think it depends on how long the method is and if the valid case is the majority the length. If the valid case is the longest part, I tend to return early for the invalid cases.

Roman Dvoskin
  • 384
  • 4
  • 16