The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

c++ flow control - did I miss something?

Last post 02-07-2010 8:04 PM by toth. 19 replies.
Page 1 of 1 (20 items)
Sort Posts: Previous Next
  • 02-02-2010 7:44 AM

    c++ flow control - did I miss something?

    Is the coder who wrote the following code (which I found earlier tucked away in a project) a genius who knows something I don't, or is it just as pointless as I suspect?

     

    for(int i=1; i<=2; i++)
    {
     switch(i)
     {
       case 1:
         /* do something */
         break;
       case 2:
         /* do the next thing */
         break;
     }
    }

     

    Filed under:
  • 02-02-2010 7:53 AM In reply to

    Re: c++ flow control - did I miss something?

  • 02-02-2010 7:53 AM In reply to

    • derula
    • Top 25 Contributor
    • Joined on 06-15-2007
    • Germany
    • Posts 862

    Re: c++ flow control - did I miss something?

    blatant_mcfakename:
    Is the coder who wrote the following code [...] a genius who knows something I don't [...]?
    Yes! You obviously don't know about the famous for-case paradigm!

    Also, maybe he refers to i in the deleted code. That would mean he avoids hard-coded values there.
    You can now help me balance the tag cloud.
  • 02-02-2010 7:55 AM In reply to

    Re: c++ flow control - did I miss something?

    blatant_mcfakename:
    Is the coder who wrote the following code a genius who knows something I don't?
     

    No.

    He is an idiot who is unable to do a programming job.

    — Flurp.
  • 02-02-2010 9:04 AM In reply to

    Re: c++ flow control - did I miss something?

    If the code referred to i at any point apart from using it for loop control and in the switch then I could've possibly figured out what he was doing. The code doesn't do *anything* in either of the blocks which can't just be done sequentially and it's...well... just odd basically.

     It's still bugging me trying to figure out who wrote this and what was going through their mind at the point they did it.

  • 02-02-2010 10:11 AM In reply to

    Re: c++ flow control - did I miss something?

     Maybe it's the end result of a series of revisions by different people? For example,maybe this loop was much longer and more complicated, but over time, it was simplified due to business logic until it got down to this.

  • 02-02-2010 10:24 AM In reply to

    • PJH
    • Top 10 Contributor
    • Joined on 02-14-2007
    • Newcastle, UK
    • Posts 1,253

    Re: c++ flow control - did I miss something?

    SuperAnalyst:
     Maybe it's the end result of a series of revisions by different people? For example,maybe this loop was much longer and more complicated, but over time, it was simplified due to business logic until it got down to this.
    If that's the case, then why did the last person 'simplifying' it not simplify it even further?
    Abstinence makes the Church grow fondlers.

    - unknown
  • 02-02-2010 2:52 PM In reply to

    Re: c++ flow control - did I miss something?

    Several years ago (think VB3), I once wrote something which I call "brute force equality." I don't recall the exact syntax, but it essentially looked like this:

    For i = 1 To 3
    If i = iFoobar Then
    ' lots of code using i
    End If
    Next

     I don't remember how I ended up with this code, however. Maybe that 500-SLOC loop might have had something to do with it ...

    Filed under:
  • 02-02-2010 4:56 PM In reply to

    Re: c++ flow control - did I miss something?

    PJH:
    SuperAnalyst:
     Maybe it's the end result of a series of revisions by different people? For example,maybe this loop was much longer and more complicated, but over time, it was simplified due to business logic until it got down to this.
    If that's the case, then why did the last person 'simplifying' it not simplify it even further?

    Maybe he was in a programmer's union and it wasn't his stinkin' problem.

  • 02-03-2010 12:49 PM In reply to

    Re: c++ flow control - did I miss something?

    All I can think is that it was used as a really crazy 'goto' mechanism. E.g. within each case, you could set i to some other value to repeat steps or skip steps. That would be awesomely evil, in fact. I imagine there is something like that inside SSDS.
  • 02-03-2010 12:53 PM In reply to

    Re: c++ flow control - did I miss something?

    savar:
    All I can think is that it was used as a really crazy 'goto' mechanism. E.g. within each case, you could set i to some other value to repeat steps or skip steps. That would be awesomely evil, in fact. I imagine there is something like that inside SSDS.
     

    thou shalt not mention ssds in my pressence. - the 11th commandment

  • 02-03-2010 12:55 PM In reply to

    Re: c++ flow control - did I miss something?

    There is exactly one "okay" use case for that construct. Test code. When you have a suite of test functions that are "mostly" independent, having a test program with a main() with that loop can be useful for selectively calling the functions. The loop min value and max value should be variables that are set from command line and default to the entire list of cases. So in the general case you run the program with no parameters and all the test functions run. When you are having trouble with test function 12, you run the program setting the min and max value of i to 12 and it just runs function 12.

     Of course, since I'm talking about test code, you should never see that construct in your production code.

  • 02-04-2010 5:35 PM In reply to

    • toth
    • Top 500 Contributor
    • Joined on 06-23-2009
    • Posts 57

    Re: c++ flow control - did I miss something?

    Now, when people talk derisively about the FOR-CASE paradigm, are they only referring to literal for() loops, or do foreach() loops follow under that umbrella, too? Because it seems like a FOREACH-CASE paradigm is actually ok. Of course, that may mean I'M TRWTF.
  • 02-05-2010 8:01 AM In reply to

    Re: c++ flow control - did I miss something?

    toth:
    Now, when people talk derisively about the FOR-CASE paradigm, are they only referring to literal for() loops, or do foreach() loops follow under that umbrella, too? Because it seems like a FOREACH-CASE paradigm is actually ok. Of course, that may mean I'M TRWTF.

    It depends.  If you're programming in Perl, and the foreach loop looks like this:

    foreach(my $i=1; $i<=2; $i++)
    {
     switch($i)
     {
       case 1 {
         # do something
         next; }
       case 2 {
         # do the next thing
         next; }
     }
    }
    Then not only is it wrong, but you're doing it more wrong than most people here could imagine (before this post, of course).  Same goes for:
    foreach $i (1..2)
    {
     switch($i)
     {
       case 1 {
         # do something
         next; }
       case 2 {
         # do the next thing
         next; }
     }
    }
    But, if the elements and/or order are uncertain, I think foreach-case may be ok.

    tharpa:
    The "Reveal Codes" feature alone makes it better than Word.

    QFT.


  • 02-05-2010 11:26 PM In reply to

    • toth
    • Top 500 Contributor
    • Joined on 06-23-2009
    • Posts 57

    Re: c++ flow control - did I miss something?

    tgape:

    It depends.  If you're programming in Perl, and the foreach loop looks like this:

    foreach(my $i=1; $i<=2; $i++)
    {
     switch($i)
     {
       case 1 {
         # do something
         next; }
       case 2 {
         # do the next thing
         next; }
     }
    }
    Then not only is it wrong, but you're doing it more wrong than most people here could imagine (before this post, of course).  Same goes for:
    foreach $i (1..2)
    {
     switch($i)
     {
       case 1 {
         # do something
         next; }
       case 2 {
         # do the next thing
         next; }
     }
    }
    But, if the elements and/or order are uncertain, I think foreach-case may be ok.
    That's what I figger. When I said "foreach loops" I meant "loops over an enumerable object that isn't (necessarily) a range of numbers".
  • 02-06-2010 5:36 AM In reply to

    • derula
    • Top 25 Contributor
    • Joined on 06-15-2007
    • Germany
    • Posts 862

    Re: c++ flow control - did I miss something?

    toth:
    figger
    I don't think that's an actual word.
    You can now help me balance the tag cloud.
  • 02-06-2010 12:21 PM In reply to

    Re: c++ flow control - did I miss something?

    derula:
    toth:
    figger
    I don't think that's an actual word.

    Whut in tarnation are y'all talkin' about?

     

    (USER WAS BANNED FOR THIS POST)


  • 02-06-2010 1:49 PM In reply to

    • derula
    • Top 25 Contributor
    • Joined on 06-15-2007
    • Germany
    • Posts 862

    Re: c++ flow control - did I miss something?

    DaveK:
    derula:
    toth:
    figger
    I don't think that's an actual word.
    Whut in tarnation r y'all talkin' aboot?
    FTFY
    You can now help me balance the tag cloud.
  • 02-07-2010 5:47 PM In reply to

    Re: c++ flow control - did I miss something?

    derula:
    DaveK:
    derula:
    toth:
    figger
    I don't think that's an actual word.
    Whut in tarnation r y'all talkin' aboot?
    FTFY
     

    I didnt think DaveK was Canadian. Unless you're still being possessed by SpectateSwamp, derula.

    "YO DAWG, I HERD YOU LIKE SHIPS, SO WE PUT A SHIP IN YO SHIP SO YOUR CARGO IS MAXED AND YOU CANT MOVE FOR SHIT LOL" - dhromed

    Not Found: Forum Not Found

    The forum you requested does not exist.

  • 02-07-2010 8:04 PM In reply to

    • toth
    • Top 500 Contributor
    • Joined on 06-23-2009
    • Posts 57

    Re: c++ flow control - did I miss something?

    derula:
    toth:
    figger
    I don't think that's an actual word.
    Git over here, you varmint, y'all be askin' for a good whuppin'.
Page 1 of 1 (20 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems