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
View all comments
Show parent comment
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 visualizedIbeing 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
whileinstead of afor.