161

This is a bit pedantic, but I've seen some people use Id as in:

private int userId;
public int getUserId();

and others use:

private int userID;
public int getUserID();

Is one of these a better name than the other? Why? I've seen this done very inconsistently in large projects. If I were to set a standard which would most people be familiar with? Which is the conventional standard?

Deduplicator
  • 8,591
  • 5
  • 31
  • 50
Adam
  • 2,141
  • 2
  • 16
  • 15
  • 44
    Consistency is the most important thing that matters. Be it camel case, or underscores or whatnot. Be consistent. –  Feb 07 '13 at 23:28
  • 42
    Look at your language's XML APIs to see how they do it. Java names classes like `SAXParser` and `DOMException`, .NET names classes like `XmlDocument`. Based on that, I'd say "ID" in Java, "Id" in C#. – luiscubal Feb 08 '13 at 00:12
  • 2
    But the upper-case identifiers, by convention, are used in Java for static fields, so the "ID" name for base field is not the best one. And there comes the consistency... – Danubian Sailor Feb 08 '13 at 08:57
  • 1
    I am currently working with a HUGE codebase that frequently interchanges `id`, `Id`, and `ID`. It is extremely painful to work with. – Simon Whitehead Feb 08 '13 at 11:36
  • 9
    Would you name the variables `EGO` and `SuperEGO`? I didn't think so. ;) – kojiro Feb 08 '13 at 13:20
  • 1
    Usually I would go uppercase for acronyms only (eg HTML). "Id" is not an acronym, it's an abbreviation for identity, so I treat it the same as a regular, un-abbreviated identifier and remain consistent with the language conventions (PascalCase in .NET) – MattDavey Feb 11 '13 at 11:13
  • @luiscubal Please make your comment into an answer, it makes the most sense to me. – Adam Feb 11 '13 at 20:11
  • 1
    I suggest you take an existing style guide and use it. Here is Google's Java style guide for Android developers: http://source.android.com/source/code-style.html#treat-acronyms-as-words – Dave Feb 11 '13 at 20:59
  • @Adam I've posted an expanded answer. – luiscubal Feb 11 '13 at 21:31
  • In some territories, ID is an acronym for Identification Documents. – JBRWilkinson Feb 12 '13 at 08:32
  • 4
    What?! Consistency? Where's the rage war?! That's it, I hereby nominate myself keeper of the sacred camel-case syntax flame and hereby decree that doing it with all-caps for acryonyms is for noobs. Also, it's correct to want to have the toilet paper roll from the top unless you have cats who get bored and do strange things, in which case they have much harder time unraveling the toilet paper set to roll from the bottom making such heresy acceptable for cat owners. I don't know why I get to decree on that. Bob the keeper of the toilet-roll-direction's sacred flame is busy I guess. – Erik Reppen Feb 24 '13 at 06:35
  • If using camelcase convention, it should definitely be xxId – Kemoda Mar 01 '13 at 14:21
  • I don't understand why this question has so many upvotes. – SH7890 Aug 02 '17 at 20:08

6 Answers6

116

Consistency is king; pick one or the other, but do it consistently everywhere.

That said, I prefer the first variation, because it doesn't violate camelCase (doing so means you have two style rules to remember, not just one).

