3 ms·
I have for years made it a habit to write "if (CONSTANT == variable)" rather than "if (variable == CONSTANT)" so that if (oh, let's be honest -- when) I acciden
by Lagged2Death 18y ago
I have for years made it a habit to write "if (CONSTANT == variable)" rather than "if (variable == CONSTANT)" so that if (oh, let's be honest -- when) I accidentally use "=" instead of "==" I'll get a big nasty compiler error.
Some people have commented that it "looks kinda backwards," and I'm not sure what to make of that. My "backwards" way is perfectly correct, C-wise and math-wise, it doesn't make the code hard to understand, and it solves a real problem.
But I appreciate that it's important not to upset the other people on your team willy-nilly. So it's a bit of a quandry.
- altano 18y agoOther things to prevent this error: 1) Don't ever use assignment in a conditional expression, even on purpose. 2) Aggressively declare everything as const, even scalar input parameters to your function. 3) Use code analysis tools that treat this as a warning, which you should be treating as an error.