General
Programming Practices
Avoid magic numbers (literal primitive numbers) and magic strings by using variables with meaningful names.
Write functions that are small and do one thing.
Write test cases before writing any code.
Formatting
All lines should be less than 80 characters (the default width of an Emacs window). The best way to ensure that all lines of code are less than 80 characters is to write simple statements that do only one thing. When a line must span multiple lines:
break after commas.
break before operators.
prefer higher level breaks.
Functions should be 40 lines or less, including comments.
Indent with spaces (the default behavior for Emacs), not with tabs.
Documentation
- Files should have a block comment at the top with the author
All functions should have a block comment above them that includes:
- The pre and post conditions of the function. If the pre conditions are met before execution, then the post conditions are guaranteed to be true on completion. The precondition must document every parameter and any instance variables used in the function. The postcondition should document all variables that may change during execution and the return value.
Statements
Lines should not have more than one statement.
Use only one assignment operator per statement.
- Create flat programs, i.e. avoid nesting conditionals, functions, and loops multiple times.
White Space
- Blank lines should be used to separate related sections of code and should be preceded by a comment explaining the section of code.
Binary operators (+, -, *, etc.) should be separated from their operands by spaces (except for the . operator).
Unary operators (-, ++, etc.) should not be separated from their operand by a space.
Argument lists should have spaces after commas.
Naming Conventions
Names for variables, functions, and classes should be meaningful. Common abbreviations are OK (avg, max, etc.) but single character names and acronyms should be avoided.
Function names should start with verbs and describe what the function does.
Python
Programming Practices
Default parameters and named arguments break the spaces around binary operator rule.
Do not use from module_name import . Instead replace the with what specifically to import from the module.