Do we really need const?

Robert Fraser fraserofthenight at gmail.com
Mon Sep 17 23:34:15 PDT 2007


Walter Bright Wrote:

> Robert Fraser wrote:
> > I'd like to pose a question to those who have used C++'s const: do
> > you feel that it has saved more time by preventing bugs than it has
> > taken by being forced to type it all the time, and the time spent
> > when it has to be removed all throughout a hierarchy, as inevitably
> > has to happen at least once? That is, const-correctness is a time
> > investment, so do you feel that investment has paid off for you?
> 
> At the upcoming http://www.astoriaseminar.com, I think I'll do some 
> asking around on this issue. There'll be a lot of C++ diehards there.
> 
> 
> > I can say that working in Java, I have _never_ felt that if I pass a
> > class reference that was "constant" in nature to a method written by
> > somebody else or even to entirely different subsystem, that the
> > invariantness contract, specified only in the documentation, would be
> > broken.
> 
> Some Java professionals have reported problems with not being able to 
> specify const classes. These people work on Java programs with large teams.

I work (well, intern...) on a Java project with a team of ~35 developers. Can't say I've ever really wanted to constify something, although needing to synchronize everything when in doubt does get annoying (and as I keep telling my boss hurts performance dramatically, but they're big fans of the "throw more hardware at it" form of performance refactoring) so knowing something is INVARIANT might occasionally be helpful. But as a purely interface thing, I think docs serve better.
> 
> > Compiler checks in that case end up being as useless and
> > annoying as checked exceptions.
> 
> I don't think that's the same issue. The problem with checked exceptions 
> is that suppose you have functions A, B, C, where A calls B, and B calls 
> C. Now, you throw a new exception in C, and catch it in A. Arggh, you've 
> got to change B.
> 

Same with const, but top-down. Make something const in A, you have to make it const in B and C. If you ague that if B and C weren't changing it, then what if C's implementation changed in such a way as it dos make a change. Now, arrgh, got to go back and change it in B and A.

It's not nearly as bad as checked exceptions, but const for const's sake is excessive and useless. Hopefully, though, IDEs will be able to do it all for you (Descent will get there someday!).



More information about the Digitalmars-d mailing list