views:

612

answers:

13

Given that these two examples are equivalent, which do you think is preferrable?

Without explicit modifier

public class MyClass
{

    string name = "james";

    public string Name {
        get { return name; }
        set { name = value; }
    }

    void SomeMethod() { ... }

}

With explicit modifier

public class MyClass
{

    private string name = "james";

    public string Name {
        get { return name; }
        set { name = value; }
    }

    private void SomeMethod() { ... }

}


I've always used the latter, but recently I've started adopting the former style. The private is redundant as that's the default accessor modifier, so doesn't it make sense to exclude it?

+24  A: 

I think explicity stating private helps in readability. It won't allow for a programmer to interpret its visibility differently.

Nicholas Mancuso
It's pretty clear in the C# spec that the default modifier for classes is private. It's also a commonly asked question in interviews.
jonnii
So true.. the compiler will remove it anyway. And those few letters don't cost a thing!
Tigraine
They cost me time :D
jonnii
It's extremely helpful if you have developers on the team that are learning C# after years of programming with Java. Even though I know better, my instinct is to default to Java defaults.
James Schek
how long does it take to type pri and hit space in VS? I mean .. we're talking milliseconds here ;) .. Readability > all
Tigraine
@jonnii. ya the spec states that, but as James said, in java the default is protected, so newer programmers coming from java may not know. or just newer programmers in general for that matter.
Nicholas Mancuso
Never mind newer programmers -- explicit is almost always better. Don't make me think. :) +1 to this answer.
John Rudy
The default access modifier in Java is 'default', not 'protected'. Although 'default' may be similar to protected or private, I'm pretty sure there is a subtle difference.
Dalin Seivewright
Also, after a quick google search, it appears that the default access modifier for C# is 'internal' and not 'private'.
Dalin Seivewright
@Dalin: It's private for members of types (including nested types) and internal for top-level types.
Jon Skeet
StyleCop enforces you to always explicitly state the visibility. Just getting in the habit has helped prevent me from leaving off the visibility on public members by accident.
Will Eddins
The default access modifier in Java is actually "package-private". The member appears as public to other classes in the same package, but as private to other packages and to subclasses. More details here: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
Ricket
+1  A: 

I always prefer to be explicit, even if it is redundant. This provides built-in code comments and can be helpful for the next guy, especially if he's a noob. :-)

Noah Goodrich
A: 

you are correct but since you want your code to be understandable for everyone i think you should include, you never know when if someone does not know this

Oscar Cabrero
+3  A: 

Personally, I prefer the private modifier - I like explicitness. For fields, it also highlights that it's a member variable as opposed to a function variable (the only difference otherwise is location - which is okay if folks can indent properly, but can be confusing otherwise).

Mark Brackett
A: 

Always use the explicit form. If for whatever reason the underlying assumption changes, the code with an explicit denotation of access won't break, whereas the implicit connotation my easily break.

Also, when you are talking about different types of structures, they may have different default accessibilities. Without the explicit modifiers, the ownus is on the reader to know which structure has what default. E.g. in C#, struct fields default to public, class fields default to private, and class definitions default to internal.

Marcus Griep
A: 

My understanding has always been members have "internal" accessibility unless stated otherwise. If that's true, the "private" modifier would be required to ensure those members are in fact private.

Regardless of whether I'm correct about the above or not, leaving the modifier in place will increase the readability of the code in case another developer is later modifying this class and is curious about the accessibility. Hope this helps!

Adam Alexander
Members are "private" unless stated otherwise. *Types* (or at least, outer-types) are internal by default; and different again for nested classes. See - there are already 3 scenarios to remember - worth making it explicit ;-p
Marc Gravell
You don't have to remember three scenarios. All you have to remember is that by default everything has the least visibility it can possibly have. An outer type can't be private; if it were, nothing could access it. An inner type *can* be private, since it can be accessed by its outer type, and that's why private is its default visibility. The default visibilities in C# are perfectly logical - and thus it's *not* worth making it explicit.
Kyralessa
Members have internal (`Friend`) visibility by default in VB .NET. The VB .NET rules basically require you to make visibility explicit, because there's no apparent logic to them. The C# rules have one simple logical rule (see previous comment), and it's safe to leave the defaults unless you need to change them.
Kyralessa
A: 

I go for explicit all the time. If nothing else it demonstrates your intention more clearly. If I want something to be private I will say so. Explicitly typing the modifier makes sure I think about it, rather than just leaving things private because its quicker. That an a long list of members line up better :)

Oliver Hallam
+7  A: 

Marking it as private makes it clear that it is deliberate, rather than "I didn't actually think about it, so I don't know if it would be better as something else."; so I do like making it explicit. I wouldn't get religious about it, though.

Also - this prevents having to remember rules... members are private by default, (outer) types are internal by default; nested types are private by default...

Make it clear... make it explicit ;-p

Marc Gravell
But the rules are easy to remember. You could restate them as "Everything is as little visible as possible". Or "Everything is private unless it can't be" (outer types can't be because then nothing could use them). There's nothing hard about remembering the defaults.
Kyralessa
+4  A: 

I always omit it to reduce visual clutter. In C#, everything defaults to the least visibility possible. A class member (field, method, property) defaults to private. A class defaults to internal. A nested class defaults to private.

If you need something to be more visible, then add the modifier. This makes it easy to see items that deviate from the default visibility.

The only case I can see it causing problems is if you come from a VB .NET background. But hey, I come from a VB .NET background, and I still know what the default visibilities are in C#.

In VB .NET, on the other hand, I pretty much always include it because the defaults are rather stupid; the default for a Sub or Function is Friend rather than Private.

Kyralessa
+1  A: 

I always specify the visibility explicitly. I prefer not letting the compiler guess my intentions.

tvanfosson
+3  A: 

I've been developing full-time in C# for about 7 years now, and until I read this topic I didn't know what the default access modifier is. I knew that one existed, but I've never, ever used it.

I like explicitly declaring my intent as I code. Both because the declarations are there for me to see when I go back and look at it, and because actually thinking and typing the word "private" when I write a method makes me think just a little more about what I have it in mind to do.

Robert Rossney
+1  A: 

I like to be super-explicit usually. I will go for specifying "private" always. However there is another reason : Programmers coming from programming languages where the default visibility is NOT private but public, for example PHP.

Andrei Rinea
+4  A: 

It looks that we are the only one, but personally, I support the let's remove private campaign

My concern is that public and private are so similar, 6-7 chars length, blue, starting with 'p', so it's much harder to point a public method between 10 explicit private ones than between 10 that have no access attribute.

Also, its an advantage since lazy people in your team tend to save writing the modifier and making the method private, witch is actually a good thing. Otherwise you end up with everithing public.

I usually preffer explicit over implicit, but that's more important in language corner cases (tricky cheats) thant in a widespread feature. Here I think long-rung mantenability it's more important.

Also, I usually like when code it's simple and clear in a mathematical way over when the code is explicit in order to preserve future coder's ignorance. That's the VB way not C#...

Olmo
We should start a support group!
jonnii
There's something wrong with your eyes if you glance at the two words and see them as the same. Attention to detail is key in programming.
ck
have your editor colour them differently if you don't like them both blue
jk
Not all editors can color different modifiers different colors. Some add-ins (like CodeRush) can display different symbols for them, though.
Kyralessa