The "member" prefix is kind of an implementation detail. It distracts your attention from the problem domain to the technical solution domain which biases your mind when thinking of possible refactorings. – me
can you explain further? Yours is the only reason I've seen for why to not use the prefix and I don't understand it ... (by which I mean, the other things people are saying are reasons why I don't have to use it, which is not the same as why I shouldn't) – Betty Crokker
My point is extending the answers of @CandiedOrange and @Ewan.
Prefixes make refactoring harder
As your code evolves variables and methods may change their scope. Eg. you may create a private method and later finding out that it should be available to other (new) classes too. Similar may happen to method parameters and local variables.
Let's say you create a Tax calculator. According to the principle of lease visibility scope for variables you start with a method that takes both, the tax rate and the base value as parameters:
(example may look Java-ish...)
class TaxCalculator{
double Calculate(double p_TaxRateInpercent, double p_BaseValue){
return (100+p_TaxRateInpercent)/100 * p_BaseValue;
}
}
next you have to implement the reverse operation and according to TDD you do it with the least effort:
class TaxCalculator{
double Calculate(double p_TaxRateInpercent, double p_BaseValue){
return (100+p_TaxRateInpercent)/100 * p_BaseValue;
}
double CalculateBase(double p_TaxRateInpercent, double p_TaxedValue){
return p_TaxedValue/((100+p_TaxRateInpercent)/100);
}
}
Next TDD wants you to refactor the code to become cleaner which is to reduce the parameter list of both methods.
(Yes, you would extract the doubled calculation first, but let me get my point...)
The obvious solution is to change tax rate into a member variable by passing it as a constructor parameter.
class TaxCalculator{
double m_TaxRateInpercent;
TaxCalculator(double p_TaxRateInpercent){
m_TaxRateInpercent = p_TaxRateInpercent;
}
double Calculate(double p_BaseValue){
return (100+m_TaxRateInpercent)/100 * p_BaseValue;
}
double CalculateBase(double p_TaxedValue){
return p_TaxedValue/((100+m_TaxRateInpercent)/100);
}
}
As you can see we had to change all lines of our existing methods (any line we used p_TaxRateInpercent
to be exact.
The problem is, that you have no support from your IDE to do the renaming throughout the class. The only help it search/replace which will also change the constructor or any part that accidentally contains the string p_TaxRateInpercent
.
You IDE might offer a change to ... refactoring for undefined variable names but this may be restricted to the scope of the method
Without the prefix only the method signatures would have changed. No renaming would have been needed at all.
Prefixes clutter SCM history when changed
Also the SCM records the change of the prefix as changes in the logic although the logic didn't change! The SCMs history is cluttered with this technical change hiding what is important and raising the risk for conflicts with the (real logic) changes of others.
this
keyword? Couldn't you refer to members usingthis
, such asthis.count
?count
didn't just appear out of no where since it wasn't declare in the method. Frankly the "out of IDE" argument is REALLY weak. Especially since there are even code review tools which work IN the IDE.