views:

446

answers:

8

I just found out, that it seems a common pattern to user UpperFirstLetterPascalCase() for private methods. I for myself, find this completely inconsistent with naming rules of private instance fields and variables and I find it difficult to read/debug, too.

I would want to ask, why using a first upper letter for methods could be a better choice than a first lower (doThis())? Just out of curiosity...

+17  A: 

All method names in C# start with an upper-case letter by convention. All property names do so too. Only private fields, local variables and parameters to methods start with a lower-case letter (AFAIR).

It's a convention so asking for a »why« is a little misplaced. It's probably akin to asking »Why does the Java coding convention prefer lower-case letters for everything except classes?«—answers to such questions are usually »Because someone, somewhere, once decided that it'd be a good idea to do so«. The rest then merely is history—or convention and you either follow it or you don't (in which case you make the lives of people reading your code harder).

ETA: As said in a comment already, (imho) the answer to the question »why?« usually leads to what the language designers (or the people coming up with the convention) considered to be important aspects to be covered by the convention. In Java it's clearly a case of visually distinguishing classes (PascalCase), variables/fields (camelCase) and properties (get~()/set~()). For .NET there obviously was a need of immediately telling classes and interfaces apart (something I consider pretty nice to have too) and visually distinguishing property (PascalCase) and field (camelCase) access.

Usually in such scenarios all things not initially considered important for the convention fall short in obviousness.

Joey
Yeah, I found out this is the convention. But I wonder what would be reward for following this convention. To my eyes it makes the code more difficult to read - I like being able to tell from the method name, whether it is private or not.
Jan Limpens
@illdev: And I'd love to see from a name in Java whether it's an interface or not. Believe it or not, conventions often have goals they want to achieve and fall short on non-goals. That's simply a matter of what the language designers thought and deemed important.
Joey
@illdev: In what scenario would it be valuable to see that the method is private? If you're looking at the declaration, the private is on the same line. If you're trying to access the method from a different class, then you won't ever see it. If you're accessing the method from a different method in the same class, does it really matter if it's private or not?
Jacob G
@Jacob G - it doesn't matter at the call site, but it could be valuable when you're looking at a list of methods.@Johannes - though this is an excellent answer, I don't think it's misplaced to ask *why* a particular convention developed. There's often some rationale (as there is in this case).
Jeff Sternal
It just lowers the bar for exposing behavior that should not be exposed. Imperative languages are side effect ridden and if I see something is UpperCase in a situation, where I cannot guarantee for the whole object's state, I found a potential bug. Just an example.Anyway, I kind of get the picture, why somebody could want that convention. In the case of Java's interfaces... I am unsure whether the ISomething notation is a good one, too, but that's an entirely different (and probably more interesting) discussion.
Jan Limpens
@Jeff Sternal: does the "why" belong on SO? More appropriately on some C# developer's blog.
sixlettervariables
@six - really? It's programming related and may have a correct answer. It's not like the choice was completely irrational. What's more, the answer may shed some light for developers unfamiliar with .NET (particularly if my hypothesis is correct, that the property syntax - not available in every language - makes it especially important for .NET developers to easily distinguish between member variables and methods.)
Jeff Sternal
@Jeff: Well, the »why« ultimately leads to what I said in my first comment. The people coming up with the convention had a fairly good idea of what it should accomplish (i. e. which things should be obvious if the convention is followed) and the rest usually gets some kind of a »don't care« flag.
Joey
+4  A: 

It isn't really better or worse. It is simply the convention.

ChaosPandion
+2  A: 

Such is the naming convention for .NET, and that is the convention that all standard .NET libraries follow. There's nothing that requires you to follow this convention in your own code, but it's usually best to follow the standards of the language you're working in. Personally, I prefer lowerCamelCase to UpperCamelCase, but I still use the recommended .NET style when writing in C#.

Private member variables are commonly indicated with a leading underscore and lower-case first letter, e.g. _privateMember. Private methods follow the same conventions as public methods.

JSBangs
A: 

The main thing you gain from this convention is the ability to easily distinguish method calls from member variable and parameter access.

As others have mentioned, it isn't necessarily better - it's just the trade-off that .NET developers have chosen and stuck with: making it easier to tell what you're accessing rather than making it easier to tell the accessibility level of the thing you're accessing.

Properties make this especially valuable, which may explain why it took root in the .NET world. Being able to tell the difference between a property access and a member variable access can be critical. Property accessors may be calculating or validating values, and calling the related member variable by mistake (or vice-versa) creates subtle bugs.

Jeff Sternal
A: 

I'd just like to point out that while yes, PascalCase is the convention in .NET for private as well as public methods, considering we're talking about private methods here, I see no harm in choosing what looks better to you. I mean, you do have a point that there seems to be merit in differentiating between private a public methods. As long as what you're exposing to the client (if you're developing a library) is consistent with the convention, you're golden. Just work out with any team members how you're going to do things internally.

Dan Tao
A: 

I do think that you should choose you naming conventions according with your own likes/dislikes, no matter how other people do it (unless of course the guy paying the check says otherwise).

Important stuff is: once you choose a convention, stick with it!

Daniel Dolz
+2  A: 

One difference I could think of is that it distinguishes between local delegate variables and methods, though this is probably not the intention. With modern IDEs, it's generally pretty easy to find out if the call is public or private, in any case. My take on the naming conventions for properties, fields and variables is that they allow distinguishing different forms of the same 'property' like so:

public class MyClass
{
    private int _property;

    public int Property
    {
        get { return _property; }
    }

    public MyClass(int property)
    {
        _property = property;
    }
}

With methods you don't have the same ambiguity where you'd like to use the same name for different forms of the same concept (other than overloads, which don't require different casings to distinguish them).

Dan Bryant
A: 

I tend to look at the entire method signature when I read code. If I don't see an access modifier (public, protected, internal, protected internal) then I can assume it's private (although I try to be explicit about access modifiers).

You are free to adopt your own convention though - particularly for private methods.

There's one case I can think of where the convention is useful: when using C#'s implicit method group conversion syntax. Consider this class, Test:

public class Test {
    public event EventHandler MyEvent;
}

If I create a private method for handling MyEvent and add the handler using C#'s implicit method group conversion syntax a lowercase method name could be confused with a field or variable:

Test test = new Test();
test.MyEvent += myEventHandler;

private void myEventHandler(object sender, EventArgs e) {
    ...
}
dariom