53

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?

jonnii
  • 28,019
  • 8
  • 80
  • 108

14 Answers14

67

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

Nicholas Mancuso
  • 11,599
  • 6
  • 45
  • 47
  • 17
    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 Oct 31 '08 at 20:55
  • So true.. the compiler will remove it anyway. And those few letters don't cost a thing! – Tigraine Oct 31 '08 at 20:55
  • 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 Oct 31 '08 at 20:57
  • how long does it take to type pri and hit space in VS? I mean .. we're talking milliseconds here ;) .. Readability > all – Tigraine Oct 31 '08 at 20:59
  • @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 Oct 31 '08 at 21:07
  • 19
    Never mind newer programmers -- explicit is almost always better. Don't make me think. :) +1 to this answer. – John Rudy Oct 31 '08 at 21:10
  • 1
    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. –  Oct 31 '08 at 21:22
  • 1
    Also, after a quick google search, it appears that the default access modifier for C# is 'internal' and not 'private'. –  Oct 31 '08 at 21:24
  • 4
    @Dalin: It's private for members of types (including nested types) and internal for top-level types. – Jon Skeet Oct 31 '08 at 22:32
  • 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 Jan 12 '10 at 15:49
  • 1
    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 Jan 29 '10 at 17:33
  • 3
    @Will Eddins, the very fact that you could leave off the visibility "by accident" shows that the members didn't need to be public. If they did, you would have already used them in a context that required them to be public. This is why it's best to omit the visibility, so that things aren't visible until they *need* to be. – Ryan Lundy Mar 30 '11 at 15:33
  • This is an old thread, but still a discussed matter. Well in case someone noticed this, I personally don't take sides about this subject. In the end, you're coding for a team/company. And each team is different. You gotta discuss these things with the team, not the community – Cool guy Dec 25 '22 at 10:49
56

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, it's an advantage since lazy people in your team tend to save writing the modifier and making the method private, which is actually a good thing. Otherwise you end up with everything public.

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

Also, I usually like when code is 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#...

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
Olmo
  • 4,257
  • 3
  • 31
  • 35
  • 6
    We should start a support group! – jonnii Jan 13 '10 at 23:04
  • 3
    have your editor colour them differently if you don't like them both blue – jk. Jan 14 '10 at 14:22
  • Not all editors can color different modifiers different colors. Some add-ins (like CodeRush) can display different symbols for them, though. – Ryan Lundy May 06 '10 at 21:33
  • 3
    I'm in the same camp, although I don't mind if private is used. My primary reason is that C# code lines are usually very long (the leading indent space, long type names, etc.). Removing the private makes it shorter. And a block of private members kinda looks as terse as the traditional C program. – Dudu Jan 08 '11 at 08:35
  • 6
    Shorter is more elegant. Folk who need explicit clutter like 'private', extra brackets and unnecessary operators should learn the language. – James Jul 07 '14 at 14:17
  • 1
    Agreed. For me 'private' modifiers increase 'noise-to-signal' ratio when browsing the code. – Eternal21 Sep 17 '15 at 13:56
  • 1
    +1 I always like to write as little as possible so anything that is not necessary I will omit. I think it improves readability by making it less cluttered and allowing more code to show on the screen. – DonO Dec 15 '16 at 19:15
  • 4
    True, because they are access *modifier*. Why add a modifier when it is not actually *modified*? – Franklin Yu Sep 08 '17 at 15:01
27

I always omit it for two reasons: to reduce visual clutter, and to do the right thing by default.

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.

Thus if you omit your visibility except where you need it, you'll be automatically using the least visibility possible, which is the right way to do things anyway.

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.

(Unfortunately, this rule only holds for C#. In VB .NET and in F#, the defaults are quite different and definitely not "least visibility possible" in most cases.)

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
  • While I do think the idea of being as explicit as possible is good, in this case, it's either public or private so having public in front of declarations IS being explicit. This is technically not right because there are different modifiers but for all intents and purposes it is. It's really a fault of the language not to enforce either style. – One Man Monkey Squad Nov 03 '22 at 09:46
25

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 14
    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. – Ryan Lundy Mar 31 '10 at 20:16
  • 1
    If it being private by default, because you left off the modifier and didn't spend time thinking about it, has no negative impact on behavior, would actually spending time thinking about it have yielded a different result? Also, one already has to remember the difference between public and private (and any other modifiers in-between). Remembering that everything defaults to least visible/accessible seems like it's part of understanding what those modifiers mean. – iheanyi Sep 26 '22 at 08:00
10

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
  • 94,622
  • 24
  • 146
  • 218
  • 20
    "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." --> That takes some guts to admit :) – Zaid Masud Feb 11 '14 at 16:03
6

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
  • 84,552
  • 17
  • 108
  • 152
5

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
  • 24,875
  • 14
  • 66
  • 96
  • I like this argument. Let me use parenthesis galore around all my math notations because the next guy might not know math (even tho he's a programmer). People treat this like declaration visibility is like the holy grail of programming knowledge. Also, make sure to never use var, the noob might not know what it is, better write the type 20 times. One could even argue that "the noob" might understand the code better if there's no "private" keyword to confuse him. – One Man Monkey Squad Nov 03 '22 at 10:29
5

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 Rînea
  • 20,288
  • 17
  • 117
  • 166
  • Yeah, end up, they(PHP programmers) will create a bunch of private members which what they really want is public modifier and the implemented class is not tested from outside class. – Norlihazmey Ghazali Feb 06 '17 at 09:25
  • Do you also always seal your classes I hope? :) – AgentFire Feb 08 '22 at 16:09
  • But is that really an actual problem? Because they'll notice quickly they can't call the members and everything except the required bits being private is good so there's no harm. Truly switching to a different language and it's common style is so much more than learning the default visibility... – One Man Monkey Squad Nov 03 '22 at 09:49
3

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
  • 8,156
  • 1
  • 23
  • 24
  • Changing this rule after 22 years will not happen. The default is least visibility, really not much to remember or think about. It's visual clutter and apparently we need what-ifs and lets-assume-the-rules-are-complicated to justify it. – One Man Monkey Squad Nov 03 '22 at 10:25
3

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
  • 4,242
  • 1
  • 24
  • 30
2

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

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • "guess" really is the wrong word here. The rule is least visibility by default except otherwise specified. It's like using your indicator lights on every corner, even when not required and nobody else is there. – One Man Monkey Squad Nov 03 '22 at 10:18
1

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
  • 4,168
  • 8
  • 29
  • 49
1

First I will ask if there is a previous code convention/standard about that being used by the team. If there is any, you should follow it.

If you have to define the convention, I will push for the explicit way.

My reasons?;

  • I like to keep the same structure (I mean a.-access modifier, b.-return-type/type, c.-name).
  • I don't want (and I don't expect others) to remember all the modifiers by default (which are here).
  • Not all the languages have the same rules.
  • And somehow I think that, somehow, forces the developer to think and be more conscious about the scope of the variable/method and the exposition of those. If you have some experience this will probably not apply, but if you are a newbie, or you work with people with less experience, I think that is useful.
mayo
  • 3,845
  • 1
  • 32
  • 42
0

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
  • 15,132
  • 5
  • 42
  • 41
  • 3
    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 Oct 31 '08 at 21:05
  • 2
    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. – Ryan Lundy Mar 31 '10 at 20:12
  • 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. – Ryan Lundy Apr 12 '10 at 01:24