- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
And the REALLY REALLY sad part is that all loops, in just about any language, such as "while" and "for" are really implemented using GOTOs!
Okay, the really sad part is that people think jumps/gotos in the compiled assembly instructions are a sad part of a programming language.
Admin
And the REALLY REALLY sad part is that all loops, in just about any language, such as "while" and "for" are really implemented using GOTOs!
Okay, the really sad part is that people think jumps/gotos in the compiled assembly instructions are a sad part of a programming language.
Admin
Admin
Think I'm tempted to agree. I looked again at the code I wrote above and have to admit that the immediate return is probably best.
I have been in a development environment where the "single return" has been a mandatory standard, but then my colleagues were generally second-rate and needed these constraints. The above "set-and-check" technique was the attempt to cleanly implement the single return - it worked, adhered to standards and kept the depth small.
Another technique I used to use (this was Fortran, so bear with me) was to have a label at the bottom, say 999, which led to a section of code that printed a message based on the current error code status, and either exited the program or returned with an error. Any time one of the (for example) "file read" or "file open" functions failed (and the program could not therefore continue), there would be something like: "IF (ERRSTAT .NE. STATNORM) GOTO 999" after the appropriate file handling statement.
The alternative would have been an unwieldy sequence of if/then/else which would have got unmaintainably deep, particularly with routines which demanded several files to be open at once.
Admin
But your comment is absolutely correct: different people have different needs, and their decisions (whether obeying an arbitrary "industry rule" or not) should be based entirely on those needs.
Admin
EDIT: The more I think about it the more I realize its really just personal coding style and past experience that will shape which method a person prefers.
Admin
Yeah, I thought I saw more agreement there than disagreement. Sorry to spoil a good argument... now let's go out and burn down a barn, after filling it with programmers who write 20-page functions and then goto the middle of them.
Admin
Admin
Admin
Admin
Admin
Admin
Admin
Matches? Are we back to regex already?
Admin
Admin
There are a couple reasons, and in my experience, single return tends to benefit code maintenance tasks. Primarily, it makes it so that the normal (non-exceptional) code flow of a function can be deduced entirely by just looking at the indentation and/or brackets. This is a valuable property for newcomers trying to figure out what a function will do given various inputs.
Second, in complex functions, single return requires naming the thing you are returning. Combined with good naming, this can help clarify the intent of the original developer.
Third, it encourages developers to break down huge script-like functions into smaller pieces. Single return naturally causes code to become indented more than code with early returns. Of course, some developers will write 10000-line functions even if it means indenting 100 tabs.
Some people claim that single return frequently results in contrived code. As a rule of thumb, I find that if I am thinking a function would benefit from multiple return points, the code would probably benefit more from factoring out a helper function.
Admin
USA vs. Britain Imperial Units vs. Metric Grammer Starbucks vs. Tim Hortons Tea Party
Admin
Also, there are some (probably uncommon) cases in C++ that are difficult to compile in a zero-overhead way (array constructors, for example).
Admin
D/S Switching Ass to Mouth Sex with furniture, What do you think?
Is it the furniture, askistmet? Is IT?
Admin
Whereas with multiple return, you have to play spot-the-return. Okay, that's reasonable - but in your version you have to track much more nesting. As above, the problem you're describing is one of function length. If you run into this problem, the original writer should be thrown to the zunesis and the problem method refactored until you can take the whole thing in at once.
I'm sorry, but by observation the return value is always called something like "returnValue" or "returnArray" or whatnot. So there's no information there about what it should be, only what's going to happen to it eventually.
This is an interesting argument. So you're saying that by making long functions so agonizing that no reasonable programmer would write them that long, you're encouraging short functions? The trouble is you're assuming reasonable programmers, but the creators of these monstrosities are obviously neither.
This, I can go along with, I suppose. Any time there's an issue of the structure, the first question is: does this function work on too many levels at once? And in the end, either multiple-return or single-return should be equally easy to read.
Admin
Admin
Here in Hyderabad, it is featured rule never to be having return statement exception for the last method lines.
Admin
Unless of course you actually are just retarded or a bot, in which case ignore this request.
Admin
Admin
Admin
The real WTF is that your kid being mentally challenged is something noteworthy. If our schools don't challenge all of our children mentally, it's no wonder they end up as retards.
Admin
Admin
Admin
Your not too smart, are you?
Admin
Actually had someone assert that here, yesterday. No wonder there are never any comments... sigh.
Admin
What, you had a two-year-old bot for a son? My sympathies.
Admin
Thank fuck for Bob turning up then, this thread was in danger of turning into profound, intelligent and almost useful. And we don't want that sort of thing around here, thank you very much. Now, can we get back to business, you bunch of snot-gobbling turd-burglars?
Admin
Why, can you no longer afford one because you've spent all your money on "healthcare" for your substandard offspring?
Admin
Admin
first comment i ever laughed out loud...kudos to you and the 2.5 Gearys pale ales i just consumed.
Admin
And what was that about being "thrown to the zunesis"? PRICELESS!
Admin
Soon, you will be an elitist code-slinging motherfucker like the rest of us. Just keep reading this site, since it will teach you the attitudes that will guide your coding and make you great.
Admin
Admin
Instead of multiple returns, I allow myself one goto (and ONLY one which is named the same and serves the same purpose in every function): goto _exit;
The end of my functions look like this:
ret = SUCCESS; _exit: // cleanup mem/FPs. .... return(ret);
Admin
FTFY.
Admin
I also do sometimes fail as you did sir, and I believe it to be a shame and a proof that I'm not a machine yet.
So yeah, machine-ify yourself goddamit !!
Admin
What's wrong with this way of using multiple returns? (apart from the fact that there exists a TryParse method in the framework)
Admin
Spot on . The main thing I do when I refactor, is make functions short. There's nothing that's 10 (real, not 867 character long) lines long AND unreadable ... well almost nothing.
Same .. I don't see how a goto in a 10 line function could confuse anyone, neither could if/else madness or early returns.
Take that up to 20 lines it's still manageable .. maybe even 30 (that's all the indentation you need, stop there thanks.)
CAPTCHA:"tation" .. err what-tation ?
Admin
The benefit of a single return is that it's possible to add tracing of the return value in a single place.
The benefit of multiple returns is you can use them to enforce clear constraints on a function making code very readable.
Admin
Nice example . sorts of goes back to what I wrote above about function length and it's better looking than if(){if(){if(){}}}return $return;
Admin
+1 :)
Admin
Hmmm... That makes me wonder... is it legal to EVER engage in intercourse with a mentally handicapped person? Even if their legal guardian OK's it? Anyone know?
I'm going to be doing to some digging, see if I can find this out.
Admin
Well, as long as you're digging, find me a corpse or two, I'm getting horny from the talk here --
Admin
Admin