Click here to Skip to main content
15,914,111 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: C/C++ or C#? Pin
Thomas Weller3-Nov-08 10:17
Thomas Weller3-Nov-08 10:17 
GeneralRe: C/C++ or C#? Pin
Nemanja Trifunovic3-Nov-08 10:35
Nemanja Trifunovic3-Nov-08 10:35 
GeneralRe: C/C++ or C#? Pin
Dan Neely4-Nov-08 2:16
Dan Neely4-Nov-08 2:16 
GeneralRe: C/C++ or C#? Pin
Nemanja Trifunovic4-Nov-08 4:29
Nemanja Trifunovic4-Nov-08 4:29 
GeneralRe: C/C++ or C#? Pin
supercat95-Nov-08 6:18
supercat95-Nov-08 6:18 
GeneralRe: C/C++ or C#? Pin
Thomas Weller5-Nov-08 6:48
Thomas Weller5-Nov-08 6:48 
GeneralRe: C/C++ or C#? Pin
Thomas Weller5-Nov-08 6:55
Thomas Weller5-Nov-08 6:55 
GeneralRe: C/C++ or C#? Pin
supercat95-Nov-08 10:17
supercat95-Nov-08 10:17 
Thomas Weller wrote:
The second alternative is just replacing return; with break;. Furthermore, it introduces a hardcoded boolean expression which always evaluates to the same value. This in my opinion is not very desirable in itself.


Replacing a return with a break may be useful if the code has to do something besides totally exit a function. If the routine opened a file at the beginning, for example, I would consider doing a break and then closing the file after the 'while' to be much cleaner than doing "close(theFile); return;" in each failure case.

It's a little irksome having a hard-coded boolean constant like that, but C does not provide any other block structure whose semantics are "run once, but be able to jump to the beginning or end." I would consider "do ... while(0);" and "do ... while(1);" to be cleaner than a "goto", at least in cases where the enclosing block does not contain any case labels.

If a certain amount of code will be common to several case handlers, it would be far better to do something like:
switch(foo)
{
  case 0:
    code_0_special();
  COMMON:
    common_to_code_0_1_3();
    break;
  case 1:
    code_1_special();
    goto COMMON;
  case 2:
    code_2_special();
    break;
  case 3:
    code_3_special();
    goto COMMON;
  default:
    handle_default();
}

than
switch(foo)
{
  case 0:
    code_0_special();
  do {
    common_to_code_0_1_3();
    break;
  case 1:
    code_1_special();
    continue;
  case 2:
    code_2_special();
    break;
  case 3:
    code_3_special();
    continue;
  default:
    handle_default();
    break;
  } while(1);
}

or
switch(foo)
{
  do
  {
    case 0:
      code_0_special();
      break;
    case 1:
      code_1_special();
      break;
    case 3:
      code_3_special();
      break;
  } while(0);
    common_to_code_0_1_3();
    break;
  case 2:
    code_2_special();
    break;
  default:
    handle_default();
    break;
  } while(1);
}


The former would IMHO be an appropriate use of "goto"; the second is just plain horrible. The third isn't quite so bad, but is IMHO less clear than the goto.
GeneralRe: A big if Pin
Paul Conrad2-Nov-08 5:03
professionalPaul Conrad2-Nov-08 5:03 
GeneralRe: A big if Pin
Tom Deketelaere2-Nov-08 23:30
professionalTom Deketelaere2-Nov-08 23:30 
GeneralRe: A big if Pin
Kevin McFarlane5-Nov-08 23:53
Kevin McFarlane5-Nov-08 23:53 
GeneralRe: A big if Pin
Tom Deketelaere6-Nov-08 0:20
professionalTom Deketelaere6-Nov-08 0:20 
GeneralRe: A big if Pin
Kevin McFarlane6-Nov-08 1:00
Kevin McFarlane6-Nov-08 1:00 
GeneralRe: A big if Pin
Tom Deketelaere6-Nov-08 1:23
professionalTom Deketelaere6-Nov-08 1:23 
JokeRe: A big if Pin
Thomas Weller6-Nov-08 1:33
Thomas Weller6-Nov-08 1:33 
GeneralRe: A big if Pin
Tom Deketelaere6-Nov-08 1:41
professionalTom Deketelaere6-Nov-08 1:41 
GeneralRe: A big if Pin
Thomas Weller6-Nov-08 2:17
Thomas Weller6-Nov-08 2:17 
GeneralRe: A big if Pin
Tom Deketelaere6-Nov-08 3:09
professionalTom Deketelaere6-Nov-08 3:09 
GeneralRe: A big if Pin
Nemanja Trifunovic3-Nov-08 8:25
Nemanja Trifunovic3-Nov-08 8:25 
GeneralRe: A big if Pin
Thomas Weller3-Nov-08 9:02
Thomas Weller3-Nov-08 9:02 
GeneralRe: A big if Pin
Kevin McFarlane5-Nov-08 23:57
Kevin McFarlane5-Nov-08 23:57 
JokeRe: A big if Pin
Thomas Weller6-Nov-08 1:37
Thomas Weller6-Nov-08 1:37 
GeneralRe: A big if Pin
Kevin McFarlane6-Nov-08 2:11
Kevin McFarlane6-Nov-08 2:11 
GeneralRe: A big if Pin
KarstenK4-Nov-08 23:47
mveKarstenK4-Nov-08 23:47 
GeneralRe: A big if Pin
Thomas Weller5-Nov-08 1:59
Thomas Weller5-Nov-08 1:59 

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.