JL.
JL.

Reputation: 81262

What is best practice naming for private methods?

I was taught that private methods should begin with lowercase for example:

thisIsBestPractice

I've installed some code style checkers, and they're suggesting private methods should begin with upper case.

What is the current preferred industry standard?

Upvotes: 3

Views: 3316

Answers (5)

Jamie Keeling
Jamie Keeling

Reputation: 9966

I've always known method names to be uppercase regardless of them being public or private. I use The Elements of C# style frequently, they suggest using the PASCAL naming conventions such as keeping things as uppercase.

public void AnExample()
{
}

http://www.amazon.co.uk/Elements-C-Style-Kenneth-Baldwin/dp/0521671590

It's a good read, you can definitely find it cheaper though.

At the end of the day it all comes down to coding standards, whether they be decided by yourself or the organisation you work for.

Upvotes: 0

James
James

Reputation: 6499

It goes back and forth between camel casing and pascal casing. Right now I believe Microsoft recommends pascal casing for all methods, regardless of access.

In practice though, I've seen all sorts of styles (_privateMethod, privateMethod, PrivateMethod, Private_Method, etc) as mentioned, it's what works for you as long as it's consistent. Pascal casing works great if you're using Visual Studio and have the nice method drop down that categorizes them by color. If you're using something else, you might want to consider prefixing with an underscore or something.

Upvotes: 0

Russ Cam
Russ Cam

Reputation: 125488

The current industry standard is to define a standard and stick to it! What you decide your standard should be is not so important, but what is important is that you all agree to it and use it. This way, everyone knows that a camelCased method is private, although the private modifier would be a giveaway :)

I personally stick to the default of FxCop - FxCop gets run on every build and will output to the console any code not conforming to the standard

Upvotes: 12

Chathuranga Chandrasekara
Chathuranga Chandrasekara

Reputation: 20906

Generally we can find many definitions on Coding standards by a simple search. You can easily use a one of them as your guidelines.

But honestly it is a convension not a strict standard. So you may have a freedom to use whatever notation. But you should not deviate a lot from current standards because it can make some hidden or unnecessary problems (Ex : In future changes/While refactoring). But as far as you stick to a single standard that would be ok.

And the most important thing, you MUST adhere to your company\organization standards. Everyone is supposed to follow such standards, if not you are in trouble.

However this is a very good article I found on the web.

Coding Standards

Upvotes: 0

jball
jball

Reputation: 25014

There are many different standards, and examples can be found all over the internet. Consistency is what matters most, though.

Upvotes: 4

Related Questions