.NET naming convention for “ID” (anything identification: Capitalization)

久未见 提交于 2019-12-02 17:05:54

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

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

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.

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.

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)

I always capitalize ID when it is an abbreviation for identifier, etc. It just looks wrong and makes me think of Freud, otherwise -- which is definitely not a good thing.

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.

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'.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!