Originally Posted By: Roger
...which can't happen with a modern compiler. A modern C++ compiler will require some extra parentheses to suppress the warning. The C# compiler won't allow it at all.

Your coding standard has a large portion of cargo cult in it.


I don't deny it. smile

However I didn't realize that recent compilers prevented the mistake. That's cool! Is that only true of recent versions of Visual Studio? Because I could swear I had made that mistake myself within the last 5 years or so, and had adopted the reversed positioning ever since then, to prevent the problem. Maybe I'm remembering the mistake from a different language other than C/++/#, perhaps a scripting language where the mistake isn't protected against. I suppose that alone could be a good reason to adopt the practice: Some languages/compilers protect you against it, some don't.

I have one other reason that the reversed positioning makes sense to me, which is related to writing unit tests. If I have an NUnit test and I have an assert like this in it:

Assert.AreEqual(0, returnValue, "Installation failed.");

Then the expected value is almost always the first parameter in NUnit assertions, which is usually a constant of some kind (in this case 0 meaning successful return value), and the tested value is almost always the second parameter. I've seen some unit tests where the test author got them backwards, and then the email that reports the test failure is backwards, saying something like "Installation failed. Expected: 1603, Actual: 0." So I'm always double-checking that in those kind of comparisons, the constant is first and the tested value is second. This positioning happens to map directly to the "backwards 'if' statement" style in my company's cargo-cult coding standards, so it helps my brain to be always thinking that way.

Another "Cargo Cult" thing our company does is the thing with empty strings...

Bad:
string myString = "";
Good:
string myString = string.Empty;

There is some historical thing saying that there are technical reasons why the latter is better or more efficient for the compiler. But a while back I googled it, and the results seemed to tell me that the historical thing is no longer true and so this convention is no longer needed. But it's still in our coding standards and I still get corrected on it.
_________________________
Tony Fabris