C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What Value Does Const Correctness Offer
Goto page 1, 2, 3 ... 14, 15, 16  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jessica B
Guest





PostPosted: Sat Sep 11, 2004 5:00 am    Post subject: What Value Does Const Correctness Offer Reply with quote



I was having an argument recently with a fellow programmer.
His take was that const correctness does not add significant
value to your software, and, given that it is quite burdensome
to maintain, he questioned whether it should be ignored.

My primary argument in its favor is that it gives additional
information to the compiler about your intentions, which
can allow the compiler to detect bugs, however, on challenge
I really couldn't remember an example of a bug that it
prevented (from my own practical expereience.)

I also argued that const correctness was not generally
difficult to maintain, but, in all honesty, it is a
significant amount of work to maintain it. Just off the
top of my head I would say that about 10% of syntax errors
are const related errors. (And the large majority of them
only show an error in const correctness, rather than
offering any productive value.)

Often some of the strangest syntax errors arise from const
correctness errors, and they sometimes take some
detangling. Further, many external libraries one uses
often have const correctness errors in them, which have
to be eliminated by tangled casts and inappropriate
mutables.

I wonder if any of you can offer some convincing arguments
for the effort involved in const correctness. Or perhaps
offer some class of bug caught by it use.

Thanks.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Sun Sep 12, 2004 10:09 am    Post subject: Re: What Value Does Const Correctness Offer Reply with quote



Jessica B wrote:
Quote:
I was having an argument recently with a fellow programmer.
His take was that const correctness does not add significant
value to your software, and, given that it is quite burdensome
to maintain, he questioned whether it should be ignored.

You can't ignore it, because you are forced to use const anywhere you
might need to bind a reference to a temporary.

Quote:
My primary argument in its favor is that it gives additional
information to the compiler about your intentions, which
can allow the compiler to detect bugs, however, on challenge
I really couldn't remember an example of a bug that it
prevented (from my own practical expereience.)
snip


It should prevent you from putting the source and destination
arguments to memcpy or std::copy the wrong way round (note that the
correct order differs between the two functions!).

--
Ben Hutchings
If God had intended Man to program,
we'd have been born with serial I/O ports.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Walter
Guest





PostPosted: Sun Sep 12, 2004 10:09 am    Post subject: Re: What Value Does Const Correctness Offer Reply with quote




"Jessica B" <jessica_boxer (AT) yahoo (DOT) com> wrote

Quote:
I was having an argument recently with a fellow programmer.
His take was that const correctness does not add significant
value to your software, and, given that it is quite burdensome
to maintain, he questioned whether it should be ignored.

My experience is similar to your colleague's. Maintaining const correctness,
while generating lots of errors at compile time, has never found an actual
bug for me. Peppering declarations with 'const' doesn't look good and tends
to visually obfuscate what the types are. Const has numerous arcane and
sometimes inconsistent rules with how overloading and partial specialization
works (sometimes it's ignored, sometimes it isn't). And lastly, const
doesn't help the optimizer, since there are so many legal ways to get around
it.

Nevertheless, many programmers swear by const and firmly believe it improves
program readability and that it does find real bugs. I'm just not one of
them <g>. This is why D makes const a storage class, rather than a type
modifier. A const storage class means that the data is semantically
read-only, and could be placed in ROM or in a read-only data segment. The
optimizer can rely on it being constant.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Balog Pal
Guest





PostPosted: Sun Sep 12, 2004 3:59 pm    Post subject: Re: What Value Does Const Correctness Offer Reply with quote

"Jessica B" <jessica_boxer (AT) yahoo (DOT) com> wrote


Quote:
I was having an argument recently with a fellow programmer.
His take was that const correctness does not add significant
value to your software, and, given that it is quite burdensome
to maintain, he questioned whether it should be ignored.

Rubbish.

Quote:
My primary argument in its favor is that it gives additional
information to the compiler about your intentions, which
can allow the compiler to detect bugs, however, on challenge
I really couldn't remember an example of a bug that it
prevented (from my own practical expereience.)

