Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I abusing `const`?

These last weeks, I found myself using a lot of const everywhere. Not only in methods or arguments declarations, but even for temporary variables.

Let me illustrate with a simple function.

I used to write:

// A dummy function that sums some computation results
unsigned int sum_results(const Operation& p1, const Operation& p2)
{
  unsigned int result1 = p1.computeResult();
  unsigned int result2 = p2.computeResult();

  // Well this function could be in one single line but
  // assume it does more complex operations
  return result1 + result2;
}

But now it is more like:

// A dummy function that sums some computation results
unsigned int sum_results(const Operation& p1, const Operation& p2)
{
  const unsigned int result1 = p1.computeResult();
  const unsigned int result2 = p2.computeResult();

  // Well this function could be in one single line but
  // assume it does more complex operations
  return result1 + result2;
}

The latter makes more sense to me and seems less error prone. (I admit that in this example, it doesn't really matter) However I've seen very few code samples where const was used on temporary/local variables. And I'd like to understand why.

Is there any reason why this isn't a common case ? Am I abusing with my use of const ? Or is it just me that has been looking at the wrong samples ?

like image 377
ereOn Avatar asked Nov 09 '10 10:11

ereOn


4 Answers

This is effectively the same reason why assertions are rarely used. const on interfaces is mandatory, const in the implementation is voluntary. Programmers are lazy.

Edit: just in case it isn't clear, your approach is better.

like image 35
Šimon Tóth Avatar answered Sep 21 '22 01:09

Šimon Tóth


I would personally say that there never are too many const, and I use them abundantly for local variables. The only context where I could add a const but don't is on parameters of built-in types :

void foo(const int);

Here, I believe (but that's a matter of personal taste really) that it uselessly clutters the interface.

like image 32
icecrime Avatar answered Sep 23 '22 01:09

icecrime


I don't think it's purely a case of programmers being lazy - concision is also a consideration. Some people may find int x; less mental load than "const int x;" when reviewing the functions: that bit extra space might help them fit a comment alongside. I mention that not as a recommendation, but because I think it's important to understand all the "costs" that factor into peoples' attitudes, as it really is confusing that people don't consistently use const here.

It's also interesting to consider that at some point in using a variable in a function, it may become necessary to tweak it. For example, you calculate something, but then you go into a few if statements and things and there's some edge case where you need to remove a trailing element from a string, handle an off-by-one issue, clear the value etc.. If you had initially made the variable const, then your workflow is interrupted more to return to and remove const from the definition, then return the cursor to where you're working. Countering this, a habit of using const where possible is a red flag that some such tweaks are hidden in the function body, and very useful for later understanding and maintenance.

Still, I actively encourage you to continue to use const: I typically do so and consider it best practice. The reasons for that are obviously understood by you, and have been enumerated in other answers.

like image 40
Tony Delroy Avatar answered Sep 22 '22 01:09

Tony Delroy


Using const on local variables improves code clarity, so it's a good idea. You see const and you immediately know that the variable is never changed later in scope. It's from the same series as making functions short and returning early.

Developers are lazy - they often think that it's a useless word that doesn't change anything. IMO they are wrong.

like image 139
sharptooth Avatar answered Sep 23 '22 01:09

sharptooth