Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for naming private and static private methods in C#?

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.

like image 285
Mark Rogers Avatar asked Apr 04 '09 17:04

Mark Rogers


People also ask

How do you name a private variable?

We have to start a variable name with a double underscore to represent it as a private variable (not really). Example:- one, two, etc..,. As we already said the variables whose names start with a double underscore are not private.

Can we declare static methods as private?

Yes, we can have private methods or private static methods in an interface in Java 9. We can use these methods to remove the code redundancy. Private methods can be useful or accessible only within that interface only. We can't access or inherit private methods from one interface to another interface or class.

Should static methods be private or public?

They are a utility One of the reasons you should use private static methods is to reduce the complexity of the code. For instance, if there are multiple public methods in your code, there are multiple ways the program can manipulate the objects. Hence, error correction and debugging become extremely difficult.

How do you name a private variable in C#?

Use camel casing ("camelCasing") when naming private or internal fields, and prefix them with _ . When editing C# code that follows these naming conventions in an IDE that supports statement completion, typing _ will show all of the object-scoped members.


4 Answers

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 when necessary. I have no idea if I qualify as a "hardcore pro," but I do get paid. :-)

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

like image 95
tvanfosson Avatar answered Oct 13 '22 00:10

tvanfosson


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() {}
like image 38
bendewey Avatar answered Oct 13 '22 00:10

bendewey


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

like image 28
AnthonyWJones Avatar answered Oct 13 '22 00:10

AnthonyWJones


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

like image 26
Dmitri Nesteruk Avatar answered Oct 12 '22 23:10

Dmitri Nesteruk