• 0

[C++] Coding style of const local variables


Question

I've been wondering about this recently (and haven't been able to find any good resources online).

Would you make local variables that should not (or do not) change const?

From following several other projects they always seem to leave them non-const.

But I thought it is a fairly good documentation tool to make them const, although it does clutter up the code somewhat, especially if you have something like "int const * p;".

This is more of a style question, so I don't care if there are or are not any performance improvements.

22 answers to this question

Recommended Posts

  • 0

As far as an online resource: stackoverflow.com. All the gurus and professional developers answer questions there. I wouldn't be where I was if it wasn't for them. I've done very little C++ but that's because I'm caught up with PHP at this point in time...

I would like to try to answer your question but I recommend you visit the link instead if you'd like to get a thorough answer.

  • 0
  On 09/04/2011 at 13:56, Lant said:

Would you make local variables that should not (or do not) change const?

If a value does not change, then I usually declare it as a static const or a preprocessor symbol. If the variable should not change because of program logic, for instance, passing a variable to a function that doesn't have permission to change it, then a simple constant qualifier is sufficient.

  On 09/04/2011 at 13:56, Lant said:

From following several other projects they always seem to leave them non-const.

But I thought it is a fairly good documentation tool to make them const, although it does clutter up the code somewhat, especially if you have something like "int const * p;".

It certainly makes code more readable, manageable, and helps to isolate logic and behavioural errors.

  On 09/04/2011 at 13:56, Lant said:

This is more of a style question, so I don't care if there are or are not any performance improvements.

It's not merely a question of style. because a constant qualifier determines how a variable can be used. It's also self documenting, prevents unexpected behaviour, and allows the compiler to perform additional optimisations.

  • 0
  On 09/04/2011 at 13:59, surrealvortex said:

No point really. I would rather include a clear comment indicating that the value should not change.

Yes, but why include a comment when it can be ignored later on when you can add a const that will be enforced by the compiler?

  • 0
  On 09/04/2011 at 14:12, Flawed said:

If a value does not change, then I usually declare it as a static const or a preprocessor symbol. If the variable should not change because of program logic, for instance, passing a variable to a function that doesn't have permission to change it, then a simple constant qualifier is sufficient.

I'm talking about local variables inside functions here, not class level or global constants.

Plenty of my code is like the following:


bool x(C c)
{
std::vector<int> v = c.fn();
int i = c.fn2();


if ( std::find(v.begin(), v.end(), i) != v.end() )
...

return ;
}
[/CODE]

If v and i was never going to be modified by the rest of the function should I make them const?

Edited by Lant
  • 0
  On 09/04/2011 at 13:59, surrealvortex said:

No point really. I would rather include a clear comment indicating that the value should not change.

And permit behaviour that defies the design of the program? You're inviting problems if you follow that path. Let the code document itself, and at the same time prevent unexpected behaviour; it's far easier to debug and certify your application this way.

  • 0
  On 09/04/2011 at 14:19, Lant said:

I'm talking about local variables inside functions here, not class level or global constants.

Plenty of my code is like the following:


bool x(C c)
{
std::vector<int> v = c.fn();
int i = c.fn2();


if ( std::find(v.begin(), v.end(), i) != v.end() )
...

return ;
}
[/CODE]

If v and i was never going to be modified by the rest of the function should I make them const?

For instance in boost\date_time\src\gregorian\greg_month.cpp

[CODE]
special_values special_value_from_string(const std::string& s) {
short i = date_time::find_match(special_value_names,
special_value_names,
date_time::NumSpecialValues,
s);
if(i >= date_time::NumSpecialValues) { // match not found
return not_special;
}
else {
return static_cast<special_values>(i);
}
}
[/CODE]

The i is never changed, but it is not declared const.

This is from boost sources, and they use fairly up-to-date coding methods and styles.

So why don't they do it?

[b]I don't think in that example i is a constant. Simple as that.[/b]

Imagine if they call date_time::find_match() again within the program. If the i variable was a constant and date_time::find_match() returned a different value, the i variable would change and the program would throw a runtime error

Also, from: Stackoverflow.com

  Quote

In general, I think it's always a good idea to make your intentions clear this way, for a number of reasons:

It's easier to understand what you meant when you wrote it.

It's easier for others to reason about your code when they read it.

It's easier for the compiler to make inferences about what you want to do.

It's easier for the compiler to prevent logical errors.

Making variables constant is a perfectly legitimate way to accomplish all of the above, in the specific case of intending "I don't want this variable to change its value".

