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.
As "private" is the default scope in C# - should the word "private" be removed from signatures for cleaner code?
4.6k Views Asked by Neal At
4
There are 4 best solutions below
3

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)).
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?