Fog Creek Software
Discussion Board




The importance of being const

I was browsing the Web the other day, and was shocked to come across this quote from some young guy called Ken Thompson: "The const keyword only confuses library interfaces with the hope of catching some rare errors". I'm sure that all readers of JoS are careful to be "const correct" at all times, but it’s kind of depressing to see this sort of misleading garbage from a smart-alec know-nothing of the younger generation. It's almost as if none of the giants of computer science - the likes of McConnell, Booch, Meyers, and Sutter - had never written a word.

Grumpy old fart
Sunday, July 11, 2004

Thank you for that quote. I've been looking to deep six this plague upon the land that is const correctne swhich has never helped and only produced a castle of troubles for these old fingers.

Dennis Atkins
Sunday, July 11, 2004

I find that the primary benefit of using the 'const' modifier is improved code comprehension.  It's one more tool in the toolbox of self-documenting-code techniques.

Oren
Sunday, July 11, 2004

The problem is that in C/C++ it is too confusing to bother with.  I am going to make the immodest claim I know every language lawyer trick under the sun (at least in C, a lot of C++), but the const thing still is not intuitive.

I know what everything means, but it takes effort to read it.  Most everything else is intuitively obvious.

QUICK:

Without thinking, scan through these lines and tell me which ones are the same and what means what, and which ones are syntax errors:

const int* const p;
int const* p;
const* int p;
int* const p;
const int* p;
const int const* p;

I think const probably works better in other languages.

I did have someone break it into 3 cases, which made sense, but the codebase I usually work with does not use it so I haven't been religious about it.

Another problem is that converting non-const correct code to be const correct is a big bitch.  It is not something you can do very easily or mechanically on any reasonably sized codebase.

Roose
Sunday, July 11, 2004

const-correctness is required in some cases.

I have implemented an in-core transaction system for an application that I'm working on.  Basically, you can make changes to the in-core state of the app, then either commit or roll back the changes.

This is implemented using a smart pointer system. Each object in the database has a pointer to a "back up" object, to which it is copied prior to being modified. To commit, zero out the saved object pointers.  To roll back, copy the saved object back on to the original.  The Boehm garbage collector is used to manage the memory.

The smart pointer implements the following:

const T &operator*()
const T *operator->()
T *operator()(Xaction *xact)

The last operator records the modification in a transaction object.  It is designed to be used as:

obj(xact)->method().

Point being, const-correctness is IMPERATIVE in this system, as the only methods you can invoke without using a transaction object are const methods, and you want to do this where possible to avoid unnecessary copies.

David Jones
Sunday, July 11, 2004

" and was shocked to come across this quote from some young guy called Ken Thompson"

... and you call yourself an old timer.

If that name means nothing to you then I feel sorry for you, and your claim to be an old timer/old fart is a load of nonsense.

WhatTimeIsItEccles
Sunday, July 11, 2004

Roose,

Actually there's a very simple way to memorize this. Check out, for example:

http://www.geocities.com/spur4444/prog/const_and_pointers.html

The fact that C and C++ have complex concepts doesn't mean one shouldn't use them. IF you're looking for complex syntax, see Perl, and it is (justly !) one of the most popular languages around.

Eli Bendersky
Sunday, July 11, 2004

