Fully transitive const is not necessary

Bill Baxter dnewsgroup at billbaxter.com
Wed Apr 2 14:37:30 PDT 2008


Walter Bright wrote:
> Sean Kelly wrote:
>> I agree that transitive const can achieve more demonstrably correct code
>> but I don't think it follows that this will necessarily improve 
>> productivity.
>> My experience with const in C++ is that it actually reduces productivity
>> because I spend more time dealing with the misapplication of const than
>> I gain from whatever bugs the application of const may have prevented.
>> In fact, in all my time as a programmer I can't think of a single 
>> instance
>> where I've encountered a bug that could have been prevented through
>> the use of const.
> 
> C++ const is more of a suggestion than anything verifiable by the 
> compiler and static analysis. This will become more obvious as C++ tries 
> to compete in the multiprogramming arena. The C++ compiler *cannot* help 
> with multiprogramming issues; the C++ programmer is entirely dependent 
> on visual inspection of the code, making C++ a tedious, labor intensive 
> language for multiprogramming.
> 
> How many times have you looked at the documentation for a function API, 
> and wondered which of its parameters were input and which were output?
> 
> When you talk about the "misapplication" of const, does that mean 
> failure to understand what the specific API of a function is?

My guess is he means trying to call a function with a const value where 
the author forgot to put the const label on an argument.

So you go fix that function's signature.

And then find that 3 functions it calls also have incorrect signatures.

And then find 5 more that those call that are also incorrect.

Then you start thinking, wow, this library isn't const-correct at all, 
maybe I should just back out all my changes and cast away const at the 
top.  Or maybe I should slog it through and fix the library?

That kind of thing is all too familiar to C++ programmers, and quite 
annoying to deal with.   So it leaves an impression.


On the other hand, const preventing you from calling some method you 
shouldn't be calling (and thereby preventing a bug) doesn't leave much 
of an impression.  You just go "duh -- of course I can't call that" and 
move on.  Also bugs caused by data changing when you didn't expect it to 
don't usually jump out as due to lack of const.  Instead you go "doh! I 
shouldn't change that there" or "doh! I shouldn't call that function 
here".  So unfortunately I think the value of const is very difficult to 
judge using subjective measures.

But it's definitely somewhere in between "no use at all" and "absolutely 
required".  :-)

--bb



More information about the Digitalmars-d mailing list