It's not a secret anymore!*
The moose likes Programming Diversions and the fly likes Is use of only one return statement is considered as a good programming practise? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Is use of only one return statement is considered as a good programming practise?" Watch "Is use of only one return statement is considered as a good programming practise?" New topic
Author

Is use of only one return statement is considered as a good programming practise?

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

If we have multiple returns statement in a single method then what is the fuss about it? Don't it saves the processing if in case we return early based on some business rule. Someone said me that there should be only one return statement in a function.


My Blog SCJP 5 SCWCD 5
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

It is not a requirement but is considered as a best practice.
Having only one return statement for a method will make it more readable, while having many will only make the flow haphazard and difficult to understand.


-kolli
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

I hail from the FORTRAN days of structured programming and so I will tend towards the single return construct by training and nature. But only if it makes sense. If I have to contort the code to achieve a single return, I'll use multiple returns.

I prefer clarity to dogma.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11152
    
  16

I mostly agree with Bear. Follow your companies coding standards first. Then, write the code that is the easiest to follow/read/understand.

Everything else being equal, I prefer a single exit point.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Ravikanth kolli wrote:
Having only one return statement for a method will make it more readable, while having many will only make the flow haphazard and difficult to understand.


Sometimes, sometimes not. If the alternative to multiple returns is keeping a flag variable whose only purpose is to keep track of whether to return early or not, then I generally prefer the multiple returns. With the flag, you have even more stuff to understand.

Of course, an even better alternative is to keep your methods short enough, with a coherent-enough purpose, that they don't need to have multiple returns in the first place.


[Jess in Action][AskingGoodQuestions]
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2271
    
  28

I would consider differrent choices and pick the one that leads to smaller code. That's my rule of thumb. Less code to write. Less code to maintain.

If the choice is between



OR



I would use second version. THere's less stuff to maintain there

OTH if my choice is between


OR



I would do




Yes I cheated Certainly, these are very simplified examples, and it doesn't always work out so simply. Lines of code is just a rule of thumb. IMO, in most cases, you can make it simple enough with some creativity
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

I would again point out that clarity should rule. Less lines of code aren't always clearer -- in fact, sometimes just the opposite -- so I would caution against using line count as a metric.

 
 
subject: Is use of only one return statement is considered as a good programming practise?
 
Similar Threads
Comparable vs Comparator
exception
return statement after response.sendRedirect() method?
Return problems
Is it possible to create more than one HttpSession in our Application