Yesterday I had a thought in my mind (which is good already to have one) – how many return statements a method should have?
Whats the difference if you have method that looks like that:
[java]
public int calculateSum(int a, int b, int c) {
int result = -1;
if (a % b == c) {
result = c;
}
else if ((a + b – c) > (a – c)) {
result = a;
}
return result;
}
[/java]
or the same method but looks like that:
[java]
public int calculateSum(int a, int b, int c) {
if (a % b == c) {
return c;
}
else if ((a + b – c) > (a – c)) {
return a;
}
return -1;
}
[/java]
I always thought (at least this is how i was taught), that having one return statement makes code look more elegant, and i should try to avoid having multiple return statements. When i asked my colleague what he thinks about this, he said that at run time, method with multiple returns will execute faster and the code looks clearer. That sounded logical enough, and after doing some more investigation i have to say that i tend to agree.
Although some may say that having multiple return statements in a method can create a confusion, and it will be hard to see all the exit places. But if this is the case, i would say that maybe the method needs to be re-factored and simplified?
I don’t think this is a matter of taste. It’s more a matter of coding standards. Returns are a solid way of keeping control of your program. They are ideal for returning values for error’s.
Well.. I worked on a lot of different teams and I have seen both styles being used on the same projects. Personally I don’t mind if someone wants to use a single return point.
But the last point you are mentioning ‘returning errors’; that is something I would like to see an exception (this is not a 100% rule, but in most cases it is better to throw an exception than to return an errorcode). Errorcodes are very easy to be forgotten to handle (I have seen it go wrong many times)
yep, exceptions were meant to handle errors and returns meant to return a value. Plus i think its a good practice to follow the coding standards. Java already provides APIs for error handling, so there is no need really to use “return” an error.
Unless you know that you are not expecting exception, but you just returning null value, which you count as an error.
But multiple returns indeed give you control of your program.
By returning error’s I mean returning values simply other than the one you expected. There’s no need to throw an exception if the problem is minor. If I was going to bung in a load of exception handling I would use goto’s and only return once.
This is really a taste issue.
Personally I prefer to jump out of the method as soon if I can. because it makes code less ugly. When you don’t exit, it often leads to a lot of indentation.
I have heard the argument that is harder to make something ‘always’ happen with multiple return statements. But a try/finally block was invented for just that purpose.
Hi Peter,
yes i agree – its up to the each programmer individually to decide which way he/she wants to go. Just like you i prefer to jump out when i can, the code does look much clearer. Maybe because I am working in industrial environment my own approach is changing? Some things you just dont pick up at Uni