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

4

10

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.

Whitley answered 5/8, 2012 at 15:19 Comment(0)
A
19

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?

Audiometer answered 5/8, 2012 at 15:21 Comment(8)
Except 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.Yerxa
@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.Erk
You're assuming that omitting the modifier could only be accidental.Yerxa
No, I'm assuming that a third party (or even you six months from now) wouldn't necessarily know whether it was accidental or not.Erk
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.Yerxa
@Kyralessa: yes you're right, nothing bad comes out of that...Erk
@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.Serious
@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.)Yerxa
B
9

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"

Butler answered 5/8, 2012 at 15:23 Comment(1)
More readable code is cleaner. Cleaner is not (necessarily) shorter!Erk
I
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)).

Inflectional answered 5/8, 2012 at 20:13 Comment(3)
Ruben - can you point me to where I can make "private" not appear in DevExpress options for CodeRush?Whitley
DevExpress\Options - Editor\Code Style\Scope - Methods: Select "Default"Peep
@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)Inflectional
C
0

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.

Cline answered 5/8, 2012 at 15:25 Comment(2)
That would be a breaking change...Erk
Actually, 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.Bailiff

© 2022 - 2024 — McMap. All rights reserved.