views:

1048

answers:

20
+9  Q: 

ID, id, or Id?

I use camelCase in my code and database field names, etc, but with fields that have Id at the end, it always ends up being hard to read. For example, itemId, teacherId, unitId, etc. In these cases I consider breaking convention and writing itemID, teacherID, or unitID just for improved readability.

What do you do and what's a general, best practice of dealing with this issue?

+10  A: 

I do what i feel like. In general, your best practice is to err in favor of readability vs. compliance with some abstract standard.

Just be consistent.

Shog9
CW dream last only a few seconds.
OscarRyz
c'est la vie, etc. etc. ;-)
Shog9
+4  A: 

I use camelcase, too and I also use it if the name ends with Id.

lothar
+2  A: 

Id

This is my subjective answer for the subjective question.

I have marked this as CW though.

OscarRyz
The problem with Id is how the I looks similar to non-capital t. So unitId is quite ambiguous to read
Click Upvote
In this font it's similar. While using a monospaced font (which i try to use everywhere where i code) it does not.
Ólafur Waage
Or a lowercase l. Even in monospaced fonts, there can be issues.
Andrei Krotkov
Monospaced fonts are not necessarily serif.
Stefan Kendall
A: 

I prefer to use Id and in our company the standard is also Id over ID. I don't think there is a problem using ID if you find it easier to read. The compiler doesn't care :) I use the same convention in my schema Id columns.

typemismatch
+1  A: 

I think that readability is of utmost importance, and should override your convention if it's going to make it that much easier to read. I think the cons outweigh the pros for sticking to your guns/convention if you can't read it easily (as much as we programmers hate to break conventions and protocols).

Matt Hanson
+5  A: 

Id, as for Identifier. Microsoft's suggested naming conventions indicate that that's the recommended practice and compiling with code analysis will support that. You would use ID if it stood for two words starting respectively with I and D and you're really not supposed to use names starting with lowercase letters but in parameters.

emaster70
ID stands for "Identifying Digits." It's possible to create an acronym for anything. ;)
Andrei Krotkov
Agreed. Every non-trivial project should be using Code Analysis (FxCop) which will suggest "Id" instead of "ID".
Dan
+4  A: 

It doesn't matter, just as long as you're consistent throughout your program.

That being said, I'd go with "Id".

Jon Limjap
I agree with both statements, be consistent and go with 'Id' because... 404
John Himmelman
+17  A: 

Here's a sample of what I do.

id
userId
getUserId

The key is being consistent.

Ólafur Waage
Added the CW when i saw how many others had.
Ólafur Waage
All the cool kids are doing it!
Daniel Lew
@Daniel they sure are :)
Ólafur Waage
+4  A: 

I use underscores in all of my table names, so user_id. Even if id is standalone, it is all lowercase.

X-Istence
A: 

Different languages have different guidelines. For .NET you may read Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries. For Java you may read Code Conventions for the Java Programming Language.

knut
+5  A: 

Id is an abbreviation, not an acronym, so I case it "Id." UI is an acronym, and acronyms--short ones, anyway--get capitalized: "UI."

Curt Nichols
Best answer here; surprised nobody's upvoted this. +1 :)
bryan
FWIW, some would suggest (as the .NET Framework Design Guidelines do) that only two-letter acronyms are fully capitalized, all longer acronyms are capitalized as normal words. E.g., DeferIO and XmlReader. It seams a reasonable and readable approach. But Id is still Id. :)
Curt Nichols
+10  A: 

I actually prefer "ID". But, as everyone says, consistency is the most important thing.

Steve

Steve Harrison
I use "ID", it breaks consistency but it is more readable.
awaisj
Yes, I find it more readable, which is why I use it. But if you always use "ID" (e.g. "userID", "commentID"), why does it break consistency?
Steve Harrison
He might mean that the rest of the code is camalCase and ID is the only thing which isn't
Click Upvote
+1  A: 

In Machine SUIF, Mike Smith and Glenn Holloway rigidly enforce the case convention that a capital letter marks a new word. So even though CPS is an acronym it's transformToCps and not transformToCPS. I have found that in the long run, their method works better than what we did in Quick C--, where we usually did all caps for cases like ID and CPS.

Norman Ramsey
+2  A: 

I am I going to be voted down here, but I don't care!

it's obviously id. Ideas from the deep!

devin
+1  A: 

FxCop/Code Analysis will flag ID as being a bad abbreviation, so if you want to avoid disabling a rule, you might conform and use Id for that reason. But again, this really doesn't matter, as long as you are consistent, as has been pointed out by others.

jlembke
+1  A: 

Whatever you are comfortable with as long as you remain consistent within your own programs. (If you work in a team, you should follow the team's rule or set up some).

One point that hasn't been mentioned is the importance of getting a good font. That will do wonders to your program readability and it could be that your convention problem is partly a font problem:

If you can't easily distinguish Id from ld (Id and ld), you should to change font and perhaps font size too. Personally, I love Consolas.

Sylverdrag
+1  A: 

I agree with the other answers that the most important thing is to be consistent.

I'd add that, if there isn't an established standard for your particular codebase, you should follow the conventions set by your language or platform. In Java, acronyms and other capitalized words are uncapitalized for identifiers:

id
url
getId()
setUrlParameters()

In Objective-C, it's the opposite. You might have a lowercase "id" or "url" variable, but you also have classes such as:

NSURL
NSURLRequest

So in Obj-C I would choose a method name like:

setURLParameters:
The capitalized constants in C look very ugly. PHP has also inherited some of these.
Click Upvote
+1  A: 

It's pronounced "eye dee", not "id as in id, ego and superego", so ID, not Id. That's my vote, anyhow. The fact that it's an abbreviation, rather than an acronym is irrelevant, due to the way that it's pronounced. It's pronounced as if it were an acronym, so it may as well be typed that way as well. Oh, and camel case is the worst idea in the history of computing.:)

smcameron
A: 

"Id" is ambiguous. Is it part of the "ego" / "superego" model of the human psyche? Probably not: you probably are trying to abbreviate a word, like "Identity" or "Identification" or "Identifier".

Get rid of the ambiguity (and the casing question) by deciding what you mean and then don't abbreviate.

Jay Bazuzi
A: 

ID in database columns and object properties. id in parameters:

this.ID == id
n8wrl