• (cs)

    I'd like to point out (although someone almost certainly has by now, and I apologize if I'm not the first, but I don't have time to read 100 replies) that the Loop/Switch is not *always* a bad thing - it's just when the Switch occupies the entire loop body that it's ridiculously stupid, and in fact far worse than just writing out all the code sequentially.

    For example:

    for(int i=0;i<3;i++)
    {
    switch(i) {
    case 0: strFolder = 'C:\asdf'; break;
    case 1: strFolder = 'C:\Windows\Temp';break;
    case 2: strFolder = 'D:\v2'; break;
    }
    // do lots of stuff in the folder...
    }

    Of course, when you use a loop/switch to test 255 different things, and each time it's just a single function call, then it's generally a WHOLE lot faster to just write out the function calls without any loop or switch.

  • Dave (unregistered)

    We had an contract worker who put this kind of for-switch crap code in one of our projects. We fired him. The sad thing is, it was one of his more understandable pieces of code.

  • (cs) in reply to IsaacSchlueter
    IsaacSchlueter:

    I'd like to point out (although someone almost certainly has by now, and I apologize if I'm not the first, but I don't have time to read 100 replies) that the Loop/Switch is not *always* a bad thing - it's just when the Switch occupies the entire loop body that it's ridiculously stupid, and in fact far worse than just writing out all the code sequentially.

    For example:

    for(int i=0;i<3;i++)
    {
    switch(i) {
    case 0: strFolder = 'C:\asdf'; break;
    case 1: strFolder = 'C:\Windows\Temp';break;
    case 2: strFolder = 'D:\v2'; break;
    }
    // do lots of stuff in the folder...
    }

    Of course, when you use a loop/switch to test 255 different things, and each time it's just a single function call, then it's generally a WHOLE lot faster to just write out the function calls without any loop or switch.


    Nope, that's still a bad thing.  There do exist valid switches in valid loops, but this ain't one of them.  If you switch on your index variable, your logic is screwed up.

  • (cs) in reply to IsaacSchlueter
    IsaacSchlueter:

    for(int i=0;i<3;i++)
    {
    switch(i) {
      case 0: strFolder = 'C:\asdf'; break;
      case 1: strFolder = 'C:\Windows\Temp';break;
      case 2: strFolder = 'D:\v2'; break;
    }
    // do lots of stuff in the folder...
    }

    Ahem.....

    DoLotsOfStuff("C:\asdf");
    DoLotsOfStuff("'C:\Windows\Temp");
    DoLotsOfStuff("D:\v2");

    //    :
    //    :

    void DoLotsOfStuff(string strFolder)
    {
     // do lots of stuff in the folder...
    }

     

  • (cs) in reply to dubwai
    dubwai:


    You can do what I have shown in Java, which has no goto (goto is reserved but cannot be used.)

    This is actually a technique that was once suggested (seriously) to someone who wished to convert a large amount of goto filled code to Java.



    To a large extent, labels, continue and break can be used to fake gotos in Java.

  • (cs) in reply to joodie
    joodie:
    <snip>

    My main concern was that a for loop in C is such an "interesting" mix of actions, it would qualify as a WTF itself.

    I'm not all that surprised that it's allowed in C, but on the other hand, I would not be terribly surprised if a compiler translated

    for (i=0; i <10;i++) {
      i++;
    }

    to (pseudocode):

    int i=0;
    LOOP:
    if (++i < 10) {
      int some_var = i;
      some_var++;
      goto LOOP;

    }
    </snip>



    Well, you should be. Traditional ANSI C doesn't even have loop-level scoping, as far as I remember, and even if it did it wouldn't do it just cause it felt like it. You can do this in Java too; it's often used as an ugly hack by people who're scared of the break statement. There's nothing magic about the iterator.

  • (cs) in reply to igor1960
    Anonymous:

    As to whether I'm really competent or not: It's not you who decides. What matters is how much we are making and are we really happy. You frustration obviously shows that you are not.



    Sorry? Are you MAD? When a plane's guidance software crashes it (the famous example being that Airbus that decided to land itself during an air show) does the programmer sit back and say "ah, but I'm payed a lot and happy"?! No. So why should other incompetents?
  • CuriousJack (unregistered) in reply to dubwai

    Joodie,

    I have seen this 'pattern' in a product everywhere. Initially I thought it was some kind of mistake but turned out to be some kind of 'pattern' used everywhere. Your code snippet is MILES better than the one I encountered.

    The for-loop was replaced with a while() to test the i for the end state value. In between the case are littered with 'inline' code (instead of function they dump the code there directly - save the trouble of passing variables and can share the auto-variables[:'(]).

    I have been chastised for not recognising that i=9 or i=19 is not goto but controlled-goto. Damn must be reading not the right kind of book.

    You have not see the worst yet. There is a situation when there is a next_i value so that after the switch has been executed it is 'popped' off to i.

    Imagine the code spanned something like 300+ lines littered with a few dozen of variables declared in C-style in C++ program. How could anyone follow that. To the silicon brain, it is all 1's and 0's and couldn't careless. To a human, this is bad.

    That is enough to drive any sane man/woman to jump off Golden Gate Bridge.

    Having function between case is a luxury.

     

  • (cs) in reply to Charles Nadolski

    Once when I was 10 or 11 or something, I devised an algorithm to convert Dutch big numbers' zero-counts into words, and back.

    Dutch big number names are quirky. In English, you have Mi, Bi, Tri, etc. In Dutch, you do that, AND alternate between -illion and -illiard, so you get:
    million
    milliard
    billion
    billiard
    trillion
    trilliard
    etc.

    My algorithm involved a division by 3 to get to the Latin number, and even/odd comparisin to get the on/ard. Great fun!

    Ah, to be 10 again.

  • (cs) in reply to dhromed

    OH the shame in not noticing a third page.

  • Dwayne (unregistered) in reply to Pax Diablo
    Anonymous:

    Then why not insert bits of code like (sorry for my C bias):

    switch(0) {
       case 0: {
          putchar ('\n');
       }
    }



    Because you didn't include the default case.
  • Telos (unregistered) in reply to Top Cod3r

    I shit on your abject idiocy.

  • (cs) in reply to dhromed

    dhromed:
    OH the shame in not noticing a third page.

    <FONT face="Courier New" size=2>"thirth".</FONT>

  • (cs) in reply to dubwai

    that would be better written:

    for(int i = 0; i < 100; ) {
        switch(i++) {
            default:
                break;
            case 0:
                doSomething();
                break;
            case 1:
                doSomthingElse();
                break;
            case 2:
                if (doSomethingMore()) {
                    i = 1; // Goto 1
                }
                break;
            case 3:
                if (isFoo()) {
                    i = 2; // Goto 2
                }
                break;
        }
    }
    
  • (cs) in reply to dubwai

    Poop, it orgot my quote:

     

    dubwai:
    joodie:
    dubwai:

    Anonymous:
    This is probably the best way yet to reinvent line numbers and stuff them into a reasonably modern programming language.

    It's not shown in this example but it's also a way to simulate gotos in languages that do not support them.



    show me how, and maybe I'll believe you.

    You might not believe me even if I show you how?

    for (int i = 0; i < 100; i +=10; i++){
        switch (i) {
            default:
               break;
            case 0:
               doSomething();
               break;
            case 10:
               doSomthingElse();
               break;
            case 20:
               if (doSomethingMore()) {
                   i = 9; // goto 10
               }
               break;
            case 30:
               if (isFoo()) {
                   i = 19; // goto 20
               }
               break;
        }
    }

  • James Matthews (unregistered) in reply to CuriousJack
    Anonymous:

    Joodie,

    I have seen this 'pattern' in a product everywhere. Initially I thought it was some kind of mistake but turned out to be some kind of 'pattern' used everywhere. Your code snippet is MILES better than the one I encountered.

    The for-loop was replaced with a while() to test the i for the end state value. In between the case are littered with 'inline' code (instead of function they dump the code there directly - save the trouble of passing variables and can share the auto-variables[:'(]).

    I have been chastised for not recognising that i=9 or i=19 is not goto but controlled-goto. Damn must be reading not the right kind of book.

    You have not see the worst yet. There is a situation when there is a next_i value so that after the switch has been executed it is 'popped' off to i.

    Imagine the code spanned something like 300+ lines littered with a few dozen of variables declared in C-style in C++ program. How could anyone follow that. To the silicon brain, it is all 1's and 0's and couldn't careless. To a human, this is bad.

    That is enough to drive any sane man/woman to jump off Golden Gate Bridge.

    Having function between case is a luxury.

     



    While-case isn't a particular problem, it's a reasonable way to hard-code state machines.  Once you know it's a state machine, it's easy to reconstruct the automaton and figure out what the code does.  Good practice would include a comment saying "this is a state machine", of course.

    For-case is a bad way to simulate line numbers and goto in languages that don't have them.

  • TiS (unregistered)

    Hmm...

    I don't want to act smart, but about B's code piece - in some cases the EXACT code might be quite a good idea. When? In parallel programming.

    The compilers and operationg systems have several methods to make code parallel and allow it to be splitted into more than one processor. One of the most common is splitting iterations of loop commands into several processors.

    The form it's written is kind of strange and I really doubt it's what author meant, BUT - there is a possibility that every function can be executed simultaneously regardless the order (it is unlikely though) - then such a code might be the easiest way to be run parallel.

    As I mentioned - not very likely though. Well - WTF...

  • SourceError (unregistered) in reply to skicow
    skicow:

    <font face="Arial Unicode MS">Exhibit B doesn't even make sense! The programmer knows enough to procedurlize (sp?) their code, and knows how to use a for loop and a switch statement....so WhyTF would they do this?! is it so that there is a slight - and I mean bloody slight - pause between the calling of the individual procs? That would explain the:<o:p></o:p></font>

    <font face="Arial Unicode MS">case 1:
            break;<o:p></o:p></font>

    <font face="Arial Unicode MS">This code just makes me cry. [:'(]<o:p></o:p></font>

     <o:p></o:p>



    The case 1: break; is the only thing that makes sense there.  It's so the compiler will use a tableswitch opcode instead of the slower lookupswitch. :)

    -S
  • Helen (unregistered)

    LOL.  I've actually seen something very similar to exhibit A in one of my coworker's code before!  (He even forgot to add the "break" line and he was wondering why there's a bug in his code.)  He left the company after two months saying he's going to start up his own company...  -_-

  • Anonymous (unregistered) in reply to Top Cod3r

    you're joking, right? multithreading initialization for (what should be) constants? and case 1: break; would have no effect on multithreading. plus, doing it the way mentioned here makes it un-multithreadable

  • Douglastab (unregistered)

    pharmacie en ligne france fiable http://kamagraenligne.com/# pharmacie en ligne fiable

Leave a comment on “Switched on Loops”

Log In or post as a guest

Replying to comment #:

« Return to Article