问题
I have a property "EntityID" in a class. Resharper (5.1) says
Name 'EntityID' does not match rule 'Methods, properties and events'. Suggested name is 'EntityId'.
But IMHO according to the naming conventions in the Design Guidelines for Class Library Developers. 'EntityID' should be perfectly fine:
- Do not use acronyms that are not generally accepted in the computing field.
- Where appropriate, use well-known acronyms to replace lengthy phrase names. For example, use UI for User Interface and OLAP for On-line Analytical Processing.
- When using acronyms, use Pascal case or camel case for acronyms more than two characters long. For example, use HtmlButton or htmlButton. However, you should capitalize acronyms that consist of only two characters, such as System.IO instead of System.Io.
- Do not use abbreviations in identifiers or parameter names. If you must use abbreviations, use camel case for abbreviations that consist of more than two characters, even if this contradicts the standard abbreviation of the word
*Update: * The latest version of the guidelines also say:
Do capitalize both characters of two-character acronyms, except the first word of a camel-cased identifier. A property named DBRate is an example of a short acronym (DB) used as the first word of a Pascal-cased identifier. A parameter named ioChannel is an example of a short acronym (IO) used as the first word of a camel-cased identifier.
Am I understanding the guidelines correctly? If so, how can I make ReSharper accept "EntityID" (well known two letter acronym) but reject "HTMLReader" (it should be HtmlReader).
回答1:
Do you want to stick to the recommendations of the Framework Design Guidelines, or do you want to use uppercase ID
regardless?
The latest version of the guidelines has this to say:
The two abbreviations that can be used in identifiers are
ID
andOK
. In Pascal-cased identifiers they should appear asId
, andOk
. If used as the first word in a camel-cased identifier, they should appear asid
andok
, respectively.
So it seems that ReSharper is correct in the case of Id
.
Out of interest, ReSharper also recommends that two character acronyms follow the same casing rules. This is at odds with the guidelines: "Do capitalize both characters of two-character acronyms, except the first word of a camel-cased identifier."
回答2:
If you wish ReSharper to have "ID" as a valid abbreviation for "Identity" then simply select the Property, press "Alt-Enter" and then select "Add 'ID' to the abbreviations list" from the ReSharper context-menu.
回答3:
The simplest solution would be to go into ReSharper Options, under the "C# Naming Style" tab, make sure "Override common settings" is selected and then double-click "Methods, properties and events". Click on "Add" to add a new one and set it to "UpperCamelCase" with a Name Suffix of "ID".
Add additional suffixes as you like.
You can also do it with the "Advanced settings..." button on the "C# Naming Style" page, which gives you a lot more flexibility, but I wouldn't bother with that...
回答4:
What about "Add 'ID' to the abbreviations list" ??
来源:https://stackoverflow.com/questions/3351304/resharper-complains-about-uppercase-id-in-member-entityid