Math can be coded very legibly
Many beginning coders scrunch up all the math in a line in order to save keystrokes:
var airResistance=coefficientOfDrag*0.5*airDensity*Math.pow(velocity,2);
And, by gosh, it does save space!
But it is also illegible and miserable to debug.
Good math code follows this simple rule:
Leave at least one space between each number, symbol, or variable name
So at a minimum, the code above becomes:
var airResistance
= coefficientOfDrag * 0.5 * airDensity * Math.pow( velocity, 2 );
But it is even easier to debug this:
var airResistance = coefficientOfDrag
* 0.5
* airDensity
* Math.pow( velocity, 2 );
Test your habits
Are the following expressions using good, legible programming style?
-
var area = length * width;
-
var circumference=Math.PI*radius*2;
-
var hitPoints = characterLevel*2;
-
var currentPositionX = oldPositionX + (3*speed);
-
var areaOfTrapezoid = ( lengthOfShortBase + lengthOfLongBase ) * ( height / 2 );
- How about this one?
var surfaceAreaOfRectangularPrism = 2 * ( height * width ) + 2 * ( height * length ) + 2 * ( width * length );
-
var V = Math.PI * Math.pow( r, 2 );