Style and best practices that apply to all languages and frameworks.
- These are not to be blindly followed; strive to understand these and ask when in doubt.
- Don't duplicate the functionality of a built-in library.
- Don't swallow exceptions or "fail silently."
- Don't write code that guesses at future functionality.
- Exceptions should be exceptional.
- Keep the code simple.
- Avoid inline comments.
- Break long lines after 80 characters.
- Delete trailing whitespace.
- Don't misspell.
- Use empty lines around multi-line blocks.
- Use spaces around operators, except for unary operators, such as
!
. - Use Unix-style line endings
\n
. - Use uppercase for SQL key words and lowercase for SQL identifiers.
- Avoid abbreviations.
- Avoid object types in names (
user_array
,email_method
CalculatorClass
,ReportModule
). - Prefer naming classes after domain concepts rather than patterns they
implement (e.g.
Guest
vsNullUser
,CachedRequest
vsRequestDecorator
). - Name the enumeration parameter the singular of the collection.
- Name variables, methods, and classes to reveal intent.
- Treat acronyms as words in names (
XmlHttpRequest
notXMLHTTPRequest
), even if the acronym is the entire name (class Html
notclass HTML
).
- Total Page Load Time should take no more than 2 seconds, and never more than 3.
- Database queries during a web request should take no more than 200ms.
- Define methods alphabetically.
- Define class methods before instance methods.
- Always define the
initialize
method of a class at the top of instance method definitions.