Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
34  
Consistency is the most important thing that matters. Be it camel case, or underscores or whatnot. Be consistent. –  MichaelT Feb 7 '13 at 23:28
35  
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 8 '13 at 0:12
1  
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... –  РСТȢѸФХѾЦЧШЩЪЫЬѢѤЮѦѪѨѬѠѺѮѰѲѴ Feb 8 '13 at 8: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 8 '13 at 11:36
7  
Would you name the variables EGO and SuperEGO? I didn't think so. ;) –  kojiro Feb 8 '13 at 13:20

6 Answers 6

up vote 23 down vote accepted

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.

share|improve this answer
    
@Id points to an annotation classname, not a variable name. Wrong example. –  jwenting Feb 12 '13 at 7: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
4  
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 article. But even Microsoft breaks that rule (e.g. DbConnection vs. DBNull) –  Allon Guralnek Feb 14 '13 at 21:29

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

share|improve this answer

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.

share|improve this answer
    
In your third paragraph, your example does not seem to match your text? –  ruakh Feb 8 '13 at 17:33
    
@ruakh thanx.. Rectified.. –  Danton Feb 11 '13 at 10:31

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.

share|improve this answer
1  
This is the guideline I usually follow. Since id is an abbreviation and not an acronym, I always prefer to use 'Id'. –  Toby Feb 8 '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 :) –  RhysW Feb 8 '13 at 15:17
    
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 1: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 7: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 2:11

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

share|improve this answer
10  
Here is an even better idea for distinguishing word boundaries: actual word boundaries! xml_id_writer. –  Kaz Feb 7 '13 at 23:01
3  
@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. –  gilden Feb 8 '13 at 10:38
    
Using CamelCase just because the core libraries of some language use it isn't consistency, but rather conformity. –  Kaz Feb 8 '13 at 19:00
    
@Kaz: You have bigger battles to fight in your shop than code conventions. –  Robert Harvey Feb 13 '13 at 16:22

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.

share|improve this answer
16  
I for one wouldn't like it if a computer program were trying to access my id. –  Blrfl Feb 7 '13 at 18:17
1  
userIdOfSender –  Sean McSomething Feb 7 '13 at 18:39
10  
@SeanMcSomething: Ick. SenderUserId –  Robert Harvey Feb 7 '13 at 18:41
4  
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 7 '13 at 23:08
2  
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 8 '13 at 10:20

protected by maple_shaft Feb 8 '13 at 18:58

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.