views:

26

answers:

3

I'm in the middle of a debate here at work. Say you have a POCO named Person. Person has an identifier. I'm in the camp that says the identifier should be named Id.

class Person
{
    public int Id { get; set; }
}

The other camp says it should be named PersonId.

class Person
{
    public int PersonId { get; set; }
}

Is one more correct than the other?

I think that using Id is more readable and definitely less code to type.

The argument against Id is that it may be confusing because someone may not know if the ID belongs to a Person or an Address. I think that argument falls flat if you just name your variables descriptively.

+1  A: 

Even if you don't name your variables descriptively, intellisense will give you the class if there's any doubt. I don't know from "more correct", but I personally use Id as long as the class will be clear during foreseeable use. If it is a situation where the class may be unclear during use (as, for example, in the case of tables in a database or attributes in an XML file), then I use ClassId. When it comes down to it, though, which you use is a really a matter of taste and either option isn't going to slow anyone down perceptably.

Jacob Proffitt
A: 

Id and PersonId are both good names for variables representing the unconcious part of the person's psyche that contains their basic drives. I'd slightly favour the former.

I'd prefer ID (not Id) for an identifier, since classes are namespaces (in the general, not .NET-specific, sense) and what it's an ID of is clear. I wouldn't get terribly upset about PersonID though (again, not PersonId).

Jon Hanna
The MS Design Guidelines recommend `Id` rather than `ID`: *"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."* http://msdn.microsoft.com/en-us/library/ms229043.aspx
LukeH
Yes they do. I still read Id as the word "id" and ID as the abbreviation "ID". I've no issue to id though, presumably as it does the same mutation to both letters.
Jon Hanna
+1  A: 

The real issue here is: what's your standard for naming variables that hold a reference?

 obj.Id         // Not good, better call it PersonId
 teamLead.Id    // Okay, quacks like a Person
 person.Id      // Okay, clear but not the greatest var name

I always pick good variables names without fail, I like Id :) The framework classes heavily favor plain Id.

Hans Passant
I totally agree, good variable names go a long way. Thanks for pointing out that the framework uses Id. I did some checking and you're absolutely right. More great ammo for me! :)
Ecyrb