DEV Community

Discussion on: The const issue

Collapse
 
timbeaudet profile image
🏎️Tim Beaudet🚀 • Edited

I disagree, while it does read slightly better right-to-left, which is required for determining what is const, it isn't clear. For instance in your example, you did not modify myVar, (assuming that pointer was set to the address of myVar) - but you modified the *. To me it reads much cleaner as:

const int* myVarPtr

assuming you don't want to change the value, which is 99% of the time - generally we don't care if the pointer gets modified, but the contents it points to... now that is important.

In the 1% of the time that both, or just the pointer, is important to be const, then the imo ugly:

const int* const myVarPtr;
int* const myPtr;

Will stop and make the reader / maintainer look twice. If the const was ALWAYS on the right,

int const value;
int* const valuePtr;

it is easy, very easy to slip up in maintaining or accidental writing of a const pointer to a non const value, when it should have been a pointer (const or not) to a const value.