views:

58

answers:

4

Hi,

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

A: 

Just a question for other SO users: When questions are obviously from academia (tests, homework, etc) such as this one, do people still answer them? Seems like we need a "do your homework" stamp.

desau
well, we can tag it as such. I guess asking people is also "doing" homework ;-)
Peter Tillemans
Mmm, sorry man, but this is no academia. I had a hard working day to disprove you :)
Emil Petkov
@epetkov: Sorry, I know the feeling... feel free to remove the tag I added.
Peter Tillemans
Please don't post new questions as answers to existing questions. Stack Overflow isn't a forum.
Helen
+1  A: 

I prefer the 2nd option because the 1st option returns a null value.

When lists are returned chances are that the calling method is going to iterate over the list and in >80% of cases iterating over an empty list when there are no results is exactly the right behavior.

With null you always have to check for the case that null is returned which makes the code and is easy to forget, because it is conceptually different.

Peter Tillemans
That could be avoided in the first case by initializing someTypeList to a new ArrayList<SomeType> at the beginning of the method.I think the primary question above was whether it was more appropriate to have multiple returns or a result variable that is set somewhere and then returned at the end.
Angelo Genovese
@Angelo You were right. My primary concern is for the users of the function and its impact on the rest of the codebase. I tend also to collect the return value and exit at the end, although i am not religious about it.
Peter Tillemans
+2  A: 

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.

Angelo Genovese
Nice, I'll read the appropriate section in the book.
Emil Petkov
+1  A: 

I prefer 1) because of the single return path, but I would make this change in case you don't want to return null:

if (someBooleanIsTrue)
{
    someTypeList = getTypeInstance(param1, param2);
}
else if (anotherBooleanIsTrue)
{
    someTypeList = getTypeInstanceSecondMethod(param1, param2);
}
if (someTypeList == null)
{
    someTypeList = new List<SomeType>();
}
return someTypeList;
iobrien
return someTypeList ?? new List<SomeType>();Hi, I don't know this syntax. Are you sure it's Java?
Emil Petkov
Thanks for pointing out my error, I thought this was C#.
iobrien