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.
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?
public
. –
Erk public
, how do you know it was intentionally meant as private
when you just forgot to write public
? If you clearly write private
, there is no doubt wether you forgot to write public
or not. Again this is important when someone else is fixing bugs in your code. You may not be around or even not working there anymore. –
Serious 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)).
Default
means little. I dont know the solution, but maybe it should say Default (no keyword emitted)
or something? (Yes I know its more than likely technically the correct term wrt the lang standard) –
Inflectional 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.
© 2022 - 2024 — McMap. All rights reserved.