views:

118

answers:

2

In this question, I tried to give an example of two ways of doing the same thing in C++: one short but complicated "fancy" piece of code; and a longer but more readable and understandable way of doing the same thing. The question asks whether it's better to have a well-commented fancy piece of code or a self-commented (i.e. no comments) longer version.

The problem with my example was that the fancy version turned out to be more understandable than the non-fancy version. Since I can't think of any good examples, I figure I'd try asking here. So what I need is two examples of C++ code which produce the exact same result, but in one version the code is short but very complicated and cannot be understood immediately without having to think about it, and a longer but much more readable and understandable version.

I want someone to look at the fancy version and go "Shit, that's complicated. I'm glad it's commented, otherwise I wouldn't have understood what it does" and then look at the longer version and go "Ah, I understand what it does, even without comments". The shorter example should be something like 1-10 lines and the longer maybe 10-30 lines.

+2  A: 

Do you mean something along these lines?

First version:

int srtcpy(char *dst, char const *src) {
    char *dst_start = dst;
    while (*dst++ = *src++); // Incrementally copy *src to *dst until *src is 0
    return dst_start;
}

Second version:

int srtcpy(char *dst, char const *src) {
    char *dst_start = dst;
    while (*src != '\0') {
        *dst = *src;
        dst = dst + 1;
        src = src + 1;
    }
    *dst = '\0';
    return dst_start;
}

The more interesting question now is: What about the compiled versions of these functions: do they differ once compiled and optimized? Is the first actually faster than the second? What is the actual gain of writing fancy pieces of code?

Didier Trosset
Yeah, something like that. Unfortunately that's almost the exact example I provided. ^^ But apparently a lot of people thought that the "fancy" version was not all that fancy, and more readable and understandable than the "non-fancy" version. We're only interested in readability now, so how _fast_ it actually executes is of no importance. Well, it may be if you want to argue for using the fancy version over the non-fancy, but that's another story.
gablin
well, changing `foo++` to `foo = foo + 1` hardly makes C code more readable, expect perhaps for a person who sees C for the first time. For the rest of us, `foo++` is easier to understand (since it's a visual pattern the brain grasps without thinking too much, unlike `foo = foo + 1` which is uncommon in real C code)
Eli Bendersky
I think your "problem" might be that `while (*dst++ = *src++);` is actually a well known pattern immediately recognized for what it is by many C programmers. The first time a "newbie" sees it, it might need picking apart to be understood fully.
calmh
+2  A: 

How about this?

//direct formula for xoring all numbers from 1 to N
int Sum = (N & (N % 2 ? 0 : ~0) | ( ((N & 2)>>1) ^ (N & 1) ) );

instead of:

int Sum = 0;
for (int i = 1; i < N; ++i)
{
   Sum ^= i; //or Sum = Sum ^ i;
}

Formula taken from here

Armen Tsirunyan
Just what I was looking for. Looks... very complicated. Thanks. ^^
gablin