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

- 55,340
- 13
- 112
- 144
-
1Except that private is the default (except for non-nested classes). So the best thing the designer can do, to make the code more secure and easier to understand, is keep the public surface as small as possible. Making something private doesn't require a deliberate decision. Making something non-private does. – Ryan Lundy Mar 18 '14 at 21:41
-
1@Kyralessa: I mentioned it's the default, and my argument is that, at least in principle, _everything_ that you do should be _deliberate_, not just marking something `public`. – Jordão Mar 18 '14 at 22:05
-
You're assuming that omitting the modifier could only be accidental. – Ryan Lundy Mar 19 '14 at 13:13
-
1No, I'm assuming that a third party (or even you six months from now) wouldn't necessarily know whether it was accidental or not. – Jordão Mar 19 '14 at 16:43
-
And yet, it doesn't matter if it *was* accidental, because omitting it just made it as constrained as it could be. Nothing bad comes of that. – Ryan Lundy May 13 '14 at 03:51
-
1@Kyralessa: yes you're right, nothing bad comes out of that... – Jordão May 14 '14 at 09:48
-
@Kyralessa, But if you omit `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. – Max Kielland May 19 '17 at 12:38
-
@MaxKielland: Simple: If you forgot to write public, and your code works, then the method doesn't need to be public. Perhaps it doesn't need to exist at all! (If you wrote the method and didn't bother to test it, you have bigger problems than whether or not to write a default keyword.) – Ryan Lundy May 19 '17 at 12:56
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"

- 214,206
- 104
- 411
- 497
-
3More _readable_ code _is_ _cleaner_. Cleaner is not (necessarily) shorter! – Jordão Aug 08 '12 at 13:40
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)).

- 59,778
- 26
- 187
- 249
-
Ruben - can you point me to where I can make "private" not appear in DevExpress options for CodeRush? – Neal Aug 06 '12 at 13:27
-
DevExpress\Options - Editor\Code Style\Scope - Methods: Select "Default" – Rory Becker Aug 06 '12 at 22:14
-
@RoryBecker thanks for stepping in. While we're here, I only figured out it existed because I was bloody minded and was on my 3rd lap of every options menu in there. The problem is that `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) – Ruben Bartelink Aug 06 '12 at 23:17
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.

- 11,946
- 2
- 24
- 38
-
4
-
1Actually, in VB6 procedures are public by default. That changed in VB.Net. Yep, breaking change. Anyone who didn't declare their method scope had a lot of work to do when migrating VB6 code to VB.Net. – DOK Feb 19 '13 at 21:06