Two capital letters is sometimes used because of this, but an ID is really just a form of Id-entification.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • 18
    I for one wouldn't like it if a computer program were trying to access my id. – Blrfl Feb 07 '13 at 18:17
  • 1
    `userIdOfSender` – Sean McSomething Feb 07 '13 at 18:39
  • 24
    @SeanMcSomething: Ick. `SenderUserId` – Robert Harvey Feb 07 '13 at 18:41
  • 1
    As long as you're consistent, it really doesn't matter. We use "Id" simply because Resharper pings you for two capital letters in a row in a variable name, and we'd rather go with the flow than add an exception for "ID". – Carson63000 Feb 07 '13 at 21:27
  • 6
    While I agree with you that "Id" is the preferred way I can see where the confusion comes in: In day-to-day conversation we actually say it as if it were an acronym, as in "can I see your I D?" – 500 - Internal Server Error Feb 07 '13 at 23:08
  • Also it's easier to type Id than ID (Two consecutive upper cases takes more time. May have to use CAPS lock) – Pramod Feb 08 '13 at 06:47
  • 3
    Look at other acronyms in camel case. There's SoapProtocol, not SOAPProtocol. ID is short for identity document, so I don't see why it should be treated in an exceptional fashion in camel case. That said, I'd prefer userID used consistently than userId and userID used inconsistently in my program. – Neil Feb 08 '13 at 10:20
  • @Pramod Really? I find typing ID much more easier than Id because you have to conscientiously let go of the shift key when you are typing the lowercase d. It's rather easy to accidently type two upper case letters when you actually meant to type one upper case and one lower case, because your timing might be slightly off on when you release your shift key. – TtT23 Feb 08 '13 at 11:29
  • 1
    @Pramod: (and l46kok): _Typing speed_ isn't really the issue, is it? Seems like _readability_ is more important. It makes me crazy to see comments like this because they imply that the only thing keeping the code from flowing more quickly from one's fingers is the mechanics of using the keyboard. Seriously? Are you sure the delays aren't from debugging your own work, uncertainty about some API, difficult algorithms, poor/missing specs, or various interruptions? Lots of things besides keyboard mechanics make development a slow process... – JeffK Feb 08 '13 at 21:42
  • @JeffK I know, I would never ever sacrifice readability just to write faster or easier. – TtT23 Feb 09 '13 at 04:32
  • 1
    I.D. is an initialism for Identity Document, it isn't short for identity. – Tom Hawtin - tackline Feb 10 '13 at 01:40
  • 1
    I think it's obvious that in this context ID is not an acronym for "identity document". In this context it's "identity" or "indentifier", as in a database identity column, or an identifier in code. – MattDavey Feb 12 '13 at 09:10
  • 1
    The consensus of our English friends on stack exchange seems to be that 'identification' is best abbreviated 'ID': http://english.stackexchange.com/questions/21081/different-forms-of-the-abbreviations-for-identification http://english.stackexchange.com/questions/101248/should-the-abbreviation-for-identifier-be-capitalized – Eric G Feb 19 '13 at 18:16
  • I blame MS and their XMLHttpRequest for all this insanity! – Astravagrant Aug 30 '16 at 15:46
82

TL;DR: In the context of .NET class libraries, Microsoft recommends that you use Id. This is slightly counter-intuitive, since it's a rare example of an abbreviation that is allowed / recommended (abbreviations are generally frowned upon).

If we're talking about C# or .NET class library conventions, Microsoft has some fairly well defined naming guidelines available. They are well thought out, with many explanations on a variety of issues - actually, every developer should take some time to read the entire Design Guidelines section.

When it comes to acronyms, the rule of thumb is: for two letter acronyms, you tend to keep them upper case (where Pascal case is applicable), so e.g. IOStream might be the name of a class. For a longer acronym, you lower case the rest of the acronym, e.g. XmlDocument or HtmlParser. This is actually a mostly unambiguous rule (there's no confusion as to where the one word ends and the next begins, unless you're chaining two-letter acronyms), and you get used to it very quickly.

So, is it ID, or Id? Well, according to Microsoft, it might not be what you think:

Acronyms differ from abbreviations in that an abbreviation shortens a single word. For example, ID is an abbreviation for identifier. In general, library names should not use abbreviations.

The two abbreviations that can be used in identifiers are ID and OK. In Pascal-cased identifiers they should appear as Id, and Ok. If used as the first word in a camel-cased identifier, they should appear as id and ok, respectively.

Anecdotally, I'm not actually sure when this distinction started appearing in the guidelines, but a few years back (around 3.0 / 3.5) the general naming trend in class libraries went from ID to Id.

