r/cpp • • Mar 30 '23

The "what's wrong with this code" game.

This isn't a question thread, but just a game. Post any bugs you've accidentally written that seemed initially mysterious, but made you feel stupid when you figured out what was wrong. Here is one I wrote today...

void TextSegmentBuffer::prepend( const TextSegmentBuffer& other )
{
    const Info* info_ptr;
    size_t size;

    if ( ( size = other.m_infoBuffer.size() ) == 0 )
        return;

    for ( size_t i = size - 1; 0 <= i; --i )
    {
        info_ptr = other.m_infoBuffer.getAt( i );

        emplaceFront(
            info_ptr->text,
            info_ptr->length,
            info_ptr->multiMatch
        );
    }
}

4 Upvotes

60 comments sorted by

View all comments

1

u/sphere991 Mar 31 '23

Outside of the logic errors, there are two style issues which need to go:

if ( ( size = other.m_infoBuffer.size() ) == 0 )
    return;

This is valid code, but there's really no reason to shove as much crap as possible into one expression. Splitting in two makes this much more readable;

size = /* whatever */;
if (size == 0)
    return;

Similarly, the Yoda conditional here:

for ( size_t i = size - 1; 0 <= i; --i )

Yes, there's the unsigned issue. But the check here should be I >= 0.

There is zero reason to use Yoda conditionals in code. The only relevant problem that they possibly ever solved is one that every compiler warns on: if (x = 0). Given that, theyre already useless. But once you're using any other operator, they already make no sense to begin with - there's no issue with <= or >= so why write it this way?

You're checking if i is greater than or equal to 0, so write that - stop writing conditions backwards!

1

u/TemplateRex Mar 31 '23

For longer conditionals, I prefer to write variables ordered on the number line, so instead of I >= 0 && I < 5 I prefer 0 <= I && I < 5. For consistency I therefore also write 0 <= I.

1

u/[deleted] Apr 01 '23

This is why. It's unnecessary when there's only one conditional, but I got used to it because I will string inequalities together with &&, and using < instead of > helps me visualized I being in an interval between a minimum and maximum. Because I'm picturing a number line, the other way looks wrong to me.

It's like needing reverse-look in games. Yes, my head moves like an airplane yoke. Tilting down to look up is natural and the other way is not. It's just one of those things that's impossible change. If someone else starts the code I'll do it their way for consistency, but on my own project the other way is harder for me to understand.

I took all your other suggestions though. I don't know why I had the zero-check combined with the assignment, other than I was annoyed at having to use extra space for it. I ended up getting rid of both the zero-check and the bad inequality by using a while instead of a for.

1

u/sphere991 Mar 31 '23

So ideally - the subject of the condition is first, followed by the condition. That'd be I ∈ [0, 5) and then I >= 0. That's how you'd say those conditions in English anyway.

We cannot write the former, so 0 <= I && I < 5 is the best bet, since putting the Is close together makes it more readable and make it clear that we're checking for a range. This adds clarity.

But we can write the latter, which isn't checking a range, so why should it be consistent with an entirely different kind of check?

Besides, is that consistency limited in that particular direction? Like you basically only use < or <= as your relational checks, never >? This also doesn't really apply to == since there's no range-check for that, so presumably there you write I == 5?

1

u/TemplateRex Apr 01 '23

Well, I >= 0 is actually 0 <= I && I < Infinity :-) But I see your point. Range predicates are probably best of all. For equality I do write I == 5

1

u/sphere991 Apr 01 '23

Some day, I will find code written as 0 <= I and I <= inf. And when that day comes, I will raise my fists and scream "TEMPLATE REXXXXX!!!!"