3

Possible Duplicate:
Why should a function have only one exit-point?

I heard that method must have ideally one (no more) return statement. It is true?

For example what type of method is better?

//1
public Object getResult() {
   Object result; 

   if (someValue != null) {  **// NOT null checking**

       // initializing result
   }
   return result;
}


// 2
public Object getResult() {
   Object result; 

   if (someValue == null) {  // **null checking**
       return null;
   }
   // initializing result
   return result;
}
Community
  • 1
  • 1
MyTitle
  • 17,348
  • 50
  • 147
  • 265
  • 2
    The real world is far from ideal :-) So the answer depends on the project and your preferences. Personally I'd go with 2nd – zerkms Nov 09 '12 at 09:37
  • Your example isn't really about the number of return statements, it's about whether or not to deal with errors and special cases. And in general, if in doubt, handle errors gracefully. What that means is entirely up to you. – Jochen Nov 09 '12 at 09:39
  • This is totally subjective. When you just make sure that your methods are short and focused it doesn't matter which one you use. – Daniel Hilgarth Nov 09 '12 at 09:40
  • There is a context around what you've been told which is very important. Where did you learn this from? – Thorbjørn Ravn Andersen Nov 09 '12 at 10:09

8 Answers8

2

One of the guidelines of the structural programming is not to use multiple exit points in your function (method). The reason is mainly readability (try to draw an algorithm which has multiple exit points, it won't look very nice). However, today very few people draw algorithms before writing some code and modern IDEs can detect unreachable code etc. Multiple exit point method allows more flexibility, hence you can make a method that returns before the code that would make some side effects if it had continued with execution. Also, this in most times prevents the usage of complicated condition tests and therefore can be more optimal (i.e. faster). Off course how compilers change your code is a difficult question, but I presume that most of them make from your single exit method a multiple exit method. I tend to make single exit methods wherever that doesn't significantly influence performance and/or involves complicated, hard to read, selections (if, switch-case itd.)

Nikola Davidovic
  • 8,189
  • 1
  • 25
  • 32
1

If you are using checkstyle in Eclipse then it is definitely one. But there are some situations when it makes writing code more difficult (for example in some recursive methods where you are testing for a base case) if you can have only one return statement in those cases I think that you should use what's best in the context.

So it depends on the context.

Adam Arold
  • 26,256
  • 18
  • 92
  • 176
1

How many return statements ideally must have a function?

I'll say only one but not at the cost of readability.

If having more than on return statements improve the readability of code, you should choose to have more than one exit points from a function.

At the end, it depends on personal choice and project coding guidlines.

If i was to choose between the two versions of code you provided, I'll choose 2nd version. For me it is more readable.

Azodious
  • 13,385
  • 1
  • 32
  • 68
1

I say you can definitely have more than one return statement. If you have the rule "only one return statement", you might end up with this:

if (value != null) {
  if (value.fieldA != null) {
     if (value.fieldB != null) {
        // initialize
     }
 } 
 return result;

instead of this:

if (value == null) {
   return null;    
}
if (value.fieldA == null) {
   return null;
}
if (value.fieldB == null) {
   return null;
}
// initialize
return result;

I find the second much more readable, easier to debug and probably more efficient in some cases.

mxns
  • 199
  • 1
  • 7
  • 1
    you could also make one if statement with && in the example you are giving – bvanvelsen Nov 09 '12 at 09:49
  • yes, but i do not find that very readable either, say you have 10 conditions like that? – mxns Nov 09 '12 at 09:50
  • then you need more methods I think, one method should have one clear function. If you need many checks to even perform some BL in a method then those checks should be placed in another method and not one method that checks conditions and perform stuff(my opinion) – bvanvelsen Nov 09 '12 at 09:53
  • perhaps, but that sounds like a very theoretical opinion to me. you mean that you should never have to check more than one condition in a single function? or you should always separate condition checking from everything else? – mxns Nov 09 '12 at 10:01
  • code should not only be _readable_, it should also be _writable_. if you can write in a way that is easy to understand, debug and modify, with fewer keyboard strokes, go for it – mxns Nov 09 '12 at 10:07
  • 1
    After reading Clean Code by Robert Martin, I try to make sure that each method has no more then 2 if statements. Otherwise changes are you, or future you or a colleague might not know what the method does one year from now. If you need more checks I try to place them in other method(s) specifically designed to make sure these checks are covered in those methods. One method->one responsability – bvanvelsen Nov 09 '12 at 10:16
  • fair enough, thanks for the input! – mxns Nov 09 '12 at 10:17
0

In second example will return null and wond read next rules. I add return statements when I'm checking values which function accept, as I don't want to process all function if my values are bad, so I retunr null or throw an exception.

Epsil0neR
  • 1,610
  • 14
  • 22
0

> Both are correct. Its depends on scenario of your project.

For Ex:

In Second case of your code, if i have a many number of lines code below this

**if (someValue == null) 
  {  
    // **null checking**
   return null;
  }**

then returning from "if" statement is correct

If you have a very less code like in your first example(code) then setting the return value in each if condition and return it @end is ok

> Refere this also

Community
  • 1
  • 1
andy
  • 5,583
  • 2
  • 22
  • 47
0

If you have just one return statement; then code looks cleaner. But there is no hard restriction on number of return statements. So you can have multiple return statements. But this also doesn't mean to have say 10 return statements.

I feel if you have 2-3 return statements; method looks cleaner and easier to understand. If number of return statements increases then its sign that you should perform code refactoring and convert one method into multiple methods.

rai.skumar
  • 9,001
  • 5
  • 37
  • 54
0

This is a matter where probably everyone has it's own opinion. But I think there isn't a clear right or wrong answer. I usually don't care about the number of return statements. If there is nu reason to stay in some method, then I return out of it.

bvanvelsen
  • 1,712
  • 1
  • 14
  • 24