13 Sep 2003 mrorganic   » (Journeyer)

An interesting question of coding style. In C and C++, some people write the strcmp() function as follows:

if (!strcmp(foo,bar)) {
    /* do something interesting */

This makes the code hard to read, in my opinion. You are checking a positive result -- in this case, that two strings are equal -- and yet (because C and C++ treat FALSE as zero and a non-zero value as TRUE) you must check a negative result. This is highly non-intuitive, and I think it should be considered bad coding style.

Instead, the code should be written this way:

if (strcmp(foo,bar) == 0) {
    /* do something interesting */

It's not that much more typing, and it's far clearer as to what the intent of the conditional really is.

But that's just me.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!