> From: Dale Botkin [mailto:dale@botkin.org] > > So would you be as comfortable with one line of > cryptic-looking code with > five lines of comments explaining it, or five lines of perfectly clear > code with one line of comments? Or five and five? Personally I tend > toward the first: > > void strcpy (char *s, char *t) { > // This will copy from *t into *s, incrementing each > // pointer and looping until a null is encountered in > // *t. Once we see a null, *s = *t evaluates to 0 > // and the while loop ends. > while (*s++ = *t++); > } > > I tend to do it that way for a couple of reasons -- first, > since I still > consider myself to be something of a novice C programmer, I like to go > through my code a few times to see if I can optimize it > further before the > compiler gets to it. Second, it leaves enough bread crumbs > behind that > when I look at it later on I can remember what I did and why. > That's nice > no matter how many lines you use to do the same thing. Though a one line while loop looks clean, and comments above it can make it so a novice can understand the code. I think something is being overlooked. The code may need to be debugged for any number of reasons. When stepping through code at source level, a single step in the one line copy takes you through the copy, two increments, and a compare. That's a fair amount of instructions and our bug could have occurred at any one of those phases. while ((*s = *t) != '\0') { s++; t++; } should generate the exact same code as the one line loop, but now I have smaller resolution of a source level step. If the generated code will be the same, I tend to go with the source that performs minimizes the amount of instructions generated per source line of code. It makes source level debugging with an ICE a little easier and it takes less effort for me to parse a statement when I go back to it a few years later. An other benefit is that there are many organizations that use source lines of code as some kind of measurement. If you find yourself working for one of those organization, it will probably be in your benefit to inflate the source lines of code you generate. -Mike -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.