views:

251

answers:

5

Possible Duplicate:
Giving a property the same name as its class

I will frequently do/see this for local variables:

MyObject myObject = new MyObject();

I occasionally get stuck with my naming convention that has public properties capitalized. Something like this:

public MyObject MyObject { get; set;}

Is this bad? The compiler knows, but I code more for those who must maintain what I code then for the compiler.

Is there an accepted best practice on this?

+3  A: 

I think a good idea is to not name your class 'object'.

MyClass MyObject = new MyClass();

Then you won't have a problem. In general giving things the same name is bad news. It really makes searching through code a pain.

Carl Norum
We do have case sensitive text searching though, so I'm not sure this is really a problem...
jball
@jball, that doesn't fix his problem, which is where they're the same case.
Carl Norum
He's got two examples. One has the variable differentiated by case from its class. The other has a property that has the same name for it and its class, yielding exactly one extra hit on a string search. Not that onerous.
jball
The name of the class was just an example. It could have been anything. Color being a good example used by Eric.
Vaccano
+1  A: 

I would simply give it a better name. You won't name your variable like this, would you?

public String String { get; set;}  

Other things to watch for. It is not just for variables. It could be your namespace.

e.g.

namespace FootballDao
{
     class FootballDao
     {
     }
}
Syd
Using primitives as an example doesn't really cut it. I think hardly anybody would think of naming elements after their primitive types.
Sandor Drieënhuizen
Hi Sandor, Minor correction. String is a class. bool is a primitive (cf Boolean). ditto for int (c.f Integer). hope this helps.
Syd
@Syd, you're right about that. I think saying 'primitives/value types/simple immutable types' should suffice. Ofcourse there are exceptions but I think value types should have a 'primitive' characteristic anyway.
Sandor Drieënhuizen
+2  A: 

If the class that will be used as a property is descriptively named and would be the best property name as well, I do it when it seems clear to me, e.g.:

public class ContactInfo
{
   //...
}

public class Person
{
    //...
    public ContactInfo ContactInfo { get; set; }
    //...
}

public class Company
{
    //...
    public ContactInfo ContactInfo { get; set; }
    //...
}
jball
+14  A: 

I note that this is a duplicate of

http://stackoverflow.com/questions/1095644/giving-a-property-the-same-name-as-its-class

and

http://stackoverflow.com/questions/1746117/why-is-it-possible-to-have-properties-named-the-same-as-their-return-types

It is a perfectly acceptable practice; the C# language has been carefully designed so that this is legal and practical. Of course, if you can easily avoid it, do so. But if you cannot, don't stress about it.

The canonical example is a type called Color, and a property called Color. It doesn't make any sense to rename either of them, so you end up with

public Color Color { get { ...

This is known as the "Color Color" problem. For some additional thoughts on the implications on the design of C#, see my article on the subject:

http://blogs.msdn.com/b/ericlippert/archive/2009/07/06/color-color.aspx

Eric Lippert
+1 The "Color Color" problem made me laugh out loud. Does it change with the Type or is it always literally known on the team as the "Color Color" problem in any case?
John K
@jdk: It's always called the "color color" problem; that's what we call it in the specification so it's a handy short way to characterize the problem.
Eric Lippert
+1  A: 

Yes, it is a bad idea in most cases.

A good naming convention is to assign descriptive identifiers to various programming elements (classes, variables, methods and so on) to make them as self-documenting as possible.

Variables typically exist in relatively narrow scopes and their names should clearly state the role they play in the specific context they are used in. Types, on the contrary, operate at a wider level and should be named accordingly.

For example you do write:

Order lastProcessedOrder;
bool isOrderValidated;

and you don't write:

int integer;
bool boolean;

If these naming rules are followed, it should come naturally that variables in most cases don't share the same name as the type they represent.

Enrico Campidoglio
Ofcourse one wouldn't use primitive type names as element names, that's pretty obvious. Writing `Order Order` however, would be perfectly sane IMHO. It's simply because primitives always need context to give them any meaning while classes often do not.
Sandor Drieënhuizen
Agreed. The example with primitives is maybe a little too extreme. However I still believe that naming variables the same as classes should be avoided whenever possible, which indeed is the majority of cases.
Enrico Campidoglio
It's possible in the `Color Color` case, but would you do it?
Sandor Drieënhuizen
Well, the "Color Color" case is a though one to solve. I would try to think about what's specific about the color in the particular context where it is being used. Some examples could be "ForegroundColor", "BackgroundColor", or maybe just "CurrentColor". If I can't find a name that makes sense, then I would go for "Color", but I definitely wouldn't like it :-)
Enrico Campidoglio