I was wondering which is better style to return a parameter from a method:
1.
if (someBooleanIsTrue)
{
someTypeList = getTypeInstance(param1, param2);
}
else if (anotherBooleanIsTrue)
{
someTypeList = getTypeInstanceSecondMethod(param1, param2);
}
return someTypeList;
2.
List<SomeType> someTypeList = null;
...
if (someBooleanIsTrue)
{
return getTypeInstance(param1, param2);
}
else if (anotherBooleanIsTrue)
{
return getTypeInstanceSecondMethod(param1, param2);
}
return new ArrayList<SomeType>();
Which option do you like better and why? Please have arguments 🙂
Cheers
It’s pretty common for people to tell you that any given method should only return in one place. I find that if methods are kept short and simple then multiple returns aren’t as big of a readability issue. The book clean code has a pretty good discussion on the subject of readable code, including this particular topic.