[ACCEPTED]-What is the best practice for naming private and static private methods in C#?-private-methods

Accepted answer
Score: 17

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

They say that all methods should be PascalCase

public void DoWork() {}
private void StillDoWork() {}
private static void ContinueToDoWork() {}
Score: 7

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

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

EDIT 2: Since 10 writing this originally I've changed jobs. The 9 coding standard we follow is different than 8 my personal feelings and we do prefix private 7 fields with underscores. I've also started 6 using ReSharper and our standards (generally) follow 5 the default rules. I find I can easily live 4 with the underscores. I only use this when 3 absolutely required as it does not fit our 2 code standards. Consistency trumps personal 1 preference.

Score: 3

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

Score: 2

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

Score: 1

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

Score: 1

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

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

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

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

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

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

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

More Related questions