Advertisement

In search of contrary evidence

December 15, 2008

Dan_Saks-December 15, 2008

I learned to program in C nearly 30 years ago. When support for the const qualifier appeared in compilers some 10 years later, I learned to place it as most programmers do: to the immediate left of the type that it modifies. That is, when I wanted to declare p as a pointer to a constant character, I wrote the declaration as:

const char *p;

as just about everyone else did.

As I grew to understand the const qualifier better, I started to encourage other programmers to use it, which I did by using const whenever appropriate in my writing and in my lectures, and I was not alone. Other writers and speakers were using const as well. Still, many programmers seemed less than enthusiastic about using const.

I started asking programmers why they were reluctant to use const. Most said they just didn't understand it. They knew they could place const at different points within a declaration, and they just didn't understand the consequences of the different placement options. For example, the const in:

const char *p;

modifies the char to its immediate right (it declares p as a pointer to a const char). Does that mean that the const in:

char const* p;

modifies the pointer operator to its immediate right (it declares p as a const pointer to a char)? (Answer: No.)

In the mid-1990s, it occurred to me that it might be easier for programmers to understand const if they learned to apply this fairly simple rule: when writing a declaration, place const to the immediate right of the part of the type that you want to be const; when reading that declaration, read const just before you read the thing to its immediate left. For example, to declare p as a pointer to const char, write:

char const *p;

To read it aloud, just read it from right to left, as "p is a pointer to a const char." To declare p as a const pointer to char, write:

char *const p;

To read it aloud, again just read it from right to left.

I've written earlier columns on why this style makes it easier to read and write declarations using const. 1, 2, 3 You can find them at www.dansaks.com.

In the years that I've been using and teaching this style for placing const in declarations, I received a lot of positive feedback that using this style does indeed help. Unfortunately, the converts represent what appears to be a distinct minority of C and C++ programmers. Most C and C++ programmers simply won't do it. Moreover, they won't even try it.

When I ask programmers why they won't try it, they often tell me, "It's not conventional" or, "Hardly anybody does it that way," to which I offer the following response.

For the last six to eight years, I've been posing the following query to my lecture audiences. Given these declarations in the conventional style:

typedef char *ntcs;
const ntcs s;

what is it about s that's const? Is it (a) the character, or (b) the pointer, or (c) both? (The answer appears at the end of this article.) I've posed this question to thousands (the low single-digit thousands) of programmers and only about 40% get it right.4 The rest either get it wrong or are unable or unwilling to hazard a guess. At the same time, well over 90% of these programmers reported using the conventional style for placing const.

So, when someone tells me they won't use it because it's not conventional, my response is that, in this case, conventional isn't working. Maybe it's time to try something else.

< Previous
Page 1 of 2
Next >

Loading comments...