I just read Tor Norbye's latest blog entry Code Advice \#11: Initialize fields using the property name, and I have to say I disagree with him. I used the coding style where the name of the variable is prefixed with a character to indicate the scope. The following table outlines the rules.

Variable Rule Example
member Prefix with 'm' mTemperature
parameter N/a temperature
local N/a temperature
static Prefix with 's' sTemperature
static final All uppercase MAX\_TEMP

So to use Tor's example, the setter method would be written as:

void setTemperature(int temperature) {
    mTemperature = temperature;
}

Advantages of this style are:

  • Misalignments stand out, as in:
    void setTemperature(int temperature) {
        mTemperature = mTemperature;
    }
  • You can still use nice English names for parameters and local variables.
  • It is easier when reading code fragments (for example diffs) to determine the scope of a variable. For example, it is easy to read the following code fragment:
    if (mTemperature < predictedTemperature) {
        mTemperature = predictedTemperature;
        fireNewMaximum(mTemperature);
    }
  • It is readily supported by IDE's that generate setters and getters.

I enforce this style with the following checks in my Checkstyle configurations:

<module name="ConstantName"/>
<module name="ParameterName"/>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName">
  <property name="format" value="^m[a-zA-Z0-9]*$"/>
</module>
<module name="StaticVariableName">
  <property name="format" value="^s[a-zA-Z0-9]*$"/>
</module>