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

     





  • @blatant_mcfakename said:

    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.



  • @blatant_mcfakename said:

    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.



  • 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.



  •  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.



  • @SuperAnalyst said:

     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?



  • 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 ...



  • @PJH said:

    @SuperAnalyst said:
     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.



  • 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.



  • @savar said:

    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



  • 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.



  • 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.



  • @toth said:

    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.



  • @tgape said:

    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".



  • @toth said:

    figger

    I don't think that's an actual word.



  • @derula said:

    @toth said:
    figger

    I don't think that's an actual word.

    Whut in tarnation are y'all talkin' about?

     



  • @DaveK said:

    @derula said:
    @toth said:
    figger

    I don't think that's an actual word.

    Whut in tarnation r y'all talkin' aboot?

    FTFY



  • @derula said:

    @DaveK said:
    @derula said:
    @toth said:
    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.



  • @derula said:

    @toth said:
    figger

    I don't think that's an actual word.

    Git over here, you varmint, y'all be askin' for a good whuppin'.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.