Do we really need const?

Bruce Adams tortoise_74 at yeah.who.co.uk
Mon Sep 17 14:42:14 PDT 2007


Robert Fraser Wrote:

> Bruce Adams Wrote:
> > The problem with this is it isn't easy to tell how many problems would have been prevented if you are a regular user of const because they've been prevented. One exercise to try would be to take a large project and replace const with an empty string and see what the code looks like, then count the number, type and severity of potential errors relating to this absence. The problem is that doesn't take into account the dynamic nature of programming. Far more mistakes occur during development than you might be able to find in a finished product.
> > 
> > 
> 
> I think this is a similar argument to the one used to argue that static typing prevents a lot of bugs that dynamic typing might introduce. Many people who have worked in a dynamically-typed language (even on a bigger project) will confess that the gain in productivity of not having to constantly specify interfaces, being able to "blindly" pass things around, etc., create a better cost-to-benefit ratio than static typing. In fact, it's the same argument used to justify checked/specified exceptions in Java.
>
The same argument can be applied to unit testing and contracts as well but less successfully.

Dynamic / scripting languages do let you write code faster but at the expense of potentially being less reliable / robust. C++ is at the other end you over engineer and get reliable robust code - slowly. In an ideal language we could do both. "auto" almost gives us the best of both but we're currently missing an "any" type that would implicitly convert itself as freely as its dynamic language counterpart (at the expense of run-time performance).

 
> The potential for a bug doesn't mean that that potential will be used, and the productivity benefits of NOT having const might be greater.
>
I agree the case is not proven but unlike static/dynamic typing constness is an option. You can use it or not. In theory you can add it to an implementation as sugar after knocking up a prototype without it.
Though, I personally don't advocate that kind of style.
I think it is very important to get constness right in the design of interfaces, under the bonnett it matters much less.
 
> That strcpy thing _is_ a good example, though it won't catch that many bugs in its usage, since src already needs to be const. But it does show a valid use-case, which is better than I can provide for my argument ;-P.

Your toadying will not go unrewarded when I am emperor ;)



More information about the Digitalmars-d mailing list