Which of the following three options would you choose for a property name in C#, and why?
开发者_JAVA技巧- YearToDateWages
- YTDWages
- YtdWages
I would go with 1. I prefer not to abbreviate anything, unless it's a super-common acronym that would be ridiculous to spell out. Something like "HyperTextTransferProtocolRequest" would be ridiculous to spell out, so it's safe to abbreviate that as "HttpRequest." It's a little subjective, but when in doubt, I tend to not abbreviate.
If you decide to go with 2 or 3, I'd probably vote for 3, based on the recommendations from the "Framework Design Guidelines." It basically says that for acronyms that are 3 or more letters long, you should capitalize the first letter and lower-case the rest. It's a little ambiguous on 2-letter acronyms... Some people prefer to capitalize all letters like "ID" and some prefer to go with "Id". The guideline is to actually capitalize all letters of a 2-letter acronym, but that kind of contradicts with the guideline for 3+ letter acronyms, so people do it both ways.
I would use YearToDateWages
, because without that being in the list I wouldn't know what you were talking about.
See also the general naming guidelines on MSDN:
In general, you should not use abbreviations or acronyms. These make your names less readable. Similarly, it is difficult to know when it is safe to assume that an acronym is widely recognized.
For capitalization rules for abbreviations, see Capitalization Conventions.
Do not use abbreviations or contractions as parts of identifier names.
For example, use OnButtonClick rather than OnBtnClick.
Do not use any acronyms that are not widely accepted, and then only when necessary.
Emphasis in original.
bool ShouldIUseAbbreviate(string abbreviate_)
{
foreach (var peer in myPeers)
{
if (!peer.CanGetTheMeaningWithinOneSecond(abbreviate_))
{
return false;
}
}
return true;
}
I think the first one is best because it is self descriptive.
Microsoft suggested naming convention rule out #2
anything with > 2 acronym letters should be Xxx not XXX
but 2 should be XX
I like less typing so I would go with YtdWages
It depends.
If you are making a library that will see external use, the .NET Framework Design Guidelines say that #1 is preferred.
If it's an internal application/library, then I recommend using the format that is consistent with your teams development standard.
I would opt for the full name rather than one featuring an acronym. It is more descriptive, and while "YTD" may be obvious to some, it might not be to everyone. YearToDate is not excessively long and the meaning is clear.
Is there a reason why you would not use the first one?
It is not only for others; if you have to change something in your own code 2 years later, good, descriptive names will help you.
The .Net framework seems to follow mostly #1. So I would stick with it. Abbreviations should be avoided except where extremely commonly known at the class level. Of course for local(function) variables this is much less strict and I would say that abbreviations and short names are much more appropriate so as to make the code smaller and more concise.
Examples of good abbreviations are XML and HTTP. Who is seriously going to write
string x=myobject.HyperTextMarkupLanguageOutput;
I vote for number 1 as well.
There will be very few times when you do NOT want a descriptive name. Visual Studio will assist you with the long names.
Semi off topic note: If you cant find a suitable name... perhaps the planned usage is not that clear after all ;)
精彩评论