Daniel B
  • 6,184
  • 1
  • 22
  • 30
  • 2
    This is the guideline I usually follow. Since id is an abbreviation and not an acronym, I always prefer to use 'Id'. – Toby Feb 08 '13 at 14:51
  • I use ID becuase then it breaks the convention and stands out as being unique, and i like the irony of that :) –  Feb 08 '13 at 15:17
  • 1
    I think Microsoft are wrong. I.D. is an initialism for Identity Document, it isn't short for identity. (Pedantically, acronyms are pronounceable.) – Tom Hawtin - tackline Feb 10 '13 at 01:42
  • @TomHawtin-tackline You make an interesting point, although I suspect that it depends on the context. Something like an IDNumber property on a Person object would make a lot of sense, but for a VehicleId to read as "Vehicle Identity Document" versus "Vehicle Identifier"? In programming contexts, identifier is a pretty common word for anything which uniquely identifies an instance, and I'd argue that it's more applicable here. – Daniel B Feb 11 '13 at 07:15
  • @DanielB In computer languages, even SQL, "identifier" usually refers to a name, such as the name of a column. Typically it gets abbreviated to "ident". Vehicle is an interesting example because there are established VIN schemes (Vehicle Identification Numbers). In a typical programming context, the "document" for an entity is a number (could even be an unforgeable capability). – Tom Hawtin - tackline Feb 12 '13 at 02:11
  • I like that you pointed out that MS used "ID" in the past. The MS style guide I'd read 6-7 years ago required any two-letter acronyms to be uppercase, and I've been using that in my MS work since. That it had changed was news to me! And welcome, frankly. – Jerph Feb 16 '13 at 15:25
  • "The two abbreviations that can be used in identifiers are ID and OK" - OK is an abbreviation for...? – Sklivvz Feb 23 '13 at 23:33
  • @Sklivvz There are a [whole bunch of competing etymologies](http://en.wikipedia.org/wiki/Okay), but generally "OK" is thought to be short for "okay". – Daniel B Feb 24 '13 at 16:54
  • 1
    Erm your own link says otherwise as OK stands for OLL KORRECT (and similar) and thus it is not an abbreviation. Okay is the phonetic version of OK (from *oh kay*) – Sklivvz Feb 24 '13 at 23:13
  • @Sklivvz Yes, it says that under "alternative etymologies". Nobody's quite sure which is correct, but your version is thought to be less likely. – Daniel B Feb 25 '13 at 05:41
  • If you are a Microsoft shop (programming in C#, servers are running ASP.NET on Windows Server 20xx, etc.), you should probably just use Microsoft's convention (Id) without getting into pointless etymological arguments. I would have preferred ID, but this is Microsoft's language, so I'll use their convention over my preference. – NH. Mar 13 '18 at 21:05
  • It's hard to say when ID is used as abbreviation of "Identity"/"Identifier" or acronym of "Identity Document". I myself wasn't aware of the latter meaning until I googled today. I'd say in programming it has become a new word (as in "unique id") mixing both of those senses. But when spelling it also matters **how it's pronounced** — and everybody I knows says is by letters I.D. not as single syllable, hence to me it makes sense to spell ID as I would an abbreviation. – Beni Cherniavsky-Paskin Jan 31 '22 at 12:03
67

The most important rule to follow in these cases is consistency: Do as everyone else does.

For instance, look at your language's XML APIs to see how they do it.

Java names classes like SAXParser and DOMException, .NET names classes like XmlDocument.

Based on that, I'd say "ID" in Java, "Id" in C#.

However, I've seen that Java EE 6 has an annotation named @Id (see documentation), so it seems Java considers "Id" to be just a normal word.

luiscubal
  • 1,517
  • 12
  • 13
  • @Id points to an annotation classname, not a variable name. Wrong example. – jwenting Feb 12 '13 at 07:26
  • 3
    SAXParser could be (and luckily is not) SimpleAPIforXMLParser (or even SimpleApplicationProgramingInterfaceforExtesibleMarkupLanguageParser). Each capital letter is begining of word. So even in java it should be "Id" – user470365 Feb 12 '13 at 10:42
  • 2
    @jwenting The problem is finding out whether "id" is considered like a word or like two words. `@Id` says that it is a single word, so the variable name would be "id". – luiscubal Feb 12 '13 at 11:46
  • No. SAX is an acronym while Id is not. – nalply Feb 14 '13 at 19:18
  • 8
    You're right about using `Id` in C# (and .NET in general), but for a different reason. The rule is capitalizing all letters of a 2-letter acronym (e.g. `IPAddress`) and only capitalizing the first letter of longer acronyms (like the example of `XmlDocument` you gave). But `Id` and `Ok` are the exceptions to this rule, specifically mentioned. For the full brief, see the `Capitalization Rules for Acronyms` section of the [`Capitalization Conventions`](http://msdn.microsoft.com/en-us/library/vstudio/ms229043.aspx) article. But even Microsoft breaks that rule (e.g. `DbConnection` vs. `DBNull`) – Allon Guralnek Feb 14 '13 at 21:29
  • @AllonGuralnek Indeed. Daniel B has the correct answer for the .NET convention – luiscubal Feb 14 '13 at 22:52
  • Unfortunately, PHP has classes like DOMDocument, DomXPath and stdClass... – itsmequinn Aug 02 '13 at 13:15
  • FYI, Java also has a [HttpURLConnection](https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html) class... Nice one `;)` – sp00m Mar 16 '16 at 16:11
  • @AllonGuralnek `Id` is not really an exception to that rule, it's not actually covered by that rule because it's not an acronym. It's an abbreviation of a single word `identifier`, unless you mean to use it as in `identity document` which would be appropriate if you're storing information on actual identity documents, but not really for the normal use case where it's an identifier number or string for an entity. – Vala Aug 19 '16 at 08:58
  • Java is inconsistent about its camelCasing, though more recent APIs seem to have settled on capitalizing just the first letter: `InvalidRelationIdException`, `RowId`, `ZoneId`. Some non-ID examples: `AclEntry`, `DosFileAttributes`, `IsoChronology` – M. Justin Dec 17 '20 at 20:25
16

I read a very good explanation in some coding conventions' document. CamelCase should always be used for acronyms and abbreviations, because it is easier to distinguish word boundaries (compare XmlIdWriter to XMLIDWriter).

kgilden
  • 521
  • 4
  • 11
  • 16
    Here is an even better idea for distinguishing word boundaries: actual word boundaries! `xml_id_writer`. – Kaz Feb 07 '13 at 23:01
  • 5
    @Kaz Well, duh! However, CamelCase is used traditionally in some languages and it would look rather out of place to use underscores in such situations. Consistency is king, as mentioned earlier. – kgilden Feb 08 '13 at 10:38
  • 1
    Using CamelCase just because the core libraries of some language use it isn't consistency, but rather conformity. – Kaz Feb 08 '13 at 19:00
  • 4
    @Kaz: You have bigger battles to fight in your shop than code conventions. – Robert Harvey Feb 13 '13 at 16:22
3

As we can see in JavaScript's default function getElementById(); Id is written in Camel case...

Use 'id' if using with an underscore. Example: user_id

Use 'Id' if naming a var without any Underscore to differentiate the different words. Example: userId

If its a single word variable it should be in complete lowercase, if multiple word var then use lower Camel case. Example: thisIsExample

But I highly would not recommend 'ID' all in CAPS because we generally use all caps for defining CONSTANTS.

Sukrit Gupta
  • 149
  • 6
1

First, eschew abbreviation.

Second, If the abbreviation is super well known, I recommend to use camel case.

That's because you're not need to consider the meaning of that. just treat as a normal word

Sungguk Lim
  • 396
  • 2
  • 11