Do we really need const?

Bruce Adams tortoise_74 at yeah.who.co.uk
Mon Sep 17 00:55:29 PDT 2007


Bill Baxter Wrote:

> 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

Arguing that something isn't useful because you can live without it is
not sound. I can live without a dish-washer but I'd rather not have to do the washing up myself. Similarly the GIT people seem perfectly happy to write all their code in C, and from my brief glance at their code base its quite neat. Its a case of working smarter versus working harder. That said, its better to wait and deliberate and get a good const system rather than live with something that's not up to par.
 I can't really comment on pascal myself. I did do some modula-2 many years ago, which I consider to be 2 steps on from pascal in Wirths evolution (pascal, modula 1,2,3 then oberon). I think const is just used for variables assigned once and ideally at compile time when the expression permits it.

Regards,

Bruce.



More information about the Digitalmars-d mailing list