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

StackOverflow https://stackoverflow.com/questions/11817588

Question

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.

Was it helpful?

Solution

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?

OTHER TIPS

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"

In a codebase where stuff being public is an information leak (e.g. it will no longer get obfuscated), you want public to stick out. Removing private also has the same 'tide going out' effect on protected and other unnecessarily elevated visibility.

Ideally one'd use a StyleCop rule or similar to make the code actually be consistent (though that, as with all code rules should actually be agreed among the devs before someone comes to a conclusion about it).

(BTW Your contention in the premise re CodeRush's support for omitting it is incorrect - the options allow you to set method visibility etc. to be either private (OOTB) or 'default' (do not specify anything)).

It is up to compiler how to interpret methods or other class members without private, protected or public. It can be changed in nex version. So don't do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top