It's funny how people who can't (or won't) understand something, even if it is simple as const, throw it by the wayside as being 'stupid' or 'complicated', in order to satisfy their own ego.

It's simple and useful.  What if you want to make sure some dummy down the line doesn't modify a value he shouldn't modify?  What if you want to pass objects by reference, but their values shouldn't change?  What if you're shipping a redistributable library, and you want to indicate whether some objects are in/out/in-out?  All good occasions for const.

Good Code Uses Const
Sunday, July 11, 2004

> Another problem is that converting non-const correct code to be const correct is a big bitch.  It is not something you can do very easily or mechanically on any reasonably sized codebase.

That's an argument in favour of writing const-correct code in the first place. Although you *can* do it fairly mechanically:

void refactor()
{
    Change an top-layer method or parameter to const;

    while (you now see new compiler errors because the newly-const top-layer method calls some non-const lower-level methods or because the newly-const parameter is passed as a non-const parameter to lower-level methods)
    {
        Fix compiler errors from step 2, by redeclaring these lower-level methods and/or parameters as const;
    }
}

Christopher Wells
Sunday, July 11, 2004

Ignore const correctness?

YOU'RE FIRED!

sid6581
Sunday, July 11, 2004

My thoughts exactly.

Craig
Sunday, July 11, 2004

"That's an argument in favour of writing const-correct code in the first place."

Not trying to be bitchy, but do you program as a profession?  Don't you ever work on other people's code?

I would like to be const correct, but there is no chance of it with the code I work with.  I would guess that about 75% of code in shipping software is not const correct.

Hell I even read that Python is not const correct at all, and it is quite good code.

If you are really working then generlly you can't be to anal about things like this!

Roose
Sunday, July 11, 2004

Hey Roose got a question for ya.  You said in a previous thread that you worked for EA (Electronic Arts?).  Now I'm wondering if you write games code?  If so I'm wondering if you think games code is more loosely written than business code or commercial business software?  Most people say game coding is more difficult so I would assume game coders keep their code tight.

jj
Sunday, July 11, 2004

> Not trying to be bitchy, but do you program as a profession? Don't you ever work on other people's code?

I do, and yes I do.

Two projects ago, I was the lead developer ... so I set the coding standards for new hires ... which included const correctness.

One project ago, I was a new hire myself, helping to write a new module to some existing code. I noticed when reading it that much of the existing code wasn't const-correct ... but I was implementing the code for some new interfaces ... and when I did the coding/design review for those interfaces after someone coded them, I gave them feedback as to how to make the interfaces const-correct, the author of the interface complied, and so my implementation of that interface was able to be const-correct as well.

> I would like to be const correct, but there is no chance of it with the code I work with.

Me too, now ... my current project is C#, which doesn't support const-correctness.

> If you are really working then generlly you can't be to anal about things like this!

I'm as anal as I'm allowed to be (by the language, and the interfaces that I'm writing to), when I'm coding. For example, in C++ I prefer to use a reference instead of pointer ... except when I must use pointer instead of reference.

Christopher Wells
Sunday, July 11, 2004

Consts are lame. People who are into consts SUCK.

:-P

Dennis Atkins
Sunday, July 11, 2004

Programming is lame. Consts are a crutch.

Christopher Wells
Sunday, July 11, 2004

Yeah, you said it.

Dennis Atkins
Sunday, July 11, 2004

"If that name means nothing to you then I feel sorry for you, and your claim to be an old timer/old fart is a load of nonsense. "

That's the problem with message boards - it's hard to tell sarcasm from sheer stupidity.  Personally, I think Grumpy Old Fart is pulling our legs.

Yet another anon
Monday, July 12, 2004

I don't find it hard to tell the difference when they make it so darn obvious. Er, hello people! Wake up you load of sourpusses!

Tony Chang
Monday, July 12, 2004

Hey, grumpy old fart,

There was one time I went out fishing all day and I didn't catch anything. And then there was this other time when I took my rowboat out on the lake and I didn't even bring any fishing gear and these wide mouth bass were just leaping out of the lake straight into the boat one after another!! They practically sank the rowboat there was so many of them. Anything like that ever happen to you?

Tony Chang
Monday, July 12, 2004

> but it’s kind of depressing to see this sort of misleading
> garbage from a smart-alec know-nothing of the younger
> generation

Why is it that when an older person disagrees with a younger person, it's because they are a smart-alec know-it-all youngin' who needs to learn to respect his elders....and when a younger person disagrees with an older person, it's because they are an old gaffer who has lost touch with the times and is quickly becoming senile?  *sigh*

As for const in C#...it's not there, but there is the readonly modifier.  I'm not familiar with C/C++, so I'm sure there are nuances which 'readonly' doesn't cover, but it's something :)

Joe
Monday, July 12, 2004

> I'm not familiar with C/C++, so I'm sure there are nuances which 'readonly' doesn't cover, but it's something

FYI, the C# readonly lets you declare a member data field as readonly (can only be modified by the class constructor); whereas the C++ const can also be applied to:

- parameters (you're not allowed to modify this thing which I'm passing to you as a parameter)
- return values (you're not allowed to modify this thing which I'm returning to you from my method)
- local variables
- methods (this method is guaranteed to not modify any member data of "this", and may therefore be called even when you have a const-reference to the object).

Christopher Wells
Monday, July 12, 2004

Thanks for the info...

So anyone know why they decided not to include const in C#?  I imagine it's supported in MSIL, right? (or not?)

Joe
Monday, July 12, 2004

Hey,  WhatTimeIsItEccles, I Googled for "Ken Thompson". OK, so the kid's written some kind of Linux clone, that still doesn't mean he knows what he's talking about when it comes to const correctness. I agree with sid6581, the guy should be fired.

Grumpy old fart
Monday, July 12, 2004

Ken Thompson's address is 666 Mountain Ave. If you didn't suspect him of being the antichrist for saying const correctness is bad, at least there can be no doubt now.

sid6581
Monday, July 12, 2004

Joe: MSIL and the CLI don't support const except for real constants, which is also why we won't see it anytime soon in a .NET language. The readonly/initonly modifier is supported, that's all.

Chris Nahr
Tuesday, July 13, 2004

Okay, that was a bit confused. To clarify:

-- C# has a const keyword but this is actually equivalent to #define in C++, not const in C++. That is, expressions are evaluated at compile time and their values are stored as MSIL literals.

-- MSIL doesn't have any const keyword whatsoever. Constants are literals, nothing else. Names always signify modifiable values.

-- MSIL does have the initonly keyword which is the equivalent of readonly in C# and signifies a field that may be set only by a constructor.

Also, you can use const correctness with Managed C++ as usual, but the const correctness does not propagate to any non-C++ modules (including the Framework library) that are linked with your program.

Chris Nahr
Tuesday, July 13, 2004

*  Recent Topics

*  Fog Creek Home