Variable

  • Thread starter Thread starter Dang Tran
  • Start date Start date
D

Dang Tran

Hi,

Usually according to the C# books, before we declare a variable for the
class, we should use either public, private, internal, or protected. What if
we don't use any? For example:
class example
{
int intTestTheScope;
}

Will intTestTheScope be automatically declared as private? Since I am a
beginner at C#, I really appreciate any help. Thank you in advance.
 
Dang Tran said:
Hi,

Usually according to the C# books, before we declare a variable for the
class, we should use either public, private, internal, or protected. What if
we don't use any? For example:
class example
{
int intTestTheScope;
}

Will intTestTheScope be automatically declared as private? Since I am a
beginner at C#, I really appreciate any help. Thank you in advance.

you are right. the default visibility is private
 
Hi Dang,

Correct, the intTestTheScope will automatically be a 'private' variable.
However, it is bad practise not to declare the scope of the variable and I
would strongly suggest that you never just declare a variable without a
scope identifier.

Gary
 
Gary said:
[...]

Correct, the intTestTheScope will automatically be a 'private' variable.
However, it is bad practise not to declare the scope of the variable and I
would strongly suggest that you never just declare a variable without a
scope identifier.

[...]

In what way is it bad practise? C# programmers will know that the
default access level for class members is private, so I disagree with
your statement.

Further, I, personally, think it aids readability to not use the keyword
"private" to declare private class members.
 
I have to agree with Gary. I think it's more readable to explicitly declare
the accessibility of members. If the only cost of making the code clearer is
typing a few extra characters, then it's worthwhile.

While you may assume all C# programmers know the default access, that's not
necessarily a valid assumption, particularly for people new to the language.

Ambiguity leads to error.

Pete

--
http://www.petedavis.net
C# Learner said:
Gary said:
[...]

Correct, the intTestTheScope will automatically be a 'private' variable.
However, it is bad practise not to declare the scope of the variable and I
would strongly suggest that you never just declare a variable without a
scope identifier.

[...]

In what way is it bad practise? C# programmers will know that the
default access level for class members is private, so I disagree with
your statement.

Further, I, personally, think it aids readability to not use the keyword
"private" to declare private class members.
 
At said:
In what way is it bad practise? C# programmers will know that the
default access level for class members is private, so I disagree with
your statement.

Further, I, personally, think it aids readability to not use the
keyword "private" to declare private class members.

I'd say that explicitly stating the visibility is always preferrable over
relying on the implicit privateness of members.
--
Rudy Velthuis

"The longer I live the more I see that I am never wrong about anything,
and that all the pains that I have so humbly taken to verify my notions
have only wasted my time."
-- George Bernard Shaw (1856-1950)
 
Pete said:
I have to agree with Gary. I think it's more readable to explicitly declare
the accessibility of members. If the only cost of making the code clearer is
typing a few extra characters, then it's worthwhile.

I strongly disagree with you and Gary then. Here's an example of where
I think the readability is largely improved by not including the
relevant accessibility specifiers:

class EasyToRead
{
int n;
bool b;

public void Method()
{
DoSomething();
}

void DoSomething()
{
SomethingElse.DoSomething();
}
}

internal class NotSoEasyToRead
{
private int n;
private bool b;

public void Method()
{
DoSomething();
}

private void DoSomething()
{
SomethingElse.DoSomething();
}
}
While you may assume all C# programmers know the default access, that's not
necessarily a valid assumption, particularly for people new to the language.

If a newbie comes across code where a member of a class is declared with
no accessibility specifier, he/she should, as the OP has done, find out
what the default accessibility is.

Newbies' ignorance isn't the problem of the programmer who wants to
write clear code.
Ambiguity leads to error.

This "ambiguity" is non-existant for those who've taken the time to
learn the language.
 
Rudy said:
At 20:42:32, 14.03.2004, C# Learner wrote:




I'd say that explicitly stating the visibility is always preferrable over
relying on the implicit privateness of members.

I don't see this as a problem, personally, since I don't expect this
language behaviour to change.
 
At said:
I don't see this as a problem, personally, since I don't expect this
language behaviour to change.

No, but it is always better to avoid ambiguity. It makes code less
readable, IMO.
 
You are entitled to your opinion but if you ask this question to any
experienced OO developer then I can guarantee that 99% of them will agree
that you should always clearly specify the accessibility scope of any
members.

Gary
 
