views:

793

answers:

6

I'm trying to figure out what is the smartest way to name private methods and private static methods in C#.

Background: I know that the best practice for private members is underscore-prefix + camelcase. You could argue this with me, but trust me I've seen enough code from hardcore pros that follow this convention, it is the skilled industry standard.

I also know that pascal case is the industry standard for public methods. But I have seen a combination of test style naming (ie. method_must_return_false_occasionally ) to pascal case, camelcase, and underscore-prefix + camelcase, for private and private static methods.

But what is the best practice style for private and private static method naming in C#?

If there are certain styles that are used from some private methods and not others, I can understand that, just explain.

Thanks for reading.

+9  A: 

Check out the Microsoft's Naming Guidelines and Brad Abram's Style Guide

They say that all methods should be PascalCase

public void DoWork() {}
private void StillDoWork() {}
private static void ContinueToDoWork() {}
bendewey
Right, but I wonder because as we all we know microsoft can be extremely intelligent or extremely boneheaded. It seems like it makes more sense to have a different convention for private methods, like there is a different convention for private members. Also, clearly some of fxcops rules are silly
Mark Rogers
I agree that some of the FxCop's rules seem silly, but I am a strong believer in moving our community closer to standards. I feel MS has the biggest voice in this area. There are numerous tools (intellisense, etc) and compiler messages to determine an items scope, we don't need different names.
bendewey
I agree that there are various tools that make it easy, but I respectfully disagree that using different naming can be a powerful tool when combined with intellisense. For instance, I can quickly access private members with the underscore character.
Mark Rogers
disagree that -> disagree,
Mark Rogers
+2  A: 

I don't know about industry standards but use Pascal casing even for private methods and I make no distinction for static methods.

AnthonyWJones
+4  A: 

The naming guidelines for .NET class library development don't distinguish between public and private usage and recommend Pascal case for static methods.

EDIT: My personal practice is to use Pascal casing for methods and properties, camel casing for fields, parameters, and local variables. When referencing instance members from within the class I use this. to distinguish from class members and parameters. I have no idea if I qualify as a "hardcore pro," but I do get paid. :-)

tvanfosson
anyone with ~35K rep is a hardcore pro in my book.
bendewey
I appreciate the sentiment, but I feel like I still have a lot to learn.
tvanfosson
A: 

One choice is to use a tool that enforces you to be consistent, such as style cop (if you use reSharper there is a plugin for style cop on codeplex). Most of the tools seem to enforce(? suggest) the Microsoft guidelines as these get you through some tests for getting MS platform approval of your code.

Michael
+1  A: 

Each place that I have worked has always done it differently.

I find that as a professional developer, part of the professional is about fitting into new teams, that may have different coding conventions.

Being able to switch your style and cope with the cognitive dissonance that this creates in the first few weeks, is part of the profession.

I would start to look at the variety of open source and the like projects and you will see a wide variety of schemes.

I have also seen the underscore camelCase and Pascal case debates split communites and sometimes teams - which is I guess the point of a coding scheme.

So unless the project is you as sole developer in which case you are free - try find out what the rest of the team like to use and what makes it easier for the team to understand.

The other thing I would factor in is the complexity of the code in OO terms if this a simple project or a complex OO design with mutliple patterns or you are using some IOC then start to run a "spike" on different types of coding standard and then look at what the code physically looks like when you are using it - look nice to you and the team or does it look ugly.

+1  A: 

The weird_underscore_naming convention is typically restricted to tests because it makes them more readable, and is something that is heavily encouraged by BDD. Remember that, whereas a method describes in a short way what it does (DepositMoney), tests need to describe what it is they are doing, i.e., Negative_deposits_must_be_caught

Dmitri Nesteruk