Always choose self-describing variable names
Don't you hate it when people don't say what they mean?
Sometimes this happens when people think you understand, but they have not given you enough information to get the full picture.
The same thing happens in programming when you don't name things thoughtfully.
Use camelCase for variables
In most languages, you cannot have a space in a variable name.
In order to still have descriptive names without spaces, you have to use camelCase. camelCase has all the words grouped together, and has the first word lowercase, and the first letter of all remaining words capitalized.
- Bad:
var MyPonys_Name = 'Herbert';
- Good:
var myPonysName = 'Herbert';
- Good:
var currentPlayersTurnNumber = 13;
Use ALL_CAPS for constants
An exception to using camelCase is when you are defining a constant.
A constant is a variable that never changes. It holds data that is used, but never modified. Traditionally, constants are spelled in ALL_CAPS with underscores where the spaces would go.
It is also a good idea to use const
instead of var
if you know that the value is never going to change.
-
Bad:
const piValue = 3.141592654;
-
Good:
const PI_VALUE = 3.141592654;
-
Good:
const ACCELERATION_DUE_TO_GRAVITY = 9.8;
Be descriptive
Although you will type less by using short, difficult to understand variable names, You will lose that time later when you are trying to figure out what each variable does.
- Bad:
var x = 1;
- Good:
var xPosition = 1;
-
Good:
var animationSpeed = 10;
Use abbreviations sparingly
There are times when abbreviations are OK, such as when they are already used commonly in writing (texting shorthand does not count).
In general, if you are making up your own abbreviations, they are probably going to be confusing to other people.
-
Bad:
var txWt = 'bold';
-
Good:
var textWeight = 'bold';
-
Good:
var newBackgroundColour = 'green';
There are some commonly used abbreviations in programming, however:
var numApples = "number of apples";
var xPos = "position along the x-axis";
-
for (var i = 1; i < 5; i++) {}
// i is traditionally used as an index variable inside loops
Most variable names should be nouns
It is generally a good idea to avoid using verbs, adjectives, adverbs, etc. for variable names. If your variable name is not a noun, you are probably not communicating well.
-
Bad:
var charHowQuickly = 42;
-
Good:
var characterSpeed = 42;
-
Good:
var terrainType = 'desert';
BUT! Use questions for boolean (true || false) variables
Boolean variables are a lot like a true or false answer to a question. In fact, it often makes sense to use a question for a boolean variable name.
-
Bad:
var magicRingTrueOrFalse = true;
-
Good:
var hasAMagicRing = true;
-
Good:
var usesUselessVariableNames = false;
Include the variable type for objects and arrays
Most variables are straight-forward number or string variables. Programmers assume this when they are reading code.
If your variable is for a more complex data structure, then hint at that data structure in its name.
-
Bad:
var characterSheet = {};
// ... is a badly named object -
Good:
var characterSheetObj = {};
// ... is a well-named object -
Good:
var vehicleProperties = {};
// ... is a well-named object
-
Bad:
var friends = [];
// ...is a badly named array -
Good:
var friendList = [];
// ...is a well-named array -
Good:
var labResultsArr = [];
// ...is a well-named array
-
Bad:
var maze = [[]];
// ...is a badly named two-dimensional array -
Good:
var mazeGrid = [[]];
// ...is a well-named two-d array -
Good:
var tileTypeByHeightAndWidth = [[]];
// ...is a well-named 2-D array