Don’t Hand-count Characters

A common mistake that most programmers do is to hard-code hand-counted lengths of string literals. For example:

if (!strncmp(str, "MAGIC_PREFIX_", 13))
{
    ...
}

Even if the string literal “MAGIC_PREFIX_” is not due to change in a life time, it makes the code more prone to human error and harder to read when the length of the string is hand-counted and hard-coded as a separate entity (13 in this case). A better way is to use the sizeof operator:

if (!strncmp(str, "MAGIC_PREFIX_",
    sizeof("MAGIC_PREFIX_") - 1))
{
    ...
}

To some, it may look like a function call that needs to be dealt with at run time; however, sizeof is an operator and its outcome is resolved at compile time. For the example above, the compiler would simply insert 13 in place of the whole “sizeof() - 1″ expression. Note that since “MAGIC_PREFIX_” is a null-terminated character array, sizeof would yield 14, which is the exact size of the character array, since we have to take into account the extra null character (’\0′) at the end. The purpose of the “- 1″ following sizeof is to account for the null character.

The advantages are:

  1. You don’t have to hand-count the characters while doing the coding and therefore you avoid the risk of miscounting,
  2. Other people looking at your code can clearly see your intention and don’t have to do additional hand-counting to verify your code.

However, it’s a bit awkward to make a copy of the string to be used inside the sizeof operator because it requires you to update both copies when a string needs to be modified. To get around this redundancy (while unfortunately adding extra bulk to your code), you could define the string as a constant:

static const char MAGIC_PREFIX_STR[] = "MAGIC_PREFIX_";
static const size_t MAGIC_PREFIX_LEN =
    sizeof(MAGIC_PREFIX_STR) - 1;

if (!strncmp(str, MAGIC_PREFIX_STR, MAGIC_PREFIX_LEN))
{
    ...
}

Just change the value of MAGIC_PREFIX_STR and you’re good to go! Doing this obviously has additional advantages if the string in question is going to be used in more than one location.

You should also take note that the constant has to be defined as “char MAGIC_PREFIX_STR[]” and not “char *MAGIC_PREFIX_STR”. They are identical when it comes to just using the pointer of the string but sizeof will just yield the size of a pointer (char *) instead of the actual length of the string plus the null character.

One Response to “Don’t Hand-count Characters”

  1. Web 2.0 Announcer Says:

    Don’t Hand-count Characters…

    […]A common mistake that most programmers do is to hard-code hand-counted lengths of string literals. A better way is to use the sizeof operator.[…]…

Leave a Reply