A class represents a 'thing' which can be a physical object like a customer or a logical concept like an equation.  Member variables are usually attributes of the 'thing'.

For example, if there was a class called a Customer, it might have member variables like firstname, surname and creationDate.

Tip: Member variables and local variables should always be written in camel case.  This means the first letter of each word is a capital letter, except for the first word.  eg. maxCarWidth, finalCost

Following is an example of some poorly named member variables.

public class DeviceProfile
    public static final int MAX_COLOURS = 256;
    private String device_id;
    private int _width;
    private int _height;
    private boolean SupportsBMP;
    private boolean SupportsJPG;
    private boolean SupportsGIF;
    private int colours_;
    private String ideviceName;
    private int intCount;

The width and height are too generic, since by adding the classname to the variable name they are still ambiguous “Device Profile Width”.  Underscore characters are never recommended anywhere in non-static member variables, even to represent special member variables.  The 'i' prefix adds no value.

A consistency review would produce the following code:

public class DeviceProfile
    public static final int MAX_COLOURS = 256;
    private String deviceId;
    private int resolutionWidth;
    private int resolutionHeight;
    private boolean supportsBmp;
    private boolean supportsJpg;
    private boolean supportsGif;
    private int colours;
    private String deviceName;
    private int failureCount;

Note - adding an underscore '_' or an 'i' or an 'm' to the start or end of member variables (to differentiate them from local variables) is rarely used and should not be done on new projects.

Some variable names that never caught on.

These are actual variables found during code reviews.




Just because our team leader Ned requested a total be added to the code does not mean his name should be enshrined in the variable.  Ned is Great.  All hail The Ned.



Is the obj and o_ prefix so that we know it is an Object?  In most modern programming languages, Objects are pervasive, so these prefixes add no value in a variable name.


Random capitalisation.  Capitalisation should be at word boundaries only.


If the “n” is supposed to show that the variable represents a number then Hungarian notation is generally discouraged. “Var” clearly means “variable” which is assumed anyway.  Numbers such as “5” are generally discouraged in variable and class names.


Why name a variable after a getter?  Why once? What value does it contain?


Impossible to tell what the variable represents.


This looks like a 1 in some fonts.


Misspelled.  If you can't spell, check all your variables names.


This is a name.  Numbers are used in variable names only in very rare cases, such as representing a numbered standard.


A double offender - this is from a story and contains a '_'


Too Abstract


Unsociable Unicode chars should not be seen – use only a-z and A-Z.  The variable name also starts with an upper case letter but ought to be camel case.


In variable-land, everything is a value.  Be more specific.


39925 stands for something.  Use the real name of it.


No need to write the type of the object in the name of the variable.  Just use count.


“the” is spurious and used to be recommended for parameters back in the 1990s.  If you really need to have a different name for parameters, use argName, but 'name' on its own is generally used today.  It would be odd to find a method with both theName and name used at the same time, other than constructors and getters and setters.


The words like “of” and “to” are implied, and the word Horizontally is very long – a shorter synonym would suffice.  maxHorizontalThumbnails would have been fine.

Some variables that everybody loved.




It most likely contains the first invoice found.


Will contain true if the record is valid.


The grand total of all tax expenses will be in this variable.


Probably refers to a MobileUserContext type object.


Simple and concise.

Tip: to determine if you have selected the right member variable name, place the name of the class before it in your head.  For example, if the Customer class has attributes of surname and creationDate, they very nicely as “Customer surname” or “Customer creationDate”.  The result should be almost like reading English.

Hungarian notation (preceeding the variable name with something that indicates its type) mistakenly made its way into variable names during the 1990's thanks to the popular Windows development languages like Microsoft  Visual C.  This occasionally is seen in Java.

private String sDeviceId;
private int iWidth;
private Order refOrder;

Thankfully this technique has gone out of favor – it is unnecessary due to the strong type checking built into the Java language and associated Integrated Development Environments.  Also, if a developer does not know the type of a variable they are using, they probably should be sitting in front of a lecturer instead of a computer.

blog comments powered by Disqus