Do we really need const?

Bill Baxter dnewsgroup at billbaxter.com
Sun Sep 16 23:36:35 PDT 2007


Bruce Adams wrote:
 > charles Wrote:
 >
 >> Const is a god awful abomination that no language has gotten right, 
 >> the sooner its gotten rid of the better.  No one I work with has 
ever >> had a situation where const actually paid off.
 >>

> As a seasoned C++ programmer I and my colleagues regularly use the
> c++ flavour of const with no problems. It may not be the best 
> solution in the world but it works. I always found it a reasonably
> intuitive bit of syntactic sugar. That's not to say that we can't do
> better in D. I'd recommend reading Scott Meyers effective C++ books
> for a start. And perhaps we should review the deliberations that went
> into developing the C++ const system. 

> By the way, what languages other than C++ have some kind of const
 > mechanism? They've got to be worth a review too.

That's a very good point.  *Are* there any others?  Pascal seems to have 
some form of it (http://www.gnu-pascal.de/gpc/const.html).  Any Pascal 
devotees out there care to comment?  I managed to avoid ever learning 
Pascal.

Anyway, if one asserts that const is a critical feature for "enterprise 
code", yet there exist a large body of "enterprise code" written in 
languages that don't have const, then we have a contradiction.  It can't 
be all that critical if lots of successful software projects get written 
without it.

There's of course Java.  Lots of enterprise software is written in Java, 
and yet Java does not have const.

There is without a doubt some diagnostic benefit to const, but if it 
were such a great win overall wouldn't everybody be doing it?  Is it 
really worth the pain of having to create two versions of every 
function, and two flavors of every iterator, etc.?  I guess I've gotten 
used to it in C++ and certainly bought into the const-correctness 
mantra.  When I see C++ code that doesn't use const correctly my 
kneejerk reaction is always "sheesh -- amateur".  But I've never really 
taken a step back to try to seriously evaluate the cost-benefit tradeoff.

It seems from the discussion here the past week, there is no real 
multithreading benefit to be had from const/invariant.  'Pure' is where 
it's at for that.  So maybe we're just better off without the 
complexities of const.  I've certainly gotten used to the lack of const 
in Python, so why not in a C++-ish language?

--bb



More information about the Digitalmars-d mailing list