Well. IMHO you can just safely ignore the guy, really. If one thinks a
safety belt is just there for some idiots have spare time tomake additional
rules -- you will not convince them anyhow.

And 'const' in C++ is not just there to detect bugs, but serves a plenty of
other purposes -- like communicates intent, documents interface, allows
overloads, etc. And built-in rules of the language will make your life a
hell if you never write 'const' in your classes and functions. As
literals, temporaries will not bind to nonconst refs. And passing by value
will be slow for one set of cases and completely inappropriate for others.

Quote:
I also argued that const correctness was not generally
difficult to maintain,

It is trivial to maintain. What is cumbersome -- picking up some oldtimer
C library and convert it to a const-correct C++ package. But when you write
stuff from scratch, it's completely natural to add 'const' wherever your
*intetntion* is not to change the object. (And I observe it is the case for
the majority of situations.)

Quote:
but, in all honesty, it is a
significant amount of work to maintain it. Just off the
top of my head I would say that about 10% of syntax errors
are const related errors.

You mean mistyping const to cosnt?

Anyway, IMHO the syntax-error-pervcentage is not an interesting measure.
What counts the overall *time* spent on work. I find the time spent on
typing the extra letters, and correct the mistypes, and also misthinks, the
recompiles just caused by that -- all that time is still under the
measurable limit.

Quote:
(And the large majority of them
only show an error in const correctness, rather than
offering any productive value.)

Okey, then pick a single case of the minority, and estimate the time it
would cause going uncaught. My guess it will be by one magnitude more than
all the time consumed by the majority.

Quote:
Often some of the strangest syntax errors arise from const
correctness errors, and they sometimes take some
detangling.

Example?

Quote:
Further, many external libraries one uses
often have const correctness errors in them,

That is indeed a problem -- but the fact you face defective libraries is
hardly an excuse to write more defective software of your own. Smile
The general solution if you can't drop that library or manage to get it
fixed by supplier is to create some wrappers, that are const-correct.
[btw most shops use external libraries through domestic wrappers anyway, so
it is rarely a new and extra work.]

Quote:
which have
to be eliminated by tangled casts and inappropriate
mutables.

The important rule is to have those casts (just as any such stuff) AWAY from
the user code. :)

Quote:
I wonder if any of you can offer some convincing arguments
for the effort involved in const correctness. Or perhaps
offer some class of bug caught by it use.

Hm, it's hard to show the bugs for a simple reason -- they are caught so
early in the process and so easy to correct wihout cost, likely no one
bothers to document or even remember them. See the estimates for a cost
of bug caught at design/coding/autotests/review/other tests/production,
there are steep jumps.

OTOH you can't hack that problem in isolation. The issue of 'code shall
reflect the intent' and 'the design shall be clear about what changes what'
is essential to write correct stuff. If you have people who can't think
that way, sure tools that help will really just cause headache, and yield no
real benefits.

Paul



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Francis Glassborow
Guest





PostPosted: Sun Sep 12, 2004 3:59 pm    Post subject: Re: What Value Does Const Correctness Offer Reply with quote

In article <bPJ0d.74330$3l3.5033@attbi_s03>, Walter
<walter (AT) digitalmars (DOT) nospamm.com> writes
Quote:
My experience is similar to your colleague's. Maintaining const correctness,
while generating lots of errors at compile time, has never found an actual
bug for me. Peppering declarations with 'const' doesn't look good and tends
to visually obfuscate what the types are. Const has numerous arcane and
sometimes inconsistent rules with how overloading and partial specialization
works (sometimes it's ignored, sometimes it isn't). And lastly, const
doesn't help the optimizer, since there are so many legal ways to get around
it.

You can't be serious:-)

double const pi(3.14159265);

is a very different beast to

double value(2.1);

The compiler 'knows' that unless the programmer does something silly
like take the address of pi that it need not provide storage for it,
however in the case of value, it better provide storage unless it can
categorically prove that value never ever changes.

