开发者

What is best practice naming for private methods?

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

thisIsBestPracti开发者_运维百科ce

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?


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


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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