[ACCEPTED]-Should you use the private access modifier if it's redundant?-coding-style

Accepted answer
Score: 70

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

Score: 51

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

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

Also, it's 13 an advantage since lazy people in your team 12 tend to save writing the modifier and making 11 the method private, which is actually a 10 good thing. Otherwise you end up with everything 9 public.

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

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

Score: 27

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

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

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

Score: 24

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

In C#, everything defaults to 14 the least visibility possible. A class member (field, method, property) defaults 13 to private. A class defaults to internal. A 12 nested class defaults to private.

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

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

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

Score: 11

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

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

Score: 7

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

Score: 6

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

Score: 5

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

Score: 4

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

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

Score: 3

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

Score: 3

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

Score: 1

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

Score: 1

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

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

Score: 1

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

If you have to define the convention, I 1 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.

More Related questions