Equally, it is fully entitled to ROM objects that are declared as const
and if the programmer tries to change the object the programmer is
wrong, not the compiler. We even introduced an extra keyword, mutable,
in order to deal with the case of logically const objects that would
still have a mutable element.

The only people I know that have a problem with const are those that
come from a C background. Novices quickly learn to write const correct
code if correctly introduced to the concept.

Now the problem is that const is an overloaded keyword. At top level it
effectively means immutable (and so ROMable) at other levels it means
'read only'.

BTW that is yet another reason for placing const at the extreme right of
the type being qualified; if there is then any other type element (* or
&) to its right it the overall type is not immutable, only write
protected.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Antoun Kanawati
Guest





PostPosted: Sun Sep 12, 2004 4:06 pm    Post subject: Re: What Value Does Const Correctness Offer Reply with quote

Jessica B wrote:

Quote:
I was having an argument recently with a fellow programmer.
His take was that const correctness does not add significant
value to your software, and, given that it is quite burdensome
to maintain, he questioned whether it should be ignored.

My primary argument in its favor is that it gives additional
information to the compiler about your intentions, which
can allow the compiler to detect bugs, however, on challenge
I really couldn't remember an example of a bug that it
prevented (from my own practical expereience.)
[snip]


const correctness enables you to express the concept of immutable
data, and have the compiler support it; for example, if your
data is in ROM.

If you use memory mapped I/O, writing to certain memory locations
can have unintended side effects. With the proper const specifications
you can avoid such situations at compile time.

The promise of constness increases the probability of better
optimization.

And, if you're dealing with big objects, it saves you a lot
of copying to pass a const&.

There is little question that constness and its requirements
have been introduced to solve some real issues, and many love
it. It's a lot easier to deal with problems at compile time
than after releasing to the customer.
--
Antoun Kanawati
[email]antounk.at (AT) comcast (DOT) dot.net[/email]
[remove .dot and .at before use]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sun Sep 12, 2004 4:08 pm    Post subject: Re: What Value Does Const Correctness Offer Reply with quote

In article <642e01d8.0409101150.63271032 (AT) posting (DOT) google.com>, Jessica B
<jessica_boxer (AT) yahoo (DOT) com> writes
Quote:
I was having an argument recently with a fellow programmer.
His take was that const correctness does not add significant
value to your software, and, given that it is quite burdensome
to maintain, he questioned whether it should be ignored.

Let me guess that he came from a C background where they (with good
reason) talk of const poisoning. Without function overloading const is
costly because it is often possible for an object to acquire const
protection by 'accident' Such possibility means that it then has to be
cast away, and the programmer now has the responsibility to ensure that
the cast is valid (i.e. do not try to cast away const qualification of
an object that was declared as const, while safely casting away an
acquired const attribute)

Quote:

My primary argument in its favor is that it gives additional
information to the compiler about your intentions, which
can allow the compiler to detect bugs, however, on challenge
I really couldn't remember an example of a bug that it
prevented (from my own practical expereience.)

You mean you have never had to deal with accidental attempts to modify a
string literal? The concept of const in C++ is also essential to
allowing the passing of object by reference when the object must not be
changed by the function being called. Again this is an issue for C++ but
not for C.

Quote:

I also argued that const correctness was not generally
difficult to maintain, but, in all honesty, it is a
significant amount of work to maintain it. Just off the
top of my head I would say that about 10% of syntax errors
are const related errors. (And the large majority of them
only show an error in const correctness, rather than
offering any productive value.)

Just because 10% of your syntax errors are caused by breaches of const
correctness does not make const correctness hard to maintain but it does
mean that the compiler is identifying places in your code where you are
doing something suspect.

Quote:

Often some of the strangest syntax errors arise from const
correctness errors, and they sometimes take some
detangling. Further, many external libraries one uses
often have const correctness errors in them, which have
to be eliminated by tangled casts and inappropriate
mutables.

