Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning from functions  RSS feed

 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From a performance POV, is it better to return at just one place from a function rather than several places? Someone mentioned something to do with JVM allocations on the function stack.

Example:

Case 1:



Case 2:


Is Case 2 better than Case 1?
[ May 29, 2007: Message edited by: praveen balaji ]
 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Case 2 is always good from maintenance and support point.
When some other developer is looking at your code there makes it easier for hiom if there is a single return in the method
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will go with Case II.I always prefer single return point for methods.
 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit A. Patil:
Case 2 is always good from maintenance and support point.
When some other developer is looking at your code there makes it easier for hiom if there is a single return in the method


I should have been clearer there. I wanted to know from a performance POV. I modified the question.
 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
I will go with Case II.I always prefer single return point for methods.


Someone mentioned that. Any reason why?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you like modify a function with 10 exits/returns ,
knowing the fact that there are no JUnits for unit testing that piece ?
 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
Would you like modify a function with 10 exits/returns ,
knowing the fact that there are no JUnits for unit testing that piece ?


As I mentioned above, the question is about performance.

Just for the record, there are several stylistic reasons for and against the use of "guard code" (Case 1). However, I'm interested in the performance implications.
[ May 29, 2007: Message edited by: praveen balaji ]
 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by praveen balaji:


As I mentioned above, the question is about performance.

Just for the record, there are several stylistic reasons for and against the use of "guard code" (Case 1). However, I'm interested in the performance implications.



Someone mentioned it has something to do with how Java allocates the function stack. I was looking for details.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To my best of knowledge it has got nothing to do with performance.

Just curious to know that what made you think that it has something to do with performance.
 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
To my best of knowledge it has got nothing to do with performance.

Just curious to know that what made you think that it has something to do with performance.


I USUALLY prefer early aborts - specially when I'm validating function input. But someone mentioned it has performance implications. Googling didn't help much, but there may be a better search string I should be using.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We had an extendended discussion on this subject recently in this thread. Not focussed on the performance aspects, though, which is likely to be negligible anyway.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi praveen,

I m not sure of the stack thing and how it deals with it. But i am aware that the compiler has to make an entry of where are all the places the method invocation might possibly return so that it can transfer the control with the associated details of the called method.

As rahul said, having one return statement would make the code unambiguous but *not always*. At certain places, where you have to have a direct return statements then and there especially inside the conditional executions & loops. Otherwise you might have to have some extra code to deal with it once you come out of the loop.
 
Ricky Clarkson
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's easy to compare the bytecode, take a look at javap.

The performance implications are negligible, if existent at all.

Look at it from a readability point of view instead. In small methods, multiple returns are fine. In larger methods, they may sometimes be confusing. But then larger methods may be confusing regardless.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that, if your method is run frequently, the native compiler and optimiser will work hard to make it run as quick as possible. It will analyse your code and do all sorts of clever things to it, meaning the operations it actually performs aren't quite what you think.

Given this, there is even less point worrying about tiny points of style like this, regarding performance. Just write clear, concise code and the optimiser will sort it out.

In fact, I read a recent article by a Sun guy (can't be bothered to find it now, but Google may help you), where he said almost all attempts to do micro-optimisations are counter-productive for performance nowadays. This is because the optimiser is optimised for the commonest, most-obvious types of code, and can be confused by weird "optimised" source code.
 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys for the input and links.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!