• (cs)

    Is the first one Java?  That would just be:

    String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday""Saturday", "Sunday"};

    C# probably have a similar construct.  And I can't remember if C++ does.  WTF!

  • (cs)

    >>Shudder<<

    That second block of code reminds me of a project I used to work on at a previous employer.  The code was littered with non-sensical varaible names, while loops that included the comma operator in the conditional statement, and a big for loop that looked exactly like this.

  • Michael Link (unregistered)

    Exhibit A is just dumb but doesn't cause much headache. I would send the developer to some training. Exihibit B is much more worse because probably the whole application is tangled in this for-switch-***. I never would allow the developer of this beast to write one line of code anymore until he has undergone some kind of Cobol exorcism.

  • (cs)

    Classic examples of people overcomplicating really simple things. 

  • (cs)

    Thats what happens when you pay people 'by the line'.
    But a real moocher would of thrown in some useless comments

    //Case 0
    case
    0:
    //this is setting days[0]="Monday"
    //and then breaking out of the switch
    daysIdea [I] = "Monday";
    break;
    I assume theIdea [I]  is in place of the [i], is that supposed to be there or a bug?

  • (cs) in reply to Jeff S

    The second one looks almost like a state machine but what's the deal with:

    case 1: break;

    Why bother putting that in there?  Must have been maintained by someone who thought "I'm not messing with this piece of ***."

  • (cs)

    <FONT face="Courier New">for( int i = 0; i < 7; i++ ) {
      switch(i) {
        default:
        case 0:
    </FONT>

    It's good that the developer included the <FONT face="Courier New">default:</FONT> in case the <FONT face="Courier New">for</FONT> loop went crazy and decided to go outside its bounds... [;)]

  • Anonymous (unregistered)

    The real WTF here is that he didn't unroll that loop. It should have been like this:

    String[] days = new String[7];
    i = 0;
    switch(i) {
      default:
      case 0:
        days[i] = "Monday";
        break;
      case 1:
        days[i] = "Tuesday";
        break;
      case 2:
        days[i] = "Wednesday";
        break;
      case 3:
        days[i] = "Thursday";
        break;
      case 4:
        days[i] = "Friday";
        break;
      case 5:
        days[i] = "Saturday";
        break;
      case 6:
        days[i] = "Sunday";
        break;
    }
    i = 1;
    switch(i) {
      default:
      case 0:
        days[i] = "Monday";
        break;
      case 1:
        days[i] = "Tuesday";
        break;
    

    etc.

  • (cs)

    Actually, this would be a great idea for creating a code bloating script.  Just turn any set of sequential statements into a for/switch.  CA$H UP THE A$$

  • Andrew W (unregistered) in reply to dubwai

    Dude, even C has a way of setting all of the variables! I typically type it all the way out, for readability (when you're up late at night, sometimes you can do very weird things in code).

    Think char ** argv in the "main" of a C program.

    argv[1]="Monday"; argv[2]="Tuesday"..

  • Andrew W (unregistered) in reply to Anonymous

    are you retarded? That would be the most code bloat I've ever seen. I hope you were being sarcastic...

  • (cs) in reply to Sweets

    Sweets:
    Thats what happens when you pay people 'by the line'.

    Has anyone ever really paid people 'by the line' of code?  The concept keeps getting mentioned (I assume facetiously) but I've never heard of it in real life.

  • (cs) in reply to Bustaz Kool

    Bustaz, you beat me to it.  As I read this thread, the same exact thought crossed my mind.  I'm curious about that also.

  • (cs) in reply to Bustaz Kool

    Bustaz Kool:
    Has anyone ever really paid people 'by the line' of code?  The concept keeps getting mentioned (I assume facetiously) but I've never heard of it in real life

    I've know that a lot of managment types think it's a reliable way to measure developer productivity.  More code == bigger raises and more clout.  It might explain all the copy / paste code I see.

  • StarLite (unregistered) in reply to Bustaz Kool

    I sure as hell hope not, will prolly make for useless and bloated code :S

  • (cs) in reply to Bustaz Kool
    Bustaz Kool:

    Sweets:
    Thats what happens when you pay people 'by the line'.

    Has anyone ever really paid people 'by the line' of code?  The concept keeps getting mentioned (I assume facetiously) but I've never heard of it in real life.

    I give you bulk rate on code!  Very cheap!  Free <FONT color=#000000>Viagr</FONT>@ too!

  • Top Cod3r (unregistered)

    You have to look at this code in context.  By using a loop, the developer is multi-threading the process, resulting in a more performant and scalable solution.  Another advantage of Exhibit B is it allows new steps to be inserted into the process by simply creating another number in the case statement.  Personally I would have numbered the steps 10, 20, 30, and so on.  That way if you need to put a new step in you could use 25 for example.  Its all about writing maintainable code.

  • (cs) in reply to dubwai
    dubwai:

    Is the first one Java?  That would just be:

    String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday""Saturday", "Sunday"};

    C# probably have a similar construct.  And I can't remember if C++ does.  WTF!



    Yes in C you can do:
    char *szDaysArray[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};

    and in pure C++ using the STL you have to use an itermediate array because you can't directly initialize a vector. So in C++ it would be:

    std::string daysArray[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
        std::vector<std::string> vStr(daysArray,daysArray+7);

    I'm not sure what C# has either but this is a definite WTF in just about every language I've ever used. It's a shame it seems to be such common practice.
    </std::string>
  • (cs) in reply to Top Cod3r
    Anonymous:

    You have to look at this code in context.  By using a loop, the developer is multi-threading the process, resulting in a more performant and scalable solution.  Another advantage of Exhibit B is it allows new steps to be inserted into the process by simply creating another number in the case statement.  Personally I would have numbered the steps 10, 20, 30, and so on.  That way if you need to put a new step in you could use 25 for example.  Its all about writing maintainable code.



    I recognize this too as sarcasm.  Perhaps I can develop the ability to recognize it after all!  There IS hope for me yet!


  • (cs) in reply to Blue

    And I can now quote people in FireFox!  YAAAAAAY!

  • (cs)

    This makes me think of Duff's Device:
    http://www.lysator.liu.se/c/duffs-device.html
    I suppose the "For-Case" paradigm is something like the opposite of Duff's Device.

  • (cs) in reply to Top Cod3r
    Sassy Sally:

     Personally I would have numbered the steps 10, 20, 30, and so on.  That way if you need to put a new step in you could use 25 for example.  Its all about writing maintainable code.


    Ah, that takes me back to my Apple ][ days.  Programs in BASIC used line numbers, because there was no file editor, just a command line, and each line typed in was inserted by its line number.  By convention, line numbers went in steps of 10, so you could add more lines later on.  If you wanted to add more than 9 lines between two old lines . . .  Why would anybody want to ever do that?

  • (cs) in reply to kwatz
    kwatz:
    This makes me think of Duff's Device:
    http://www.lysator.liu.se/c/duffs-device.html
    I suppose the "For-Case" paradigm is something like the opposite of Duff's Device.


    Duff's device has to be one of the coolest abuses of C of all time. I use it for copying buffers of data from one file to another. This For-Case thing is something I've never seen in any real life code but from all the examples people post somebody is obviously writing this stuff. What is even more amazing is that they actually continue having jobs that involve writing code. Cross your fingers and hope they were some first year analyst fresh out of college that was instructed as to the correct way of doing these things and not some ten year VB veteran coding in the cube next to you.
  • Alan (unregistered) in reply to Top Cod3r

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

  • Ron (unregistered)

    If you want to see something awesome, check out Duff's Device:

    http://catb.org/~esr/jargon/html/D/Duffs-device.html

    An awesome piece of c hackery on top of it, implementing coroutines:

    http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

    And then protothreads, better than sharks with laser beams on their heads:

    http://www.sics.se/~adam/pt/

     

  • (cs) in reply to Alan

    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.

  • (cs)

    This is without a doubt the most idiotic piece of code I've ever seen.

    Please please please tell me this is a joke.

  • (cs) in reply to dubwai
    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.

  • (cs)

    :S  Ug.  Makes me want to upchuck seeing this kind of code.

  • (cs) in reply to cm5400
    cm5400:
    :S  Ug.  Makes me want to upchuck seeing this kind of code.


    Grrrr... Should be able to edit the posts...  The Emoticon does not work for me... Could be Firefox?  Have to try in (gulp) IE.
  • Gary Henson (unregistered) in reply to joodie

    <font size="2">With regards to sample A, it's not a joke, it is Java, and the problem was largely due to the fact the developer had just been sent on training. He'd apparently just seen how "useful" case statements could be, and decided that maybe he should be using more of them...

    Thankfully I worked for that company 4 years ago, and haven't seen anyone do this since.</font>

  • Andy (unregistered) in reply to dubwai
    dubwai:

    Is the first one Java?  That would just be:

    String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday""Saturday", "Sunday"};

    C# probably have a similar construct.  And I can't remember if C++ does.  WTF!

    C# syntax is the same as java....

    -Andy<FONT size=2>

    </FONT>
  • (cs) in reply to joodie
    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;
        }
    }

  • (cs) in reply to dubwai
    dubwai:

    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;
        }
    }


    That's terrible, but it made me smile. :-) Is modifying the iterator count during a switch even allowed/documented in C?

    But, I believe you. Even so, I don't know of any language that does a C-like switch (and no exceptions), and doesn't have a goto, which would make it completely useless except for really pathological cases.

  • (cs)

    <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:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><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>

  • igor1960 (unregistered) in reply to dubwai

    The problem with that thread is: looks like you all here has nothing else to do but to demonstrate how stupid others are.

    I'm not new in programming and trust me put alot of stupid things in my code and was paid "by lines" and/or "by time" and/or "by output" and/or "by speed" and etc. during my career.

    I understand the reason you may laugh at presented samples, but trust me -- your laugh has nothing to do with the real "brush fire extinquishing" you have to deal with sometimes in your everyday programming life. So, kindly excuse me, but there is no reason to joke about somebodies bad programming practices.

    On the other hand and being serious here: whatever language you choose -- I agreee you saved somespace in not typing that "laughable" switch statement and not confusing others, but instead used specific language/compiler feature and just use declarative approach -- end result is still the same: "During runtime equivalent of that long case was executed"... So, what exactly are you laughing about?!...

  • (cs) in reply to joodie

    Of course it's allowed. Is it a good idea? Not usually -- it's normally a side-effect of something else (generally using assignment "=" instead of equality comparator "=="), but I'd be willing to bet that just about everybody has modified an iterator variable in a loop in every language that has 'em at one time or another.

  • Top Cod3R (unregistered) in reply to igor1960
    Anonymous:

    I understand the reason you may laugh at presented samples, but trust me -- your laugh has nothing to do with the real "brush fire extinquishing" you have to deal with sometimes in your everyday programming life. So, kindly excuse me, but there is no reason to joke about somebodies bad programming practices.

    Are you the author of Exhibit B?

  • (cs) in reply to Top Cod3R
    Anonymous:

    Are you the author of Exhibit B?

    Yeah, hit a nerve I think.

  • (cs) in reply to joodie

    joodie:

    That's terrible, but it made me smile. :-) Is modifying the iterator count during a switch even allowed/documented in C?

    But, I believe you. Even so, I don't know of any language that does a C-like switch (and no exceptions), and doesn't have a goto, which would make it completely useless except for really pathological cases.

    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.

  • (cs) in reply to Stan Rogers
    Stan Rogers:
    Of course it's allowed. Is it a good idea? Not usually  <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;

    }

    either.

    god I hate the "rich-text" editor in this forum software.

    </snip>

  • (cs) in reply to igor1960

    Anonymous:
    I understand the reason you may laugh at presented samples, but trust me -- your laugh has nothing to do with the real "brush fire extinquishing" you have to deal with sometimes in your everyday programming life. So, kindly excuse me, but there is no reason to joke about somebodies bad programming practices.

    Apparently you equate 'brush fire estinguishing' with garbage code.  The code given was clearly not a fix.  It takes planning and a lot of work to construct such a foolish construct.

    Anonymous:
    On the other hand and being serious here: whatever language you choose -- I agreee you saved somespace in not typing that "laughable" switch statement and not confusing others, but instead used specific language/compiler feature and just use declarative approach -- end result is still the same: "During runtime equivalent of that long case was executed"... So, what exactly are you laughing about?!...

    The complete incompentence of the developer(s) who wote this.  I spend a lot of time fixing code that is as screwed up as this or worse.  I get to vent some frustration.  Sorry you feel threatened by this.  It kind of makes you look like an incompetent.

  • (cs) in reply to igor1960
    Anonymous:
    I understand the reason you may laugh at presented samples, but trust me -- your laugh has nothing to do with the real "brush fire extinquishing" you have to deal with sometimes in your everyday programming life. So, kindly excuse me, but there is no reason to joke about somebodies bad programming practices.


    Sure we've all done stupid sh!t in our code. I hate going back and looking at code I wrote when I first started, it's embarrasing. We all start somewhere. However roasting bad code in public forums does serve a purpose. People can come and laugh and also learn what not to do. I like coming here and seeing mistakes(especially non-obvious ones) in languages that I don't know well. So when I do use those languages I don't make the same mistakes.
  • (cs) in reply to dubwai
    dubwai:

    joodie:

    That's terrible, but it made me smile. :-) Is modifying the iterator count during a switch even allowed/documented in C?

    But, I believe you. Even so, I don't know of any language that does a C-like switch (and no exceptions), and doesn't have a goto, which would make it completely useless except for really pathological cases.

    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.



    Ok, but who the hell would use GOTOs for that kind of code? I mean, OK, if you don't have exceptions, by all means use gotos if you need to break out of a stack, but jeez, even if you don't have a goto, there's still break and continue in java...

  • igor1960 (unregistered) in reply to dubwai

    The only reason I've replyed was that as I undertsood the original joke placed at the beginning of the thread was to show that government workers are on break 7 days a week. Now, somehow thread moved into discussion of "programming practices".

    Now, in your next responce you may name me not just being incompetent, but rather "being incompetent government worker". And I'm not threatened by this. Please, continue.

    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.

  • (cs)

    I realized that (it seems) nobody has congratulated Alex on the excellent name for today's post.  Good job.  [:)]

  • (cs) in reply to igor1960
    Anonymous:

    You frustration obviously shows that you are not.

    I'm happy about somethings and not about others.  I'm rather unhappy that the MO for IT managment these days is to outsource (offshore and onshore) major developement to incompetent developers and then make full time developers maintain it.

  • (cs) in reply to joodie

    joodie:

    Ok, but who the hell would use GOTOs for that kind of code? I mean, OK, if you don't have exceptions, by all means use gotos if you need to break out of a stack, but jeez, even if you don't have a goto, there's still break and continue in java...

    No argument here.

  • igor1960 (unregistered) in reply to dubwai

    Here I agree -- MO screews everything.

    However, your criticism an claim that original code was stupid hypothetically may not be the case. In fact it may not be as stupid as it looks like.

    Just imagine that String class is not really string class as we understand it -- maybe it's more complicated then just string and which is more important it just can't be initialized by just "" object (let's assume it's a structure having 100 members that are filled based on time of day passed).

    Or, In addition -- that class could be implemented just as a singleton (or maybe it just a proxy to singleton) -- So, any declarative initialization described as a "proper solution" will initialize real singleton to "Sunday"? Isn't that what we want?

    So, my point is: it's always much efffortless to criticize -- this criticizm doesn't pay your bills though.

  • Chep (unregistered) in reply to joodie
    Is modifying the iterator count during a switch even allowed/documented in C?
    It breaks MISRA-C:2004 rule 13.6 (which makes :1998 rule#67 required), but otherwise in classic C or classic C++, it only raises seasoned eyebrows.

    Here's a nice one in CORBA:

    IDL:

    struct Foo {
       unsigned long dmy;
        // that's it
    };
    interface Bar {
        void fred(in Foo foo);
    };

    server-side:

    class SomeBarImpl: public POA_Bar, some_more_junk {
    public:
         void fred(const Foo& foo) throw (CORBA::SystemException) {
               int day = foo.dmy & 0x000000FF;
               int month = (foo.dmy & 0x0000FF00) >> 8;
               int year = (foo.dmy & 0xFFFF0000) >> 6;
    }
    };

    client side:

    void someCaller(Bar* bar, int day, int month, int day) {
         Foo foo;
         foo.dmy = day | (month << 8) | (year << 16);
    // there are pipe (OR) signs but the WTF forum eats them?
         bar.fred(foo);
    }

    at least it's both Y2K and Y10K compliant.

Leave a comment on “Switched on Loops”

Log In or post as a guest

Replying to comment #31674:

« Return to Article