So? Incompetence in developers may be a reason not to buy their products
but it certainly isn't a reason to allow their incompetence by changing
the language.

Quote:

I wonder if any of you can offer some convincing arguments
for the effort involved in const correctness. Or perhaps
offer some class of bug caught by it use.

But like exception safety, it isn't a problem once one changes ones
coding habits. Those who write C++ as if it were C will have problems,
but trying to write C++ without const correctness would result in a
mess. const wasn't introduced to C++ on a whim, though perhaps C would
have been better off had it not copied the keyword and then watered down
its semantics. C might have found it better to have made const a keyword
and then specified that it did nothing in C but was only there for C++
compatibility.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Walter
Guest





PostPosted: Mon Sep 13, 2004 10:28 am    Post subject: Re: What Value Does Const Correctness Offer Reply with quote


"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote

Quote:
You can't be serious:-)

double const pi(3.14159265);

is a very different beast to

double value(2.1);

The compiler 'knows' that unless the programmer does something silly
like take the address of pi that it need not provide storage for it,
however in the case of value, it better provide storage unless it can
categorically prove that value never ever changes.

Equally, it is fully entitled to ROM objects that are declared as const
and if the programmer tries to change the object the programmer is
wrong, not the compiler.

In this case, using const as the 'top level', const is essentially being
used as a storage class, and it can be put in ROM. I was referring, however,
to things like pointer to const. The optimizer cannot rely on whatever the
pointer is pointing to being the same value after, say, a function call or
an assignment through another pointer.


Quote:
We even introduced an extra keyword, mutable,
in order to deal with the case of logically const objects that would
still have a mutable element.

I've always been bemused by mutable - it seems to be driven by the notion
that const things are better, so we'll enable things to be declared as const
even though they're not <g>. Isn't mutable is an admission of the semantic
failure of const? Maybe there's a compelling argument for it I haven't seen.


Quote:
The only people I know that have a problem with const are those that
come from a C background. Novices quickly learn to write const correct
code if correctly introduced to the concept.

I come from a C background <g> (and BASIC, FORTRAN, Pascal, and asm), and I
know how to write const correct code. I just haven't found a practical
benefit to it - it doesn't find real bugs, it doesn't look good, and it
doesn't help code generation.


Quote:
Now the problem is that const is an overloaded keyword. At top level it
effectively means immutable (and so ROMable)

D keeps this meaning, and so making const a storage class makes sense.

Quote:
at other levels it means 'read only'.

BTW that is yet another reason for placing const at the extreme right of
the type being qualified; if there is then any other type element (* or
&) to its right it the overall type is not immutable, only write
protected.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Pavel Vozenilek
Guest





PostPosted: Mon Sep 13, 2004 10:29 am    Post subject: Re: What Value Does Const Correctness Offer Reply with quote


"Francis Glassborow" wrote:

Walter <walter (AT) digitalmars (DOT) nospamm.com> writes
Quote:
My experience is similar to your colleague's. Maintaining const
correctness,
while generating lots of errors at compile time, has never found an
actual
bug for me.

You can't be serious:-)

double const pi(3.14159265);

is a very different beast to

double value(2.1);

I guess the experience is more about const qualified member

functions than variables.

Const qualifier for member function helps
to catch errors (though not as often I would like)
but single change somewhere deep in code can
propagate across whole project with dozens of places
needing update.

That's how I understand the complaint.

/Pavel




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dave Harris
Guest





PostPosted: Mon Sep 13, 2004 10:33 am    Post subject: Re: What Value Does Const Correctness Offer Reply with quote

[email]jessica_boxer (AT) yahoo (DOT) com[/email] (Jessica B) wrote (abridged):
Quote:
I wonder if any of you can offer some convincing arguments
for the effort involved in const correctness. Or perhaps
offer some class of bug caught by it use.

For me the main use is when you have a reference and aren't sure what the
effects of changing it would be. For example:

void move( Rect *pRect ) {
pRect->topLeft().x += 10;
}

