.NET naming convention for “ID” (anything identifi

2020-05-15 13:40发布

I am in the process of unifying these inconsistent naming conventions and this one problem has been a bit driving me crazy lately.

In the code base I am working with has no convention regarding "ID"; "ID", "Id" and even "iD" are used inconsistently.

****Question**: In .NET, how do you guys capitalize "ID"? For an example, nodeID, nodeId? FolderID or FolderId?

****Edit**: How about plural cases? then should I do "NodeIDs" or "NodeIds"?

Thanks

7条回答
该账号已被封号
2楼-- · 2020-05-15 14:13

Capitalization is for 2 letters acronyms. UI, IP, etc.

"Id" is an abbreviation for Identifier, so it should stay pascal cased.

查看更多
虎瘦雄心在
3楼-- · 2020-05-15 14:19

I think that the reason some people are using 'ID' instead of 'Id' is because it is a subset of 'GUID', 'UUID' or 'UID' that are generally capitalized.

I agree that it should be 'Id' but I think that it strongly depends on the context because in some environments it might be more appropriate to use 'ID'.

So to me 'ID' is a technical term and a subset of the above or used to refer either one of these technical terms whereas 'Id' is the shorthand to any kind of identifier and in a non-technical context when you need a person to read it like in your public APIs or UML you should (in my opinion) use 'Id'.

查看更多
Juvenile、少年°
4楼-- · 2020-05-15 14:21

Microsoft has updated its guidelines since this question was asked.

In the MSDN article Capitalization Conventions (.NET Framework 4.5), there is a section named Capitalizing Compound Words and Common Terms. Within the table in this section, the following capitalization guidelines are given for the abbreviation for "identifier" when naming identifiers:

  • When Pascal casing applies, use Id. (Example: System.Attribute.TypeId)
  • When Camel casing applies, use id. (Example: var id = 42;)
  • Never use ID.
查看更多
▲ chillily
5楼-- · 2020-05-15 14:21

The latest guidance is "Id", for more on this and others (e.g. "Ok"), see my post on the very latest Framework Design Guidelines (2nd edition)

查看更多
成全新的幸福
6楼-- · 2020-05-15 14:23

If you would care to check the dictionary, you would find that ID is not treated as an abbreviation. In fact, the form is specified as ID or I.D. there. This is probably because it came from an expression like "Identity Documentation" or "Identification Data", and was not originally taken as a short form of "Identifier". Thus the form ID is not only recommended, but is directly supported by the dictionary.

查看更多
The star\"
7楼-- · 2020-05-15 14:28

Microsoft's naming guidelines suggest using all capitalized for 2 letter acronyms made into identifiers (IP, IO, UI, etc), so I tend towards "ID" (even though it's not an acronym) because when I read it, I still say the letters individually.

But honestly, I don't think Microsoft knows/knew what to do with ID/Id either:

ID

System.Runtime.InteropServices._Activator.GetIDsOfNames()
System._AppDomain.GetIDsOfNames()
System.Runtime.InteropServices._Attribute.GetIDsOfNames()
System.Type.GetTypeFromProgID()
System.Threading.Thread.ThreadID
System.Threading.Thread.GetDomainID()
System.Runtime.Serialization.ObjectHolder.ContainerID
System.Globalization.Calendar.ID
System.Globalization.CultureInfo.InvariantCultureID
System.Web.UI.Control.ClientID
System.Web.UI.Control.UniqueID

Id

System.AppDomain.GetCurrentThreadId()
System.AppDomain.GetIdForUnload()
System.AppDomain.IsDomainIdValid()
System.AppDomain.GetId()
System.Attribute.TypeId
System.TypeLoadException.ResourceId
System.Reflection.AssemblyAlgorithm.AssemblyAlgorithmAttribute.AlgorithmId
System.Runtime.Remoting.Lifetime.Lease.GetNextId()
System.Xml.Xpath.XPathNavigator.UniqueId
System.Data.OleDb.DBPropSet.PropertyId

(from http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/dcb8e08b-026a-4903-a413-7dbdda131a82/)

I guess that's why they invented intellisense...

In regards to pluralization: in my mind the s should always be lower-case.

查看更多
登录 后发表回答