Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

As "private" is the default scope in C# - should the word "private" be removed from signatures for cleaner code? [closed]

I've heard various programmers suggest not including the word "private" in declarations, method signatures, etc. as private is the default scope when not specified. It can make for cleaner code but I'm interested in what the opinions are on whether you use the "private" scope on your variables, methods, etc. Tools like CodeRush that generate code for you include the word "private" so I'm curious if this is good or bad or just a matter of personal preference.

like image 492
Neal Avatar asked Aug 05 '12 15:08

Neal


People also ask

What is the default scope?

The default scope is package-private. All classes in the same package can access the method/field/class. Package-private is stricter than protected and public scopes, but more permissive than private scope.

Is private default in C#?

The default access for a class member in C# is private. Member variables i.e. class members are the attributes of an object (from design perspective) and they are kept private to implement encapsulation. These variables can only be accessed using the public member functions.

What is the default scope of a class in C#?

In other words the default scope of a C# class is internal.

Are class members public by default C#?

Class members, including nested classes, can be public, protected internal, protected, internal, private, or private protected. Members are private by default.


2 Answers

Cleaner code is more explicit as to the designer's intentions. Using private demonstrates a deliberate choice, not open to debate. Falling to the default opens up the questions: was this on purpose, or he simply forgot to include a modifier?

like image 115
Jordão Avatar answered Oct 03 '22 04:10

Jordão


Remove the private and Ask your fellow developers whether they are confused or not

Personally i feel, including private make your code more readable. I would give more importance to "Readability" than "being cleaner"

like image 31
Shyju Avatar answered Oct 03 '22 04:10

Shyju