M Easter
Software Composer - http://codetojoy.blogspot.com
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Originally posted by Jeanne Boyarsky:
What about a third choice of "no more than two return statements" ? I'm in that camp. Having two makes sense - a guard and the "real" one. More than that tends to result in code that is too difficult to refactor because returns are strewn throughout.
M Easter
Software Composer - http://codetojoy.blogspot.com
But no hard religion. I don't consider multi-returners blasphemers, and I never try to tell anyone that single-return is the one true way to light and goodness. And if it makes sense for a method to have multiple returns, I'll code it that way without feeling that I need to shower afterwards.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Jesper Young:
I prefer a single return statement but I'm not very "fundamental" about it. However, I did find a bug recently in the software that I'm currently developing that was caused exactly because there was an extra return statement, it looked like this:
The problem was that the extra code below the loop, which was added to the method later, wasn't executed when the condition in the if-statement was true, so that the method returned from the loop immediately. Ofcourse this bug would have been prevented if I wouldn't have allowed multiple return statements.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
M Easter
Software Composer - http://codetojoy.blogspot.com
Originally posted by M Easter:
A colleague of mine had a rule: in addition to a single return, also no break or continue statements. They cause bugs.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
If not now then never.
Originally posted by Joe Wolfe:
With plenty of inexperienced ptrogrammers around an insistance on one return only helps keep the code easy to read.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Henry Wong:
Would this make your "skin crawl"?...
"I'm not back." - Bill Harding, Twister
Originally posted by M Easter:
IMHO, once there is more than one, then the bubble bursts. ...
the "Broken Windows" theory. ... my concern is that once > 1 return occurs, others are likely to follow. Perhaps not by you or me but by that intern.
(b) Maybe it's me but when I'm debugging or reading code and see more than one return, my brain feels a need to go into "parallel" mode. I know that multiple returns does not equate to multiple threads, but still....
(c) It just seems so much easier! One sets the default value for the result and then only change it as necessary.
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Joanne
Originally posted by Daniel Platon:
why wouldn't one use "break"
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
"I'm not back." - Bill Harding, Twister
Jesper: really? Using finally is abusing exception handling? I really can't agree with that. No exceptions needs to be thrown, and the path through the code is linear. The try/catch/finally blocks do add a bit of visual clutter which I dislike, but otherwise, I don't see a problem.
Hm, I rather thought that was part of the point, that the programmer does want the code executed even if an exception was thrown. Certainly, if they don't, they shouldn't code this way. But if they do, they should.
"I'm not back." - Bill Harding, Twister
Originally posted by Henry Wong:
Would this make your "skin crawl"?...
"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
SCEA, PMP, read my blog: www.yegor256.com
Campbell Ritchie wrote:[edit]Nobody noticed the missing return type (int) which I have corrected?[/edit]
Stephan van Hulst wrote:I was more surprised with why the method was both final and static, since static methods are final by definition.
It is a compile-time error to attempt to override or hide a final method.
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|