const
-keyword to the right of the subject, never the left.For as long as people have learned C and C++, they have first learned that if you want to declare a variable as constant, you prepend the variable type in the declaration with the const
keyword, like so:
const uint32_t counter = 40;
And while this certainly compiles, it's actually an exception to the rule of how the compiler parses declarations. It also adds confusion when one encounters more complicated declarations. To top it off, it completely destroys any hope of having consistent and elegant rules when dealing with const-correctness.
This convention of placing the const
left-most (when allowed) is a self-perpetuating fallacy that is ruining codebases and programmers all over the world, and should have never been allowed in the first place.
To be clear for newcomers: placing const
to the left is only allowed when the thing you're modifying is already on the far left. It's a weird parsing exception.
One of the more confusing areas of C and C++ to truly master is const-correctness. It usually takes programmers several years to write clean code in this regard, and I would argue that the convention of always using the left-const fallback (when allowed) is to blame.
Using const
in C and C++ is actually very simple, and it naturally follows how you otherwise parse declarations from right to left. You simply put the const
to the right of the subject you want to be constant:
// foo is a const uint32_t uint32_t const foo; // foo is a const ptr to a uint32_t uint32_t *const foo; // foo is a ptr to a const uint32_t uint32_t const *foo; // foo is a const ptr to a ptr to a const uint32_t uint32_t const **const foo; // it even applies to member functions: // foo::bar is a const function that returns a ptr to a const uint32_t uint32_t const *foo::bar() const { ... }
If programmers were taught this way from the get-go, while learning how to otherwise parse declarations (right-to-left), then learning const-correctness would not have been the big pain that it often is.
Now, compare the above elegant, consistent rules to this seemingly arbitrary mess of despair:
// foo is a const uint32_t const uint32_t foo; // foo is a const ptr to a uint32_t uint32_t *const foo; // foo is a ptr to a const uint32_t const uint32_t *foo; // foo is a const ptr to a ptr to a const uint32_t const uint32_t **const foo; // it even applies to member functions: // foo::bar is a const function that returns a ptr to a const uint32_t const uint32_t *foo::bar() const { ... }
Suddenly, there is no apparent consistency as to how const
should be applied; and this is how we currently teach new programmers these languages.
What are we programmers, if not connoisseurs of consistency & elegant solutions?
The confusion for newcomers aside, maintaining const-correct code can be very elegant and consistent if you fully reject the left-const convention.
Compilers always expect const
to be to the right, only checking the left side as an exception in specific cases, inconsistent to the normal rules, and also without any real added benefit.
Having a habit of always putting the const
-keyword to the left of the subject then becomes just as logical as using a fallback-value as a default; nonsensical.
Variable and function declarations can usually be read from right-to-left by humans as well, in a logical and elegant manner. It is only when you make an exception to the standard rules that you get the unnecessary inconsistency.
So why do people still insist on putting the const
-keyword to the left, if it's so inferior?
Because you let them. Or rather, because this is how they've been taught from the start, and this is how they've been teaching others. It's simple social perpetuation and surely to some extent peer-pressure. It's one of those things where people do it because that's how it's always been done.
People often don't enjoy making a big deal out of what feels like small details. And they rarely like change. This perpetuates the convention even further, which only acts to amplify the issues listed above. It's a negative spiral. One that we could break.
If you've read this far, the solution to these problems should be clear; Always put const
to the right.
Tell your friends, and even your enemies. Be slightly annoying at work and dare to make a little fuzz about this, tell your co-workers why they are wrong and how they can fix it. Send a polite but strongly worded letter to the committees in charge of the C and C++ standards, maybe one day left-const could become a compiler warning. And then later maybe even become unsupported, unless you add an extraneous compiler flag like --i-am-lazy-please-let-me-write-inconsistent-code
.
Thank you for spreading the word.