-1

I am trying to optimize and write the best code possible (as any developer is) and I have been racking my head trying to figure out if this particular method I am writing is bad code. For some reason in my head I find that having multiple return statements are bad as opposed to having just one. Yet I have nothing in solid writing stating this is bad. Please tell me if this is bad code or not

public boolean fooBar(int foo, int bar){
   if(foo == 3) {
     return true;
   }

   if(bar == 3) {
     return true;
   }

   System.out.println("foo or bar is not equal to three");
   return false;
}

I am not looking for simplification, I am just looking for general ideas on if this is bad code or not and if there are performance issues that could arise from doing this.

superuserdo
  • 1,637
  • 3
  • 21
  • 33
  • In terms of execution, I think it's a good thing. If your condition is satisfied then it returns, no need to execute other code. I might be wrong though :D – Soumitri Pattnaik May 17 '15 at 20:25
  • Some older people like to have only one return statement at the bottom since that's the only thing that was allowed in some language (can't remember which), but nowadays it's really a matter of opinion. I would definitely have some return statements at the top for base cases of recursion for example. [This](http://stackoverflow.com/a/733858/645270) Code Complete quote is nice. – keyser May 17 '15 at 20:26
  • 2
    @keyser I'm 66, so I probably count as an "older person". I use multiple returns in Java, but not in many other languages that permit them. The difference is that Java has try-finally. If I need e.g. for debug to force something to happen on every return path, I can do it without rewriting the whole method. – Patricia Shanahan May 17 '15 at 20:30
  • @PatriciaShanahan that's an excellent point. – keyser May 17 '15 at 21:08

2 Answers2

1

In general, is a good practice to have one return statement per method. Some reasons to support this:

  • you have a clear flow in your method, as you do not have to trace multiple exit points.
  • derived from the previous one, a clear flow it means a more testable method. Your unit tests can easily follow and check the different flows in your code, that leads up to the single point of exit;
  • it is more maintainable;

Some arguments in the favour of writing with multiple returns: you write the code a bit faster, you do not use local variables to hold return values up until the final exit point, you avoid breaking out of the loops (in case you need to reach the return statement), or if nor breaking out, enhancing loop conditions to consider also the return values as part of exit criteria.

iullianr
  • 1,274
  • 10
  • 11
1

Personally, I would reduce redundancy by condensing the boolean logic with , but your code is generally acceptable.

public boolean fooBar(int foo, int bar) {
    if (foo == 3 || bar == 3) {
        return true;
    }
    System.out.println("foo or bar is not equal to three");
    return false;
}
Bob Jones
  • 37
  • 2
  • 11