Re: More than a style issue?
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sun, 12 Aug 2007 22:13:58 +0100
GlennDoten <gdoten@xxxxxxxxx> wrote:
<snip>
Personally, I agree with what you've said, and that's how I happen to
write my code too.
I guess what I'm wondering is why does the language allow defaults like
this? If it didn't, it seems to me that the code would be crystal clear
in all instances.
It would be clear for each individual declaration, but it wouldn't draw
attention to the non-private declarations.
Perhaps it is just this sense of "completeness" a lot
of us have: "well, this field can be internal, protected, public,
internal protected, or private; we better have keyword(s) for each of
those 'just for completeness'."
Possibly. I couldn't say for sure, to be honest.
It's like with curly braces:
if (foo) doSomething();
is the same as:
if (foo) { doSomething(); }
Since a lot of people seem to think that the curly braces should always
be there, for stylistic (i.e., clarity) reasons, then why make them
optional?
I'm with you on that one. The funny thing is that the ECMA spec
contains this (section 8.2.1):
<quote>
The bool type is used to represent Boolean values: values that are
either true or false. The inclusion of bool
makes it easier to write self-documenting code; it also helps eliminate
the all-too-common C++ coding error
in which a developer mistakenly uses =3F==3F when =3F===3F should have been
used. In C#, the example
int i = =3F;
F(i);
if (i = 0) // Bug: the test should be (i == 0)
G();
</quote>
In other words, when highlighting a deficiency in C++ the example given
is just *waiting* for a different kind of bug to be introduced.
I mean, the language requires a break (or some other "exit")
from each and every case in a switch statement because this is much
clearer than what, say, C allows in this regard. And it doesn't allow =
in an if statement the way C does (another good decision, but, again,
some would consider a missing feature). And there's probably other examples.
It *does* allow assignment in "if" statements - the difference is that
it doesn't have an implicit conversion from arbitrary values to a
boolean, and an "if" only accepts a boolean expression. You can still
accidentally do:
bool x = false;
if (x=true)
{
....
}
Maybe the question I'm trying to get to is why did the language
designers take out what some would consider features (case statements
falling through to the next) for nothing more than the sake of clarity
(and I do like this far better than what C allows), yet allow some
optional keywords and optional braces cloud things back up?
I've never seen any justification for this decision. However, at least
the default is a good one - the Java one is awful, for instance.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- Follow-Ups:
- Re: More than a style issue?
- From: GlennDoten
- Re: More than a style issue?
- References:
- More than a style issue?
- From: GlennDoten
- Re: More than a style issue?
- From: Jon Skeet [C# MVP]
- Re: More than a style issue?
- From: GlennDoten
- More than a style issue?
- Prev by Date: Re: Strange, but C# does allow multiple inheritance, so long as it's serial
- Next by Date: Re: Strange, but C# does allow multiple inheritance, so long as it's serial
- Previous by thread: Re: More than a style issue?
- Next by thread: Re: More than a style issue?
- Index(es):
Relevant Pages
|