Hi C# Learner:
I'm too a fresh learner of C#. At first, I appreciate your all help for my
question. Finally, I'd like to contribute my thought to the on-going fight.
C# or any kind of language, in my mind, is simply a tool which should be
used to clearly state functions of the project. Hence, I believe that it
should not confuse reader by any assumption. I previosly asked the question
about the scope of a variable whose scope is not clearly define. I was
confused and had to guess its scope. I think the guessing is the most
dangerous thing in software development. If we misunderstand the
functionality of any variable, function, we'll face a huge problem later on
in Testing phase.
I hope that my opinion is not incorrect. However, C# learner, you have your
point. As long as we feel comfortable about our coding style, we are doing
fine, aren't we?
:-)
 
Gary Milton said:
Correct, the intTestTheScope will automatically be a 'private' variable.
However, it is bad practise not to declare the scope of the variable and I
would strongly suggest that you never just declare a variable without a
scope identifier.

I disagree strongly. One of the nice things about C# is that the
default access is *always* the most restrictive one. I therefore
*always* leave it off if I can - the fact that something is *less*
restrictive than it can be is something that should be noted, and it
makes it easier to see that if there's an explicit modifier there.
 
Pete Davis said:
I have to agree with Gary. I think it's more readable to explicitly declare
the accessibility of members. If the only cost of making the code clearer is
typing a few extra characters, then it's worthwhile.

If it made the code more readable, I'd agree. I believe it makes the
code *less* readable, because it means there's less of a highlight when
something *isn't* private.
While you may assume all C# programmers know the default access, that's not
necessarily a valid assumption, particularly for people new to the language.

I would ask anyone reading my code to become familiar with C# then.
Ambiguity leads to error.

It's not ambiguous - it's clearly defined by the spec.
 
Gary Milton said:
You are entitled to your opinion but if you ask this question to any
experienced OO developer then I can guarantee that 99% of them will agree
that you should always clearly specify the accessibility scope of any
members.

How on earth can you make such a guarantee? Have you seen studies
(regarding C# in particular, where the defaults are well chosen and
easily remembered) which show this? Or is it just that you feel
strongly about it, and so think that all experienced developers will
feel the same way?
 
Rudy Velthuis said:
No, but it is always better to avoid ambiguity. It makes code less
readable, IMO.

Whereas IMO, it makes it more readable. It makes it easier to instantly
see which members *aren't* private, because they're the ones with
access modifiers.
 
Dang Tran said:
Hi C# Learner:
I'm too a fresh learner of C#. At first, I appreciate your all help for my
question. Finally, I'd like to contribute my thought to the on-going fight.
C# or any kind of language, in my mind, is simply a tool which should be
used to clearly state functions of the project. Hence, I believe that it
should not confuse reader by any assumption. I previosly asked the question
about the scope of a variable whose scope is not clearly define. I was
confused and had to guess its scope.

No, you *didn't* have to guess its scope. You should never *guess* what
the language will do, and you don't have to. That's what the language
specification is for.
I think the guessing is the most
dangerous thing in software development.
Agreed.

If we misunderstand the functionality of any variable, function, we'll face
a huge problem later on in Testing phase.

Again, agreed. That's why when you see something that you're unsure of,
you should consult the language specification. You can then decide
whether to adopt the omission of the access modifier as part of your
coding style or not.

There are some times where redundant specification is good - extra
brackets (and spaces!) can do wonders for readability in complicated
lines of code, for instance.

In this case, however, I feel *every* respectable C# developer should
know the default access, especially as it's a single easy-to-remember
rule: the default access is always the most restrictive one you can
specify in that situation.

Given that, I believe it makes code clearer to omit the default access
specifer as it then makes it more obvious when you've got a member
which deviates from that most restrictive access. Making things
accidentally *more* restrictive than you intend pretty much always has
far less worrying consequences than making something accidentally
*less* restrictive than you intend.
I hope that my opinion is not incorrect. However, C# learner, you have your
point. As long as we feel comfortable about our coding style, we are doing
fine, aren't we?

On that, I disagree. People may feel comfortable with non-thread-safe
code, and never find out it's problematic until they have a customer
with a multi-processor machine, etc.

There are things where there are valid differing opinions - and the
issue of specifying private access is one of them, I feel. There are
other areas where code readability more obviously suffers (usually due
to laziness) and there it's worth getting into good habits.
 
Hi C# Learner:

I'm Rudy.
--
Rudy Velthuis

"Good people do not need laws to tell them to act responsibly, while bad
people will find a way around the laws."
- Plato (427-347 B.C.)
 
At said:
In this case, however, I feel every respectable C# developer should
know the default access

True.

But it doesn't hurt to explicitly specify the access modifier, even if it
is implicit. I always prefer explicitness over implicit rules.
 
Back
Top