Martin Golding
Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live.
The aim of a coding standard is to make the entire codebase look like it was written by one person.
Naming
- Use nouns for classes/variables and verbs for methods/functions
- Distinguish clearly between single valued and multi-valued functions
- Use standard words (avoid foreign language, slang, names only meaningful given specific contexts)
- Name should explain the method/class
- The order of names should be sensible
- Names should not be too long or too short
- Avoid misleading names - name related things similarly
- Avoid ambiguities (such as 0 vs O)
- Avoid hard to pronounce
- Avoid ambiguous meanings (right as in correct, or the direction)
- Avoid similarly pronounced names
Readability
- Avoid long methods
- consider shortening when a method goes beyond 30LoC
- Avoid deep nesting
- Avoid complicated expressions
- Avoid magic literals
- use constants to give a name to the numbers being used
- Make code obvious/explicit
- use explicit type conversion
- use parantheses/braces to show groupings
- use enumerations when variables can take only a small number of finite values
- Structure code logically
- Do not ‘trip up’ reader
- unused parameters in method signature
- similar things that look different
- different things that look similar
- multiple statements in same line
- data flow anomalies
- Practice KISS (“keep it simple, stupid)
- Avoid premature optimisations
- Avoid multiple levels of abstraction within a code fragment
- Make happy path prominent
- Restructure code to make intended execution path as less-nested as possible
Unsafe Practices
Use language constructs in the way they are meant to be used.
- Use
default
branch inswitch
statements. - Don’t recycle variables/properties
- Avoid empty
catch
blocks - Delete dead code
- Minimise scope of variables
- Minimise code duplicatioon
Comments
- Do not repeat the obvious
- Write to the reader (target other programmers, reading the code)
- Explain what and why, not how