Better Java method Syntax? Return early or late?

13,493

Solution 1

If it's a method you'll be calling thousands of times, then early return is better to achieve a [slightly] increased performance.

If not, then I'd prefer late return, since it improves readability.

Remember programmers usually spend more time reading than writing code, so anything you can do to improve readability will be certainly welcome.

Solution 2

I prefer returning early and avoiding deep nesting. This is particularly true right at the start of the method: test anything that's simple, and get out (or throw an exception) if you can do so really early.

If it's right in the middle of a method, it's more of a judgement call.

Note that I'd refactor your example straight away to use a single if:

boolean validate(DomainObject o) {    
  if (o.property == x || o.property2 == y) {
     return true;
  } ...
  return false; 
}

I realise this was only a toy example, but my point is that it's always worth looking for more ways to simplify your code :)

Solution 3

As with most coding styles, it's really a matter of preference, but guard clauses are considered by many to be a best practice.

Solution 4

The only time I would say you definitely shouldn't return early is if you can't easily see every return within a single screen (whatever the standard might be for people working on the same code base), you should at the very least be adding comments indicating that the function can return early if there is an early return.

The only time I would say you definitely should return early is if your code looks like...

boolean valid = true;
if( condition1 ) {
   valid = false;
}
if( valid ) {
   ...
   if( condition2 ) {
      valid = false;
   }
}
if( valid ) {
   ...
   if( condition3 ) {
      valid = false;
   }
}
... (etc)

If you find yourself in either of these situations, however... you should probably be refactoring the function.

Solution 5

To me, this is sort of one of those religious war topics with no correct answer. The argument against returning early essentially boils down to the fact that having one and only one point where a function can exit reduces the number of possible paths through your code, thus, in theory at least, reducing the chances for bugs. My personal style is to, in situations where it makes sense to return early do so, and in situations where it makes sense to limit to one return statement I do that.

Share:
13,493
Gandalf
Author by

Gandalf

Java, C++ coder - sometimes Oracle DBA. Spring Framework, Security, Tomcat, MongoDB, Lucene.

Updated on June 21, 2022

Comments

  • Gandalf
    Gandalf about 2 years

    Duplicate: Should a function have only one return statement?

    Often times you might have a method that checks numerous conditions and returns a status (lets say boolean for now). Is it better to define a flag, set it during the method, and return it at the end :

    boolean validate(DomainObject o) {
      boolean valid = false;
      if (o.property == x) {
         valid = true;
      } else if (o.property2 == y) {
         valid = true;
      } ...
      return valid; 
    }
    

    or is it better/more correct to simply return once you know the method's outcome?

    boolean validate(DomainObject o) {
    
      if (o.property == x) {
         return true;
      } else if (o.property2 == y) {
         return true;
      } ...
      return false; 
    }
    

    Now obviously there could be try/catch blocks and all other kinds of conditions, but I think the concept is clear. Opinions?

  • Ben S
    Ben S about 15 years
    How is late return more readable? To me it's convoluted to return late. I infer from it that the method isn't done doing what it needs to if it hasn't returned.
  • tmsppc
    tmsppc about 15 years
    This is not a matter of guard clause; it's a matter of returning different values depending on parameters. Nobody talked about parameters being right or wrong, or the need of having preconditions.
  • tmsppc
    tmsppc about 15 years
    It's more readable because you don't cut the process flow in the middle of it. Instead, you save the state and return at the end of the method.
  • Vinko Vrsalovic
    Vinko Vrsalovic about 15 years
    I find the flag way less readable precisely because you have to follow exactly each condition, method call, etc. to know if someone else touched the flag before returning, whereas with the other alternative you are certain that when property is equal to x you will get true, no matter what else happens there.
  • Hace
    Hace almost 14 years
    +1 for your last line; you should indeed be pulling out smaller methods if the function gets to be too long.
  • Matt
    Matt almost 12 years
    It's pretty clear that guard code is a good place to put an early return in Java. You aren't "cutting the process flow in the middle" because the process hasn't started and you avoid wrapping the body of the method in an if block. Of course it is sometimes appropriate to throw an exception after guard code. Multiple nested blocks are the enemy of readability not early returns (but if you have multiple nested blocks early returns can make it worse).