Is it bad practice to use the same name for arguments and members?
Posted
by
stijn
on Programmers
See other posts from Programmers
or by stijn
Published on 2012-10-08T15:51:19Z
Indexed on
2012/10/08
21:50 UTC
Read the original article
Hit count: 260
naming
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#)?
© Programmers or respective owner