Is it bad practice to use the same name for arguments and members?
- by stijn
Sometimes I write constructor code like
class X
{
public:
X( const int numberOfThingsToDo ) :
numberOfThingsToDo( numberOfThingsToDo )
{
}
private:
int numberOfThingsToDo;
};
or in C#
class X
{
public X( int numberOfThingsToDo )
{
this.numberOfThingsToDo = numberOfThingsToDo;
}
private int numberOfThingsToDo;
}
I think the main reason is that when I come up with a suitable member name, I see no reason to use a different one for the argument initializing it, and since I'm also no fan of using underscores the easiest is just to pick the same name. After all it's suitable.
Is this considered bad practice however? Any drawbacks (apart from shooting yourself in the foot when forgetting the this in C#)?