Jon said:
GlennDoten said:
Looking at this posted code made me think of the issue of optional
keywords and their usage (or lake of usage). [UL-Tomten, I don't mean to
pick on your code; it just made me think to ask the following question.]
I always wondered why people write
class Foo
yet write
private int i;
instead of
int i;
Or, more generally, why use (or not use) optional keywords at all?
A random early Sunday morning question. I suppose it is simply a matter
of style. Any deeper reasons than that?
I miss them out when they're optional, for the sake of clarity. It
doesn't make *those* declarations clearer, but it makes *others*
clearer. The defaults in C# always go for the most private possible
option (with one exception when you are making one part of a property
more private than the property itself). Now, if I've got code like
this:
int Method1() {...}
int Method2() {...}
int Method3() {...}
public int Method4() {...}
then it calls attention to the "public" - it makes me think, "Did I
really want to make this public?"
That's my reasoning.
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. 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'."
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 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.
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 know that coding standards meetings would go a lot smoother if the
language did not allow multiple ways to express these things. (But then
the meetings would be a lot more boring too!)