Self-WTF



  • So I was looking through some old stuff I wrote when I was a student, and came across this gem (carbuncles are gems, right?):

     

    //returns true if it made a step, false if it couldn't do so
    bool oneStep(vector<unsigned long long>& baseVec)
    {
        const unsigned lastEl = baseVec.size();
        baseVec.push_back(0);

        for (int i = lastEl; i >= 0; i--)
            if (baseVec[i] >= 2)
                for (unsigned j = i + 1; j <= lastEl; j++)
                    if (baseVec[j] + 2 <= baseVec[i])
                    {
                        baseVec[j]++;
                        baseVec[i]--;
                        for (unsigned k = j, l = lastEl; k < l;)
                            if (baseVec[k] == baseVec[k - 1])
                                k++;
                            else if (baseVec[l])
                            {
                                baseVec[k]++;
                                baseVec[l]--;
                            }
                            else
                                l--;
                        while (!baseVec.back())
                            baseVec.pop_back();
                        return true;
                    }
        return false;
    }

     

    I'm not sure what the worst part is: my apparent allergy to braces, the variable names, the singularly unhelpful comment, the complete lack of any indication what the function actually does, the bizarre way I used repeated ++ and -- rather than adding and subtracting a lump all at once... geez. (I actually remember writing this, a little. In an earlier version, the variable i was unsigned. That is, I was testing an unsigned number to see if it was >= 0. Yeah. That caused some problems.)



  • @RedFeather said:

    (I actually remember writing this, a little. In an earlier version, the variable i was unsigned. That is, I was testing an unsigned number to see if it was >= 0. Yeah. That caused some problems.)

    Don't you know that C13 will require that unsigned can represent -1 as well, exactly to remove that source of errors?

    On a similar note, float and double will need to store integers up to 2**61, too.

    So you're just a bit early.

    Edit: why are the types marked with TT in the edit field, but not in the result?



  • @flop said:

    Don't you know that C13 will require that unsigned can represent -1 as well, exactly to remove that source of errors?

    Wait, what, seriously? That's going to cause way more bugs than it prevents. What I really should have done is test whether (i+1) > 0. On another variable type note: looking at the program, there was no real reason for the vector to hold unsigned long longs; there was no plausible way any entry in it could even get into the triple digits.



  •  I hope that was a joke.



  •  Okay, yeah, that must have been a joke. There's no such thing as C13, for one thing. Too early in the morning for me to get humor, I guess.



  • @Medinoc said:

    I hope that was a joke.

    On this site?!



  • @blakeyrat said:

    @Medinoc said:
    I hope that was a joke.

    On this site?!

    It's less likely than you think.



  • @RedFeather said:

    const unsigned lastEl = baseVec.size();

    Error right here. Must subtract one. That will fail for an empty vector, though.



  • @alegr said:

    Error right here. Must subtract one. That will fail for an empty vector, though.
     

    No, that's intentional. Note that in the next line I add another element to the end.

     


Log in to reply
 

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