(This is why I love stack overflow, they just find the right words to explain which I'm bad at) :)

In short, it is strongly suggested you make the int a constant if the integer will not change throughout the program.

  • 0
  On 09/04/2011 at 14:19, Lant said:

I'm talking about local variables inside functions here, not class level or global constants.

Plenty of my code is like the following:


bool x(C c)
{
std::vector<int> v = c.fn();
int i = c.fn2();


if ( std::find(v.begin(), v.end(), i) != v.end() )
...

return ;
}
[/CODE]

If v and i was never going to be modified by the rest of the function should I make them const?

Why not write it as:

[CODE]
bool x(C c)
{
std::vector<int> v = c.fn();
if ( std::find(v.begin(), v.end(), c.fn2()) != v.end() )
...

return ;
}
[/CODE]

But if you insist on doing it that way, no I don't see a need to define it as a constant in the function. The const qualifier is most beneficial when two modules are interacting, or to prevent some unexpected behaviour. Use of it in a local function as in your example is unnecessary. There are cases though where you might want to define a static local constant for a function; for instance, in order to encapsulate/hide the details of it if it's specific to that function, or perhaps if a function returns a constant pointer, but for simple temporary values on the stack, there isn't much point.

  • 0
  On 09/04/2011 at 14:34, Flawed said:

Why not write it as:


bool x(C c)
{
std::vector<int> v = c.fn();
if ( std::find(v.begin(), v.end(), c.fn2()) != v.end() )
...

return ;
}
[/CODE]

But if you insist on doing it that way, no I don't see a need to define it as a constant in the function. The const qualifier is most beneficial when two modules are interacting, or to prevent some unexpected behaviour. Use of it in a local function as in your example is unnecessary. There are cases though where you might want to define a static local constant for a function; for instance, in order to encapsulate/hide the details of it if it's specific to that function, or perhaps if a function returns a constant pointer, but for simple temporary values on the stack, there isn't much point.

I disagree with this. For the same reasons I had posted in my reply above Flawed's post.

  • 0
  On 09/04/2011 at 14:25, Tekkerson said:

I don't think in that example i is a constant. Simple as that.

Imagine if they call date_time::find_match() again within the program. If the i variable was a constant and date_time::find_match() returned a different value, the i variable would change and the program would throw a runtime error

It could be declared "const short i" and it would all be perfectly fine until the static_cast which would probably cause a compile error as the cast is trying to remove constness. (I realised it was a bad example so I removed it)

But it wouldn't throw a runtime error.

  • 0
  On 09/04/2011 at 14:25, Tekkerson said:

In short, it is strongly suggested you make the int a constant if the integer will not change throughout the program.

I agree with "throughout the program", or even:

void my_func() {
static const int my_func_number = 12345;
/* do some work */
}

But for temporary local function variables, I don't really see a justification in general, and certainly not in the example code provided by the OP.

  • 0
  On 09/04/2011 at 14:44, Lant said:

It could be declared "const short i" and it would all be perfectly fine until the static_cast which would probably cause a compile error as the cast is trying to remove constness. (I realised it was a bad example so I removed it)

But it wouldn't throw a runtime error.

You know, that's funny at first I typed compile then I edited it to runtime... (thought processes got mixed up or something)

  • 0
  On 09/04/2011 at 14:45, Flawed said:

I agree with "throughout the program", or even:

void my_func() {
static const int my_func_number = 12345;
/* do some work */
}

But for temporary local function variables, I don't really see a justification in general, and certainly not in the example code provided by the OP.

If it's a constant, it should always be declared constant no matter where it is. What if for some reason you changed something in the code that made the variable change when it wasn't supposed to? It's just a good coding practice. But that's for the OP to decide.

  • 0
  On 09/04/2011 at 14:41, Tekkerson said:

I disagree with this. For the same reasons I had posted in my reply above Flawed's post.

That was talking in general, and I do agree with those principles as can be seen by my original post, but in general, temporary local function variables aren't likely to be const targets.

Edit: Removed my unnecessary sarcasm.

  • 0

My code is already littered with BOOST_STATIC_CONSTANTs,

what I was really after is if the documentation value of declaring local variables (mostly stl containers) const is worth it over the readability loss of adding const.

I suppose its just one of those things in C++ that there is no real answer either way

  • 0
  On 09/04/2011 at 14:53, Tekkerson said:

If it's a constant, it should always be declared constant no matter where it is. What if for some reason you changed something in the code that made the variable change when it wasn't supposed to? It's just a good coding practice. But that's for the OP to decide.

I agree in general with your logic, but you have to ask the question, why is that variable a constant? We are speculating without knowing the actual case. At first glance, I wouldn't declare it as a constant, but that's just me. Perhaps you are right.

  • 0
  On 09/04/2011 at 14:57, Flawed said:

That was talking in general, and I do agree with those principles as can be seen by my original post, but in general, temporary local function variables aren't likely to be const targets.

I do have another thing to add though. Even with Flawed's point, even in the smallest cases I just think it's a good habit to do it where it applies, Lant.

Though, in the end I think this is more of a personal question. It boils down to:

Would you prefer cleanliness/convenience (less declarations) over "correctness" (which also has it's benefits)? Which is what I think sums up both me and Flawed's point.

Edit: To fit with Flawed's edits. :p

  • 0
  On 09/04/2011 at 14:58, Lant said:

My code is already littered with BOOST_STATIC_CONSTANTs,

what I was really after is if the documentation value of declaring local variables (mostly stl containers) const is worth it over the readability loss of adding const.

I suppose its just one of those things in C++ that there is no real answer either way

It's the same in C as well. While I agree with most of what Tekkerson's said, in practise, I rarely use const qualifiers in my own code in temporary local function variables. The occasions when I do are, if I want to declare a static const value to be used only for that function only, or if another function returns a pointer value that is immutable.

Though now I think about it, Tekkerson's arguments are compelling. So if you find it makes your code more readable, manageable, and prevents program logic / behavioural errors, then go for it.

  • 0

I only use const where the language forces me to, for example:

void myFunction(const std::string&amp; arg); // const must be used to allow the following usage:
myFunction("String literal");

Having successfully developed applications in Python, where there is no equivalent of the C++ const keyword, I don't believe const adds enough value to be worth having to constantly (no pun intended) worry about it. I just put constants in ALL_CAPS and I never declare const methods.

  • 0
  On 09/04/2011 at 14:22, Flawed said:

And permit behaviour that defies the design of the program? You're inviting problems if you follow that path. Let the code document itself, and at the same time prevent unexpected behaviour; it's far easier to debug and certify your application this way.

Const variables can be modified indirectly. It is not foolproof.

I can see where you are coming from. The code definitely will document itself that way, but when I suggested adding a comment, I meant a detailed comment explaining the intention. Not something like: /*This is a const*/

  • 0
  On 09/04/2011 at 18:17, surrealvortex said:

Const variables can be modified indirectly. It is not foolproof.

I can see where you are coming from. The code definitely will document itself that way, but when I suggested adding a comment, I meant a detailed comment explaining the intention. Not something like: /*This is a const*/

I would discard what I said there. I misunderstood the original question, and that comment was aimed at function parameters and class/global scope variables. Nevertheless, I agree with what you are saying. A terse comment can help explain the variable's purpose and/or why it's immutable if it's not already self explanatory. Personally, I haven't really felt the need to use constant qualifiers at the function scope level except in the special circumstances I outlined previously, but I suppose as long as the code is readily readable, then either way would work.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Bethesda's Deathloop is free to claim on the Epic Games Store this week by Pulasthi Ariyasinghe The Epic Games Store's Mega Sale promotion is in its final week, and that means mystery giveaways are soon ending too. For the finale, Epic Games has called in something big from Bethesda, with the Arkane-developed time loop adventure Deathloop coming in as the latest freebie for PC gamers. Alongside it, the indie adventure Ogu and the Secret Forest is also free. Developed by Arkane Lyon and published by Bethesda, Deathloop comes in touting an action-packed campaign involving plenty of time travel shenanigans. The game is set on the mysterious island of Blackreef, where two rival assassins, Colt and Julianna, are trapped in a time loop. The player, as Colt, has to figure out how to eliminate eight targets within a single day to escape the loop. Each of the assassinations can be taken care of in many ways, including stealth, traps, accidents, or simply going in guns blazing. Aside from gunplay, the title also makes use of supernatural systems very similar to the studio's Dishonored franchise, letting players teleport, go invisible, use telekinesis, and more. There is a multiplayer twist here too, where players, as Julianna, can invade the campaigns of others to take the role of the rival assassin, flipping the tables on the main character and his plans. As for Ogu and the Secret Forest, this is a 2024-released indie adventure featuring hand-drawn characters and various types of puzzles. The 2D game involves befriending characters across a fantasy land as baby Ogu, with plenty of exploration, puzzle solving, and boss battles available. The Deathloop and Ogu and the Secret Forest giveaways are now live on the Epic Games Store for all PC gamers. The promotion is slated to last until June 12, giving you seven days to claim a copy for your library permanently. While the summer mystery giveaways are ending, regular freebies will continue to arrive from the Epic Games Store. When this one comes to an end on Thursday, the SEGA-published humorous hospital simulation entry Two Point Hospital is incoming as the next giveaway.
    • If graphics is all that matters to you go watch a Pixar movie or something, you sound like an absolute baby right now. The game looks great.
    • For sure!! My take is... If the dev choses to use Apple's payment services, sure, Apple can take a cut. Apple disallowing 3rd party payment processing directly within an app, kind of a gray area, its anti-competitive sure, but I can at least see arguments on both sides. Apple blocking or attempting to take a cut from weblinks is pure evil, there is no possible justification at all. Heck, if we take this to the logical next step, Apple should start demanding a cut of retail transactions if the user used GPS directions on an iPhone to get to the store.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      407
    2. 2
      +FloatingFatMan
      181
    3. 3
      snowy owl
      176
    4. 4
      ATLien_0
      170
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!