2 ms·
i disagree with the sentiment and preferences expressed in your comment. the code on display is at the sweet spot of complexity given the scope of task at hand
by contras1970 9y ago
i disagree with the sentiment and preferences expressed in your comment. the code on display is at the sweet spot of complexity given the scope of task at hand. your preferences might lead to code with lower complexity of any given expression, but there would be a higher number of expressions, higher number of statements, and higher number of names. that would mean higher complexity of the full code. should we ban multiplication on the basis that X times Y is the same as X plus X plus X...?
> Why pre-inc/decrement?
as opposed to what? nothing? clearly, the code needs to advance the iterators before dereferencing.
> Why the +1 on the for?
because it's interested in the option name as opposed to the leading dash: argv[0][0] is '-' (see the while above), the switch is looking at argv[0][1].
> Did he actually get the precedence of the operators correct?
not sure what this is about, the only involved expression has explicit parentheses (out of necessity).
> Okay, it looks like argc and argv will be mangled if you actually want to do anything else with them.
yes... and? mutating a local int and a local pointer is bad? how would you go about this without mutating an iterator?
> I see comparing to '\0' with no count limit--is there a buffer overrun lurking here?
nope, it's an array of null-terminated strings.
> Does that error handling actually work--that loop can't exit with that condition unless the second clause does something.
i do not understand what you're pointing at here.
> I'd assign argc and argv to something else so if I need them later for something they are in their original state.
consider they're already local names, and YAGNI. do alias them if you need them later, not because you might one day.
> I'd give myself a variable that points to each individual argv on each iteration, and I'd call it something.
you would give yourself a possible bug and an obligation to keep the two things in sync.
> I'd make the error handling more explicit so if I had to add another case later I wouldn't have to rack my brain about whether the error gets handled.
what is more explicit than
printf("Illegal option %c\n", *s);
?
i'd really love to see your preferred version of the snippet from TFA. i posit it'd be two to three times as long, and its total complexity would be similarly increased.
- tomsmeding 9y agoI sort-of agree that the code in the article is not really future-expansion-proof, and in fact, it's not really incompetent-programmer-proof. In the enterprise, you'd probably write incompetent-programmer-proof code, even if you're very competent yourself, just to prevent trouble later. Kernighan and Ritchie did not need to bother with less profient programmers maintaining their code. They wrote what they and their friends understood, and apparently that was this. So in short, the GP (bsder) does have a point.
- bsder 9y ago> Why pre-inc/decrement? No, why "pre-" instead of "post-"? Is that a real requirement , or is this an old C++ programmer who knows to write pre-crement in order to avoid creating unnecessary copies? > Did he actually get the precedence of the operators correct? It's because I see a "chain" of operators and I have to think about it. > nope, it's an array of null-terminated strings. Is it? What happens when I pass 256 '-' characters. Or maybe 32767/32768/65535/65536? Or maybe ... > Does that error handling actually work--that loop can't exit with that condition unless the second clause does something. If I need to add a second unflagged argument, that error condition test also needs to be updated. It's an annoyance. A lot of this is no big deal in this code. The problem is that code is habit. You will carry the habits from short code over to long code. So, when you are now parsing 45 flags and 8 arguments, you will write code the same way and it WILL have bugs.