aspose file tools*
The moose likes Beginning Java and the fly likes return statement practices Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return statement practices" Watch "return statement practices" New topic
Author

return statement practices

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Is it really that bad of a practice to return true and false? if so can someone tell me why?




Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Where did you hear that? And in what context?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
You should use the square root of n, not n / 2. And try to avoid value of 0 or 1 for i, since ArithmeticExceptions are annoying, and the fact that n % i is always 0 will give you an out-by-one ( ) error.

I think yes. I know other people will disagree, however.
It can be confusing and more difficult to maintain to have multiple returns. And remember that in structured programming you can always, but always program everything with multiple sequence repetition and selection.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Are you asking if it's "bad" to return a boolean? or "bad" to have multiple returns?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Sorry for the isPrime mistakes ; was trying to whip up a quick example.

@Bear - I was asking about return the boolean values true/false. It was something one of my friends was telling me, he said that apparently it was a bad practice; I had never heard that before, so I wanted to check.

Thanks for your replies.
Hunter
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

What possible justification could he have for such a statement?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

None that I could find on the internet, so that's why I posted here . It could be that he was complaining about the multiple returns I was using, but from what he said it seemed to me he was saying that returning the booleans without storing them locally was a bad practice.

Hunter
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

News to me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
Campbell Ritchie wrote: . . . I know other people will disagree, however. . . .
See, told you so
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
It could be that he was talking about a case like this:

Many of us would prefer to write this as

simply because it's shorter. But that only works if you're returning something on both branches of the if statement. In your first example above, you return on only one branch each time, and continue with the method otherwise. So the trick I just showed doesn't work. I would say your original code with multiple returns is fine - though Campbell's point about using Math.sqrt() is a good one, and the first if statement isn't necessary if you start the index at 2 as Campbell did:

To me, the multiple returns above are slightly more readable than the code Campbell gave with the extra boolean condition. But often, readability is just a matter of what you're used to.

Note that I also avoided recalculating Math.sqrt() each time through the loop. Then again, there are plenty of additional optimizations possible for a good industrial-strength prime checker, and that's beyond the scope of what you probably intended as a simple example.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

Sounds like he meant the multiple returns, then. Some people consider it bad practice to have multiple exit points from a method.

Personally I think that's overstated. Having multiple returns can be confusing, but there are other occasions when it's clearer than the alternative - and clear code is really the aim here.

I'd see it more as a potential warning flag. If you're using multiple returns, just take a second to ask yourself if your code is as clear as it could be. If rearranging it to have a single return would make it clearer, do it. If not, don't worry about it.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Hunter McMillen wrote:None that I could find on the internet, so that's why I posted here . It could be that he was complaining about the multiple returns I was using, but from what he said it seemed to me he was saying that returning the booleans without storing them locally was a bad practice.

Ah, now that I've read that, I don't think he was talking about my if-condition-return-true-else-return-false example. Oh well. Then I'll simply say I agree with Bear on this one. Sorry, Campbell.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
I earlier wrote:. . . I know other people will disagree, however. . . .
See how correct I was in that opinion
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

Here's an article arguing for the use of multiple return statements.

For balance, I was going to link to an article arguing the opposite. I couldn't actually find one though. All the (vaguely recent) ones I could find (in a quick search) were saying "some people say that multiple returns statements are bad, but I disagree because...". Nobody standing up for the structural purists!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Campbell Ritchie wrote:
Campbell Ritchie wrote: . . . I know other people will disagree, however. . . .
See, told you so

No, you didn't. The question is on the practice of return boolean, not using multiple returns.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

There's nothing wrong with returning true or false directly. There's no reason to store the values somewhere first before returning them. Mike Simmons makes a good point about just doing return condition; instead of if (condition) return true; else return false;.

Having multiple returns can lead to bugs, I've experienced that myself a few times. For example, I worked on a system that had to do with the railways, which contained a lot of business logic to find trains in the system, the matching carriages, train schedule etc. I don't remember all the details exactly (it's been more than a year ago that I worked on that software), but there was a lot of searching and matching of objects in collections. Suppose there would be a method that looks like this (not an actual method from the source code):

There were a number of early returns in the method.

One day, new features had to be added, which meant that some more processing on the list of carriages had to be done. Initially, we just added the additional processing to the end of the method. During testing we discovered that the additional processing was sometimes not done. The cause was those early returns: other logic in the method would do an early return under certain circumstances and skip the additional processing. We had to meticulously go through the code again to make sure that it would do the additional processing at the right time.

If there wouldn't have been all those early returns, it would probably have been easier to add the new functionality, and we wouldn't have had the bug.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
Bear Bibeault wrote:The question is on the practice of return boolean, not using multiple returns.
The original post had returns in the middle of a for loop, which looks like multiple return to me. I agree this sort of thing . . . is an abomination, and it looks even worse if you realise the "else" is redundant and write
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return statement practices