Strive to make your code easy to read, because code that is easy to read is easy to maintain. Use a lot of white space where it will help to make things clear. As a rule, I treat objects and arrays as a single thing, and I don't put space before the references (e.g. myObject.SomeMethod()
).
Unary operators are associated with their operand, so I don't put a space between them. On the other hand, I do put a space on the side away from the operand. When it comes to binary operators, I put a space both sides. Here are some other suggestions:
Don't use spaces to indicate precedence. For that matter, don't depend on operator precedence, use parentheses.
( 4+ 3*2
) // don't do this
( 4 + ( 3 * 2 ) ) // much better
Put a space after commas and semicolons, not before.
Parentheses should have spaces on the inside, and keywords should be set off with a space:
if ( a == b )