This is how you parse command-line arguments!



  •  Let's play count-the-wtfs! This code sample is heavily anonymized, so I'll give you some freebie WTFs that I removed when anonymizing:

    1. There is exactly one comment. Although poorly worded, it's actually helpful, so I suppose this is a "minor wtf". 
    2. The indenting was quite bad...I've cleaned it up enough that it should be readable.


    Apologies in advance if this gets mangled by the forum. I can't remember offhand how to code-ify this.

     

     

    for (int i = 0; i <argc; ++i) {

        if (strcmp(argv[1], "-m")) {
            errorCode = log_error_message(...);
            help();
            exit(0);
           
        } else if ( i == 2 ) {
            if (!in){
                errorCode = log_error_message(...);
                help();
                exit(0);
            }

        } else if( strcmp( argv[3], "-n" ) ) {
            errorCode = log_error_message(...);
            help();
            exit(0);

        } else if( i == 4 ) {
            std::string str = argv[4];
            if (strcmp(argv[4], "-1") != 0){
                for (int k = 0; k < (int)(str.length()); k++){
                    if (!isdigit(str[k])) {
                        errorCode = log_error_message(...);
                        exit(0);
                    }
                }
            }
            num = atoi(argv[4]);

        } else if( i == 5 ) {
            if( argv[5] != NULL && strcmp( argv[5], "-s" ) ) {
                errorCode = log_error_message(...);
                help();
                exit(0);
            }

        } else if( i == 6 ) {
            std::string str = argv[6];
            for (int k = 0; k < (int)(str.length()); k++){
                if (!isdigit(str[k])) {
                    errorCode = log_error_message(...);
                    exit(0);
                }
            }

            // These actually had useful variable names until I anonymized.
            int_var1 = atoi(argv[6]);
            int_var2 = (int)floor( 1000000.0 / int_var1 );
            // The only original comment in the entire thing, redacted
            int_var3 = 1000000 - ( int_var1 * int_var2 ) + int_var1;

        }
    }



  •  for...case is quite possibly my favourite anti-pattern.


  • Discourse touched me in a no-no place

    It would appear to be somewhat strict about the order and presence of certain options.



  • @davidrhoskin said:

     for...case is quite possibly my favourite anti-pattern.

     

    But this isn't even a true for-case loop, it's even worse:

    for ( i = 0; i < n; i++ ) {
      if ( thing[1] == foo ) {
        stuff();
      }
      if ( thing[2] == bar ) {
        morestuff();
      }
    }

    I don't know what to call this...



  • It's even better than for-case: it's a for-case where the constant constraints are checked at each iteration! It's a safer for-case!

    Let's not forget returning 0 (which is supposed to indicate success) when things go wrong.



  • Step 1: Investigate where this programmer went to college.

    Step 2: Go to that college and make note of what the hell made one even think in this bizarre, backwards way... was it easy professors? Chemicals in the water? Tolerance to cheating?

    Step 3: No matter what the reason, do everything within your power to get the school to become unaccredited.



  •  No, THIS is how you parse command-line arguments!  With your bare fists!  Like Chuck Schneier!

     

    Wait, what? 



  • @RHuckster said:

    Step 1: Investigate where this programmer went to college.

    You may have an unfounded assumption here.  Do we have an indication this programmer attended any school at all?  In any event, it looks like herd-of-monkeys code to me.  If it is, then the real question is, 'is programmer the right term?'.  For all we know, the individual could have simply copied and pasted from working code until it got something that compiled and performed close enough to correctly for its tastes.

    Disclaimer: I realize it is possible the individual attended an 'institute of higher learning'.  Statistically speaking, it's a virtual guarantee that there's a few whose 'computer sciences' curriculum would be improved simply by taking away the instructors and computers, but giving the students good books about programming.  (Normally, this would be a really bad thing - but depending on the instructors, the chosen texts, and the computers provided, it *could* be an improvement for some, and I'm suggesting that it's likely that remote tail of the probability curve is actually represented.)  It's also possible that the individual was at a particular school in much the same way about half a dozen homeless people were at my alma mater when I was in attendance...  And, it's possible the individual attended a good school, but refused to learn.    Mind-altering chemicals could have been involved.  I probably missed at least one possibility.



  • @RHuckster said:

    Step 1: Investigate where this programmer went to college.

    Step 2: Go to that college and make note of what the hell made one even think in this bizarre, backwards way... was it easy professors? Chemicals in the water? Tolerance to cheating?

    Step 3: No matter what the reason, do everything within your power to get the school to become unaccredited.

     

    No, no. Nuke the school from orbit. It's the only way to be sure.



  •  For the record, yes the person who coded this went to (and passed) a university program for computer programming or engineering. This person is apparently a "senior engineer" here. It's also quite possibly the same university I attend currently (not to worry, they're one of the best for Computer Science), so I'd probably best not nuke-from-orbit.

     The only thing that makes this even remotely acceptable is that this code is part of a project that was thrown together in 4 months from design to release. Sadly, they've only increased their technical debt since then.



  • @davidrhoskin said:

    for...case is quite possibly my favourite anti-pattern.
     

    And "anti-pattern" is quite possibly my least favorite neologism.

    The opposite of a pattern is no pattern at all, dammit.



  • @Zylon said:

    @davidrhoskin said:

    for...case is quite possibly my favourite anti-pattern.
     

    And "anti-pattern" is quite possibly my least favorite neologism.

    The opposite of a pattern is no pattern at all, dammit.

    anti is a prefix that means "against," not "antonym of"

    So these things are against patterns. They defy patterns, similar to how the Antichrist fights with the Christ.



  • @DaveK said:

     No, THIS is how you parse command-line arguments!  With your bare fists!  Like Chuck Schneier!
    I think you must have him confused with Bruce Norris.  Chuck Schneier is the guy who roundhouse kicks 2048-bit encryption for being too easy.



  • I love the mix of using the str*() functions and using (somewhat pointlessly) std::strings.



  • @bstorer said:

    Chuck Schneier is the guy who roundhouse kicks 2048-bit encryption for being too easy.
     

    Chuck Schneier enumerated all possible 2048 bit hashes off the top of his head. Twice.


Log in to reply
 

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