Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Oppinion on 'least priviledge', 'const correctness', etc. (http://www.velocityreviews.com/forums/t728747-oppinion-on-least-priviledge-const-correctness-etc.html)

Alexander 07-20-2010 01:00 PM

Oppinion on 'least priviledge', 'const correctness', etc.
 
Wherever I find something on the topic, these are considered positive.
Why? I only find it time-consuming. Could you respond (preferably on
comp.programming) why it can be considered as such, but motivated,
that is without responses like "it's good software engineering
practice", "it's just better", etc... I'm a learner, and I think now
is the best time to shape out practices and priorities.

Jorgen Grahn 07-20-2010 02:18 PM

Const correctness (was Re: Oppinion on 'least priviledge', 'constcorrectness', etc.)
 
["Followup-To:" header set to comp.lang.c++. Neither the Java nor the
comp.programming people want to read about const correctness, I'm sure.]

On Tue, 2010-07-20, Alexander wrote:
> Wherever I find something on the topic, these are considered positive.


Only these two, or do you include a number of other things under
"etc", unknown to us?

> Why? I only find it time-consuming. Could you respond (preferably on
> comp.programming) why it can be considered as such, but motivated,
> that is without responses like "it's good software engineering
> practice", "it's just better", etc...


Const specifically: a language feature I really like.

I guess you can say that it adds another dimension to the type system.
It's good for the same reasons that the rest of the static typing is
good. E.g. that we can have have Foo* and Bar*, not just void*.

You make more information about your intentions explicit, in the code,
for the benefit for the reader. And the compiler can check it.

> I'm a learner, and I think now
> is the best time to shape out practices and priorities.


Yes. For const, you don't really have a choice -- if you refuse to use
it, you'll be in constant conflict with other programmers working on
the code.

There are still, I think, old C programmers who reject const, but I
never heard of a C++ programmer who did.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Juha Nieminen 07-20-2010 04:06 PM

Re: Const correctness (was Re: Oppinion on 'least priviledge', 'const correctness', etc.)
 
In comp.lang.c++ Jorgen Grahn <grahn+nntp@snipabacken.se> wrote:
> Const specifically: a language feature I really like.
>
> I guess you can say that it adds another dimension to the type system.
> It's good for the same reasons that the rest of the static typing is
> good. E.g. that we can have have Foo* and Bar*, not just void*.
>
> You make more information about your intentions explicit, in the code,
> for the benefit for the reader. And the compiler can check it.


Sometimes constness can be used for efficiency.

It is possible for a class to have two member functions with an otherwise
identical signature (ie. name, parameters amount and types) except that one
is const and the other isn't. When the member function is called, the
compiler will choose which one of those two will be called depending on the
constness of the object, reference or pointer.

There are situations where this feature is taken advantage of for
efficiency: Sometimes the const version of the member function can be
made significantly more efficient because it can be made simpler due to
the fact that it doesn't change the object or, more usually, it can assume
that whatever it's returning will not be changed by the calling code.

(As an example, if eg. std::string implemented copy-on-write, then its
const version of operator[] can be significantly more efficient than its
non-const version because the const version doesn't need to deep-copy the
string even if it's currently being shared, while the non-const version
always has to deep-copy a shared string.)

Öö Tiib 07-20-2010 04:29 PM

Re: Oppinion on 'least priviledge', 'const correctness', etc.
 
On 20 juuli, 16:00, Alexander <alva...@gmail.com> wrote:
> Wherever I find something on the topic, these are considered positive.
> Why? I only find it time-consuming.


I have heard them named as "principle of minimal privilege" and "const
correctness".

Illustrative example: Imagine that you play classical click-around,
find-items, combine-and-use-them-to-proceed adventure game. You need
to use 5 items for winning it but the game throws at you 80 red
herring items too. Some of the items may lead you to wrong, useless
places or into inescapable situations (that game does not indicate any
way). Fun to click and try to combine them all and to use everywhere
and then to reload earlier saves and to retry? No. Most like it be
better if game does not give red herrings to them at all or gave lot
less than possible. So "more" is not always "better".

Same is with access restrictions in computer science. Anyone writing
module or class interfaces should take good care about it being
sufficient and complete for its purpose. It is done for protecting the
users of interface from need to know about various red herrings and
wrong places. Best is to give to user *only* such information and
resources and access that they need for legitimate usage purposes.
Also it is important to give them *everything* that they need for
legitimate usage purposes, otherwise they start to ask questions like
"is this game winnable at all". ;)

Of course it is initially time consuming for interface designer to
carefully arrange that access but it saves lot of time of the innocent
users of the interface. Also it gets easier when you have habit to do
it. If you write it all by yourself you may initially think that you
are the user yourself (and so not innocent) and so it does not apply
and habit is not needed. Wrong. Let me display why you are wrong there
too.

Why you write it at all? Usually it is done for fame and/or big bucks.
What is goal-reaching indicator? Popularity and/or commercial success.
Have you faced (however limited) popularity or commercial success? It
is terrible thing. The list of bugs and feature requests may grow to
hundreds or thousands despite how good effort you put up. You will see
it is hard, when you are lucky enough.

Lets say you manage alone? During maintenance your product grows over
100 000 lines of code easily in less than 5 years heroic maintenance.
100 000 lines is about the spot where you start to forget things why
you wrote that or that. Finally you are player yourself with full of
red herrings puzzle. You need to maintain it and hate it at same time.
You can not possibly manage alone.

Now comes last point. Writing interfaces lousily is bad habit. Others
do not like it. It is very hard to find allies. Very precious few can
navigate in 100 000 lines of one-man spaghetti. None of these precious
few lacks better offers or opportunities than to join you. Also it is
very hard for you to get rid of your bad habits (you have worked by
them for 5 years say).

> Could you respond (preferably on
> comp.programming) why it can be considered as such, but motivated,
> that is without responses like "it's good software engineering
> practice", "it's just better", etc... I'm a learner, and I think now
> is the best time to shape out practices and priorities.


Why you cross posted to several groups? Post into every group
individually if you need different opinions. There are lot more
languages. Each is different. For example java does not have language
elements dedicated for const correctness at all i think. However ...
general reasons why principle of minimal privileges is good to follow
are lot older than C++ or java. I think most good developers have
habit to limit access to their modules internals in one way or other.

Lew 07-20-2010 05:28 PM

Re: Oppinion on 'least priviledge', 'const correctness', etc.
 
Öö Tiib wrote:
> Why you cross posted to several groups? Post into every group
> individually if you need different opinions. There are lot more
>


Wrong. You describe multi-posting, one of the cardinal sins of
Usenet. Cross-posting is much better.

Do not multi-post. Ever.

Cross-post only when you must, to the least number of relevant groups.

> languages. Each is different. For example java [sic] does not have language
> elements dedicated for const correctness at all i [sic] think. However ....


Wrong again, sort of. Java has 'final' which is sort of similar to
'const'.

--
Lew

Lew 07-20-2010 05:33 PM

Re: Const correctness (was Re: Oppinion on 'least priviledge', 'constcorrectness', etc.)
 
On Jul 20, 10:18*am, Jorgen Grahn <grahn+n...@snipabacken.se> wrote:
> ["Followup-To:" header set to comp.lang.c++. *Neither the Java nor the
> comp.programming people want to read about const correctness, I'm sure.]
>


Don't be so sure. Java has 'final', which isn't exactly the same as
'const' but is similar and applies similarly to the "principle of
least privilege" and the safety thereof.

Both 'const' and 'final' express the intention to prevent change to a
variable's value.

--
Lew

Öö Tiib 07-20-2010 06:04 PM

Re: Oppinion on 'least priviledge', 'const correctness', etc.
 
On 20 juuli, 20:28, Lew <l...@lewscanon.com> wrote:
> Öö Tiib wrote:
> > Why you cross posted to several groups? Post into every group
> > individually if you need different opinions. There are lot more

>
> Wrong. *You describe multi-posting, one of the cardinal sins of
> Usenet. *Cross-posting is much better.
>
> Do not multi-post. *Ever.
>
> Cross-post only when you must, to the least number of relevant groups.


OK. Thanks for correcting. I do neither anyway unless replying.
comp.lang.c++ and comp.lang.c++.moderated keep me usually entertained
enough.

> > languages. Each is different. For example java [sic] does not have language
> > elements dedicated for const correctness at all i [sic] think. However ....

>
> Wrong again, sort of. *Java has 'final' which is sort of similar to
> 'const'.


I have not seen much usage of it nor heard much talk about 'final-
correctness' in friendly java teams. C devs talk about const a lot
more. Perhaps that 'final' sort of misses some useful perks of
'const'.

Joshua Maurice 07-20-2010 06:05 PM

Re: Const correctness (was Re: Oppinion on 'least priviledge', 'constcorrectness', etc.)
 
On Jul 20, 10:54*am, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com>
wrote:
> I'm a big fan of language constructs that constrain the code in certain
> ways, from data/implementation hiding/encapsulation to things like
> "const", "final", "readonly" (C#), etc. that help convey and,
> especially, enforce intent. *But these kinds of things really need to be
> done in a way that doesn't allow the programmer to just wish the
> restrictions away any time they like. *Otherwise, it's too tempting to
> do just that when the alternative is to spend hours or days updating the
> code to use the restriction properly.


Unfortunately (or fortunately ?), this is C++, and the motto is we'll
give you tools to help you not shoot yourself in the foot, perhaps
even make them the default, but if you're dead set on shooting
yourself in the foot, C++ will allow you to do so.

Jonathan Lee 07-20-2010 06:14 PM

Re: Oppinion on 'least priviledge', 'const correctness', etc.
 
On Jul 20, 2:04*pm, Öö Tiib <oot...@hot.ee> wrote:
> On 20 juuli, 20:28, Lew <l...@lewscanon.com> wrote:
> > Wrong again, sort of. *Java has 'final' which is sort of similar to
> > 'const'.

>
> I have not seen much usage of it nor heard much talk about 'final-
> correctness' in friendly java teams. C devs talk about const a lot
> more. Perhaps that 'final' sort of misses some useful perks of
> 'const'.


I've never heard of an equivalent of "const correctness" in Java,
but I also don't use it very much. Though, a quick Google search
seems to support the idea that "final" is really nothing like
const-correctness:

http://en.wikipedia.org/wiki/Final_%28Java%29
http://stackoverflow.com/questions/1...-specific-to-c
http://mannu.livejournal.com/131085.html
http://en.wikipedia.org/wiki/Const-correctness

--Jonathan

Öö Tiib 07-20-2010 06:38 PM

Re: Const correctness (was Re: Oppinion on 'least priviledge', 'constcorrectness', etc.)
 
On 20 juuli, 20:54, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com> wrote:
> Lew wrote:
>
> I'm a big fan of language constructs that constrain the code in certain
> ways, from data/implementation hiding/encapsulation to things like
> "const", "final", "readonly" (C#), etc. that help convey and,
> especially, enforce intent. *But these kinds of things really need to be
> done in a way that doesn't allow the programmer to just wish the
> restrictions away any time they like. *Otherwise, it's too tempting to
> do just that when the alternative is to spend hours or days updating the
> code to use the restriction properly.


C++ is yes, relatively anarchistic language so teams usually agree
upon policies that they follow and do not expect software (compiler)
to tell to human how to program it. There are always ways to
circumvent the language protection mechanics. If i remember correctly
then calling private member functions in C# is even easier than in C+
+. If something evil gets too annoyingly tempting then build
gallons ... few public executions later it is less tempting.


All times are GMT. The time now is 05:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.