Click here to Skip to main content
15,889,335 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
geoffs9-Dec-08 10:05
geoffs9-Dec-08 10:05 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
FatBuddha10-Dec-08 4:11
FatBuddha10-Dec-08 4:11 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult10-Dec-08 8:35
mvePIEBALDconsult10-Dec-08 8:35 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
FatBuddha10-Dec-08 14:48
FatBuddha10-Dec-08 14:48 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult10-Dec-08 15:43
mvePIEBALDconsult10-Dec-08 15:43 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
FatBuddha11-Dec-08 3:04
FatBuddha11-Dec-08 3:04 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult11-Dec-08 6:14
mvePIEBALDconsult11-Dec-08 6:14 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
supercat915-Dec-08 11:57
supercat915-Dec-08 11:57 
PIEBALDconsult wrote:
And that technique is a horror unto itself -- a Weasel-Goto.


Yeah, and writing
  if (condition)
{
  do_something();
}
else
{
  do_something_else();
}
is really using "weasel-gotos" to obscure the fact that you're really writing:
if (condition) goto TRUE_CASE;
do_something_else();
goto END_OF_IF;
TRUE_CASE:
do_something();
END_OF_IF:


The goto statement itself is not inherently evil, but gets a bad rap for a couple reasons:

-1- There is nothing inherent in the GOTO which gives any clue about where to find its target, or what the significance of its target might be; having the target of a branch always be either the first statement of an indented block that is being executed or has just completed, or the first statement following such a block (and having the control-flow instruction clearly imply which target applies) makes it much easier to see where the program flow is going.

-2- Programs which use gotos (they are unavoidable when coding in things like assembly language), but where the overall program structure is consistent with the style indicated in (1) are generally easy for both humans and computers to analyze. If a program can clearly be divided into nested blocks such that there are no GOTOs into any block from anywhere outside it, program-flow analysis will generally be pretty easy. One of the major problems, in the days before structured programming, was that there were many pieces of code that had to go somewhere, and there wasn't any perceived reason not to put them anywhere that happened to be convenient.

For example, consider the code I listed above; notice that a conditional branch will be taken in the true case, and an unconditional branch will be taken in the false case. If one case occurred much more frequently than the other, the code could be rewritten:
if (rare_condition) goto TRUE_CASE;
handle_false_case();  /* Optional */
END_OF_IF:
/* Continue on with code. */
/* After an unconditional jump somewhere, insert: */
TRUE_CASE:
  handle_true_case();
  goto END_OF_IF;
That approach will result in two branches taken in the true case, and none taken in the false case. In some situations, I've written code like that (when writing assembly code, and when performance was critical) but it's nasty to work with. Such code was the norm prior to the development of structured programming, and was the basis for much of the hatred related to GOTO.

Incidentally, I wish languages had a structure equivalent to "do{}while(0);" It can be very useful.
GeneralRe: Avoid return statement in the middle - horror or not? [modified] Pin
PIEBALDconsult15-Dec-08 13:33
mvePIEBALDconsult15-Dec-08 13:33 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
supercat916-Dec-08 8:14
supercat916-Dec-08 8:14 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult16-Dec-08 13:13
mvePIEBALDconsult16-Dec-08 13:13 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
supercat916-Dec-08 17:20
supercat916-Dec-08 17:20 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult17-Dec-08 12:24
mvePIEBALDconsult17-Dec-08 12:24 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Lutosław12-Dec-08 10:11
Lutosław12-Dec-08 10:11 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult12-Dec-08 10:45
mvePIEBALDconsult12-Dec-08 10:45 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
rfidel10-Dec-08 5:03
rfidel10-Dec-08 5:03 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult10-Dec-08 8:33
mvePIEBALDconsult10-Dec-08 8:33 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Manish K. Agarwal10-Dec-08 21:32
Manish K. Agarwal10-Dec-08 21:32 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult11-Dec-08 6:15
mvePIEBALDconsult11-Dec-08 6:15 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Thomas Weller11-Dec-08 19:56
Thomas Weller11-Dec-08 19:56 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult12-Dec-08 3:18
mvePIEBALDconsult12-Dec-08 3:18 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Marcello Faga11-Dec-08 1:30
Marcello Faga11-Dec-08 1:30 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
riced11-Dec-08 14:04
riced11-Dec-08 14:04 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Thomas Weller11-Dec-08 19:49
Thomas Weller11-Dec-08 19:49 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult12-Dec-08 3:34
mvePIEBALDconsult12-Dec-08 3:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.