The C++ community has worked hard in the past decade and more to move the language forward, to enrich but also simplify it, and to adopt new paradigms and coding styles. Yet, a single topic, a simple matter of style is splitting the community, in a pure Swiftian manner: the use of the const qualifier that some prefer it on the left of what it modifies, as it has been the de facto standard for decades, and which is now called West const, or to the right of that it modifies, that is a style that more and more people are adopting, and which is now called East const. I don’t particularly like this terms, I would rather use left const and right const, but if this is what the community prefers to use let’s call them so for consistency.
In the recent past, more and more flames have erupted on this topic, with people discussing it at conferences, on social media, or blog posts. This reminds me of the great war between the people of Lilliput and Belfuscu described by Johnathan Swift in Gulliver’s Travels:
It is allowed on all Hands, that the primitive way of breaking Eggs, before we eat them, was upon the larger End: But his present Majesty’s Grand-father, while he was a Boy, going to eat an Egg, and breaking it according to the ancient Practice, happened to cut one of his Fingers. Whereupon the Emperor his Father published an Edict, commanding all his Subjects, upon great Penaltys, to break the smaller End of their Eggs. The People so highly resented this Law, that our Histories tell us there have been six Rebellions raised on that account; wherein one Emperor lost his Life, and another his Crown. These civil Commotions were constantly fomented by the Monarchs of Blefuscu; and when they were quelled, the Exiles always fled for Refuge to that Empire. It is computed, that eleven thousand Persons have, at several times, suffered Death, rather than submit to break their Eggs at the smaller End.
Although breaking an egg at its little or big end was the cause for such a great war (and which, by the way, is the etymology of the little endian and big endian terms used to denote the sequential order bytes are stored in memory or transmitted over a network) it is, however, ultimately described as a matter of personal preference:
This, however, is thought to be a meer Strain upon the Text: For the Words are these: That all true Believers shall break their Eggs at the convenient End: and which is the convenient End, seems, in my humble Opinion, to be left to every Man’s Conscience, or at least in the power of the Chief Magistrate to determine.
How const works
The const qualifier is applied to what’s on its left. If there is nothing of its left, then it is applied to what it is on its right. Therefore, the following two are equivalent:
int const a = 42; // East const const int a = 42; // West const
In both cases, a is a constant integer. Notice though that we read the declaration from right to left and the East const style enables us to write the declaration exactly in that manner. That becomes even more useful when pointers are involved:
int * p; // p is a mutable pointer to a mutable int int const * p; // p is a mutable pointer to a constant int int * const p; // p is a constant pointer to a mutable int int const * const p; // p is a constant pointer to a constant int
These declarations are harder to read when the West const notation is used.
const int * p; // p is a mutable pointer to a constant int int * const p; // p is a constant pointer to a mutable int const int * const p; // p is a constant pointer to a constant int
Here is another example: in both cases p is a constant pointer to a mutable int, but the second alternative (the East const one) is more logical.
using int_ptr = int*; const int_ptr p; int_ptr const p;
There is also the following situation when you declare multiple constants in a single declaration. In the following example, a, b and c are all constant integers, but some argue that the West const alternative is better denoting this. I do believe in single line declarations, I’m never writing code like this and I believe this should be discouraged.
const int a = 1, b = 2, c = 3; int const a = 1, b = 2, c = 3;
The East const style is also consistent with the way constant member functions are declared, with the const qualifier on the right.
int get() const;
The arguments against East const
The supporters of the West const bring two main arguments against using East const, none of which being really strong.
The most important argument is that West const is the traditional style, that the standard itself uses it, and that most of the C++ codebase ever written is using it. That’s the same argument raised for ages against new things. It’s the inertia to change but it eventually cannot stop the progress.
The C++ Core Guidelines, edited by Bjarne Stroustrup and Herb Sutter, go as far as defining a naming and layout ruled NL.26: Use conventional const notation that says that the East const is “more logical” but recommend using West const because “Conventional notation is more familiar to more programmers.” and for “Consistency in large code bases.” And they even specify an enforcement to “Flag const used as a suffix for a type.”
I believe this guideline rule is wrong and should be removed. Not only people should be free to choose their coding style, but they should be encouraged to use the more logical approach, as NL. 26 acknowledges that East const is.
The other argument is that the English language has prepositive adjectives (such as in big book or peaceful moment), and although many other languages don’t, English is the lingua franca of software engineering and programming languages should adhere to English rules. This argument is probably best explained by Borislav Stanimirov in a CppCon talk called The Bad Big Wolf Meets Riding Hood Little Red.
I personally disagree with this argument too because I don’t think C++ is English (although, of course, it uses a series of English words as keywords and reserved words) and I don’t think English grammar rules should come before C++ grammar rules. Since C++ declarations are best read from right to left, the position of adjectives in the English language should be irrelevant.
Those that defend the West const style argue (mostly for the fun of it) that if the standard committee wanted us to use East const there would be no constexpr, consteval, or const_cast but exprconst, evalconst, and cast_const. These are all keywords, and as we already established, they are all English words, so it makes sense that they follow the prepositive adjective rule. That’s also the reason we refer to these styles as West const and East const and not const West and const East.
Join the revolution
It’s time to put the past aside and adopt a better coding style concerning const and that style is East const.
Therefore, I urge you to join this revolution of coding and place the const qualifier to the right of the type that it qualifies. And also teach others, colleagues, students, strangers to use this style so they could write not only modern C++ code but modern const consistent C++ code too.
And do check the Petition for const Consistency!