Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Oppinion on 'least priviledge', 'const correctness', etc.

Reply
Thread Tools

Oppinion on 'least priviledge', 'const correctness', etc.

 
 
Alexander
Guest
Posts: n/a
 
      07-20-2010
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.
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      07-20-2010
["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 .
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      07-20-2010
On 20 juuli, 16:00, Alexander <(E-Mail Removed)> 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.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      07-20-2010
Öö 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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      07-20-2010
On Jul 20, 10:18*am, Jorgen Grahn <(E-Mail Removed)> 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
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      07-20-2010
On 20 juuli, 20:28, Lew <(E-Mail Removed)> 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'.
 
Reply With Quote
 
Joshua Maurice
Guest
Posts: n/a
 
      07-20-2010
On Jul 20, 10:54*am, Peter Duniho <(E-Mail Removed)>
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.
 
Reply With Quote
 
Jonathan Lee
Guest
Posts: n/a
 
      07-20-2010
On Jul 20, 2:04*pm, Öö Tiib <(E-Mail Removed)> wrote:
> On 20 juuli, 20:28, Lew <(E-Mail Removed)> 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
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      07-20-2010
On 20 juuli, 20:54, Peter Duniho <(E-Mail Removed)> 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.
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      07-20-2010
On 7/20/2010 10:33 AM, Lew wrote:
> On Jul 20, 10:18 am, Jorgen Grahn<(E-Mail Removed)> 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.

Almost. const expresses that a specific object should be unchanged, but
final (unfortunately) only refers to primitive/reference immutability.
It is definitely a feature I miss in Java. Especially since immutable
objects are guaranteed to be thread-safe.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Oppinion on 'least priviledge', 'const correctness', etc. Alexander C++ 41 07-25-2010 08:41 PM
Second Oppinion? FingAZ Computer Support 3 05-26-2006 04:28 AM
Oppinion regarding grid layout vs flow layout NWx ASP .Net 4 02-19-2004 08:56 PM



Advertisments