Does this move the given rectangle 10 units to the left? It depends on
whether what topLeft() returns is a copy of the rectangle's point, or the
actual defining point itself. If you have garbage collection or are using
smart pointers, then a result can be a (smart) pointer and still be a
copy, and the examples are not so trivial.

Anyway, const cuts this Gordian Knot by disallowing it; by not allowing
you to change through the reference. It makes it much easier to use
references and shared data without unwanted side effects. When I first
started using Smalltalk (a language which has garbage collection, but no
static type checks) I found const type errors to be the ones which took
most of my debugging time. (Admittedly when I returned to Smalltalk a few
years later my experienced changed, so this may be a matter of programming
style or competence).

The importance of "const", in the broader sense of state-free programming,
is such that the entire school of Functional Programming is largely based
upon the idea. I find it quite useful when organising my thoughts and code
to distinguish between operations which may change the program's state,
and operations which will not. It helps when debugging live code, for
example - if a routine is const, I know I can set the program counter back
and re-execute a call to it without changing anything. It helps when
reasoning about the code.

Whether all this is compelling is another matter. I agree the cost is
high. I am especially bothered by the frequent need to replicate code, to
have const and non-const versions of the same function.

-- Dave Harris, Nottingham, UK

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Kanze
Guest





PostPosted: Mon Sep 13, 2004 10:38 am    Post subject: Re: What Value Does Const Correctness Offer Reply with quote

"Walter" <walter (AT) digitalmars (DOT) nospamm.com> writes:

Quote:
"Jessica B" <jessica_boxer (AT) yahoo (DOT) com> wrote in message
news:642e01d8.0409101150.63271032 (AT) posting (DOT) google.com...
I was having an argument recently with a fellow programmer. His
take was that const correctness does not add significant value to
your software, and, given that it is quite burdensome to
maintain, he questioned whether it should be ignored.

My experience is similar to your colleague's. Maintaining const
correctness, while generating lots of errors at compile time, has
never found an actual bug for me. Peppering declarations with
'const' doesn't look good and tends to visually obfuscate what the
types are. Const has numerous arcane and sometimes inconsistent
rules with how overloading and partial specialization works
(sometimes it's ignored, sometimes it isn't). And lastly, const
doesn't help the optimizer, since there are so many legal ways to
get around it.

I can understand your point of view; I find it particularly agravating
that I sometimes have to provide two exactly identical functions (e.g.
like operator[]), one of which can be called on a const object, and the
other which allows modification through the returned reference. On the
other hand, I find that when joined with the refusal to bind a const
reference to a temporary, it does catch some errors -- cases when an
unintentional implicit conversion reared its head. IMHO, the better
solution would have been to get rid of implicit conversions, but
somehow, I don't think that that would have flown.

At any rate, given the current rules, you really don't have the choice
in general. The choice you might have is whether to use const, and
enforce const correctness, for entity objects, i.e. objects you pass by
reference or pointer, and never copy. Even their, all things
considered, I prefer the consistency of const correctness.

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
David B. Held
Guest





PostPosted: Mon Sep 13, 2004 10:39 am    Post subject: Re: What Value Does Const Correctness Offer Reply with quote

Jessica B wrote:
Quote:
[...]
My primary argument in its favor is that it gives additional
information to the compiler about your intentions, which
can allow the compiler to detect bugs, however, on challenge
I really couldn't remember an example of a bug that it
prevented (from my own practical expereience.)

How about any kind of ownership transfer scenario? For instance,
ownership probably should not leave a const object, since that
would imply that the object should lose its reference to the object.
Thus, a perfectly obvious and ubiquitous example would be
std::auto_ptr<>. You should never be able to move from a const
auto_ptr<>.

Quote:
I also argued that const correctness was not generally
difficult to maintain, but, in all honesty, it is a
significant amount of work to maintain it. Just off the
top of my head I would say that about 10% of syntax errors
are const related errors. (And the large majority of them
only show an error in const correctness, rather than
offering any productive value.)

