Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 67420
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 67420
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
    Bookmark Topic Watch Topic
  • New Topic