by didgetmaster · 4y ago · 0 comments
if(/some really long boolean value with AND and OR clauses/); { /* some code to execute if TRUE */ }
He was sure that the if statement was FALSE some of the time, but the code inside the brackets would always execute. He spent more than an hour pouring over the code.
It took me a few minutes before I noticed the semicolon at the end of the if statement. I'm sure he felt stupid when I pointed it out.
He certainly did. There is no amount of experience that makes people not feel stupid about those. What changes is that you learn to not be affected by it, because you know that it's not a personal flaw (at least not one specific to you), and while you can improve on it, you can't completely fix it.
But the alternating between feeling like the dumbest person on Earth and the smartest person on Earth never goes away.
After at least a few hours away (ideally not coding) your fresh eyes will often spot it.
Similarly, demoing the bug to someone else is a good way, and often through the mere act of demoing it you'll find it yourself (also known as "rubber ducking" [1]).
bool condition = /some really long boolean value with AND and OR clauses/;
if(condition); { /* some code to execute if TRUE */ }
Two advantages of doing it this way:1. It's much easier to notice that incorrect semicolon
2. Even if you don't, you can stick a breakpoint (or log) between when condition is set and when it is evaluated, to convince yourself it works.
3. All modern compilers will produce the exact same code as they produce with it mashed into the if()