views:

31

answers:

1

Take for example the following two statements:

if (booleanVariable)
{
  doSomething();

  return true;
}
else
{
  return false;
}

and

if (booleanVariable)
{
  doSomething();

  return true;
}

return false;

Which one would be preferred?

They both return the same result in the end.

Any reason one would be better to use than the other?

+4  A: 

Personally, I think the cleanest way would be

if (booleanVariable) {
    doSomething();
}
return booleanVariable;

Moving the redundant returns outside the if block highlights what you are doing differently if the variable is set.

Or, whenever booleanVariable is set to true (probably defaulted as false), immediately call doSomething(). Either way, it's less code.
Stefan Kendall