Tricky Tricks ›› Programming Tricks ›› C Languages Tricks ›› Equivalency Tests

Equivalency Tests

C Programming Tricks

Equivalency Tests:

If you write your equivalency tests in C "backwards", your are much less likely to accidentally use = where you meant ==. For instance:

    if (VALUE == variable)
    {
       /* ... */
    }
 rathern than: 
 
    if (variable == VALUE)
    {
       /* ... */
    }
    
 

because the 1st method will generate an error on the compile if VALUE is not a modifiable lvalue (a variable into which you can assign a value). Using the 2nd method, you could accidentally write if (variable = VALUE) and the mistake may go unnoticed, for a while anyway. BTW, this trick isn't really my own, but I use it.

Partners