Do you say that because you've written the code in a const-free
form and obtained correct programs, or because the programs that
forced you to use const-correctness turned out to be correct?
Make sure you don't infer causality in the wrong direction.

Quote:
Often some of the strangest syntax errors arise from const
correctness errors, and they sometimes take some
detangling. Further, many external libraries one uses
often have const correctness errors in them, which have
to be eliminated by tangled casts and inappropriate
mutables.
[...]

Is that the fault of const or people who don't use it correctly?
Does that fact that people abuse pointers imply that pointers are
a flawed language concept that should be removed? (Well, I guess
Java has answered that question).

Dave

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Clem Dickey
Guest





PostPosted: Mon Sep 13, 2004 7:59 pm    Post subject: Re: What Value Does Const Correctness Offer Reply with quote

"Balog Pal" <pasa (AT) lib (DOT) hu> wrote


Quote:
it's completely natural to add 'const' wherever your
*intention* is not to change the object. (And I observe it is the case for
the majority of situations.)

Agreed. And therefore const correctness would be simpler and programs
would be shorter if "const" were the default, and "var" modifier to
designated modifiable items. If not for backwards compatibility ...

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jessica B
Guest





PostPosted: Mon Sep 13, 2004 8:00 pm    Post subject: Re: What Value Does Const Correctness Offer Reply with quote

Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote

Quote:
In article <642e01d8.0409101150.63271032 (AT) posting (DOT) google.com>, Jessica B
[email]jessica_boxer (AT) yahoo (DOT) com[/email]> writes
You mean you have never had to deal with accidental attempts to modify a
string literal?

Perhaps once or twice. I rarely use string literals
anyway, since I produce shrink-wrap software that must
be localized, meaning text strings usually come from
an external source into a string variable.

Let me start by clarifying the question. If is not
whether const is useful or necessary, clearly it is.
It is whether const correctness throughout the system
is necessary or, more specifically, a net benefit.

Quote:
The concept of const in C++ is also essential to
allowing the passing of object by reference when the object must not be
changed by the function being called. Again this is an issue for C++ but
not for C.

But that begs the question. You say const is essential
for that, which is undoubtedly true, but the goal you set
out (making in immutable) is the very matter under question.

Quote:
Just because 10% of your syntax errors are caused by breaches of const
correctness does not make const correctness hard to maintain but it does
mean that the compiler is identifying places in your code where you are
doing something suspect.

But if, as the premise of the thread suggests, const correctness
adds little value, then the extra work involved in
the aforementioned syntax problems is still significant
enough to offset any minimal benefit conveyed. Of course
that is a judgment call. Const obviously is going to catch
some bugs, with a cost of n hours work saved, and the
challenge of maintaining cost is m hours. The question is
whether in practice n > m. If n -> 0, and m>0 then we
have an answer.

Quote:
Often some of the strangest syntax errors arise from const
correctness errors, and they sometimes take some
detangling. Further, many external libraries one uses
often have const correctness errors in them, which have
to be eliminated by tangled casts and inappropriate
mutables.

So? Incompetence in developers may be a reason not to buy their products
but it certainly isn't a reason to allow their incompetence by changing
the language.

I find that a curious statement. What if you have no
choice but to use a particular library (as is often
the case in many, many situations)? What does one do
then? Toss the project out of some sense of linguistic
purity?

This is not some meaningless debate either. There are
many such real world problems. For example, one of the
most widely used libraries in the world, the Microsoft
Foundational Class library has at least a half a dozen
const correctness issues I know of. Should I not use it
because of that reason? Should I really write a mega
wrapper around it?

Further I can think of several features added to the
language to facilitate the use of errant or old libraries
such as extern "C" or the use of the "using namespace std"
idiom, both of which fall under this category, and that's
what I can think of off the top of my head. (One facilitates
the linking of old C libraries, and the other facilitates
old code that does not use namespace qualification.)

Anyway, the question is not one of changing the language
rather it is a question of whether a particular feature
of the language should be used. And that is an entirely
different thing.

Quote:
But like exception safety, it isn't a problem once one changes ones
coding habits.

But that isn't true. Regardless of ones coding habits
it still takes extra cognitive effort to maintain
const correctness. And I might add that to suggest that
providing exception safety is easy once one has good
habits is not a viewpoint I agree with at all. If I
remember correctly Stroustrup wrote a whole appendix
in "The C++ Programming Language" on the subtleties of
exception safety in the standard library.

I find it curious indeed that despite the fact that
there is almost universal acceptance of the dogma
that const correctness is key to great software, (and
I am an priest of that church also), that there is
no good quality list of reasons why.

For many of the other good software practices I use
I can clearly articulate a list of reasons for their
use, that are clear, irrefutable, and legion. For
example, can you give me a list of reasons to prefer
exceptions over error return codes? I can give you
at least a dozen excellent reasons. What about
encapsulation? Why shouldn't I use protected member
variables? Why should I avoid multiple inheritance?
Why should I prefer the standard library over roll
your own code? I can give lists of reasons for these
that are simply beyond question. However, const
correctness seems different.

Here are the reasons I have heard here:

* "it is needed for embedded programming", sure,
you need to put stuff in ROM, but that hardly
applies to programming in general, and seems more
like the sort of thing that the toolset should deal
with, in a pragma for example;

* "bind refs to temps need it" which seems to me to
be something that can be dealt with locally rather
than globally (i.e., const is useful, const correctness
not so much);

* "const pi = 3.14" again is important, but local;

* "const provides opportunities for optimization", which
it does in theory, however, I doubt it really translates
into anything significant in practice. For example:

class C { int f() const; };
int test(const C c)
{
return c.f() + c.f();
}

Would any compiler actually optimize out the second call to
f? I doubt there is a complier in existence that would. I
may be wrong, however, perhaps there are some aggressive
compilers that do. However, make c non const, and there is
no realistic way to expect a compiler to eliminate the second
call. For optimization to succeed requires a lot of stars
to line up just right. As I say, I doubt it happens much
in the real world. (Of course except for constant folding
const pi = 3.14; but that example doesn't count since it
doesn't require systematic const correctness to use.)


Even Myers in his Tips books says const correctness is
crucial, but doesn't really give a good explanation of
why.

I feel unsatisfied. Perhaps my problem is that after
reading the article linked below I wonder if the whole
concept of static type checking is more effort than it
is worth (written by Java maven Bruce Eckel -- not some
inexperienced kid from college.)

<http://mindview.net/WebLog/log-0025>

This article basically argues that static type checking
is a test applied by the compiler to test a small subset
of the correctness of a program. However, the cost of
maintaining and implementing it is large enough that it
slows the programmer down reducing the amount of unit
testing that can be done, and bang for buck, unit testing
is much more valuable than static type checking for
validating your software. To use pseudo math:

For some project P,
n is the number of hours saved by static type checking
m is the number of hours cost maintaining static checks
and f(x) is the number of hours saved by spending x hours
writing and running unit tests (which presumably declines
exponentially as x -> infinity.)

Static type checking is inefficient if

n < f(m)

and it is Eckel's contention in the above article that
n is much smaller than f(m). If he is right, that is a
pretty scary result.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Sep 13, 2004 8:01 pm    Post subject: Re: What Value Does Const Correctness Offer Reply with quote

In article <2qjhk1F102644U1 (AT) uni-berlin (DOT) de>, Pavel Vozenilek
<pavel_vozenilek (AT) yahoo (DOT) co.uk> writes
Quote:
I guess the experience is more about const qualified member
functions than variables.

But the two necessarily go hand-in-glove. The concept of immutable
objects only works if we have a way to guarantee that they are not going
to be violated.

Quote:

Const qualifier for member function helps
to catch errors (though not as often I would like)
but single change somewhere deep in code can
propagate across whole project with dozens of places
needing update.

Indeed, correcting a design fault often has that property.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2, 3 ... 14, 15, 16  Next
Page 1 of 16

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.