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.
Instance variables are for representing an object of a class, not a convenient way to pass information between functions.
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.
All functions should have a block comment above them that includes:
A description of the purpose of the function.
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.
- There should be only one variable declaration per line (unless multiple variables are related enough to warrant one comment for all).
- Declare variables at the beginning of the block of code in which they will be used.
Lines should not have more than one statement.
Use only one assignment operator per statement.
Functions should have only one return statement (if any) and it should be the last statement in the function.
Create flat programs, i.e. avoid nesting conditionals, functions, and loops multiple times.
Do not use break or continue statements.
Functions should only have one return statement, the last line in the function.
- Blank lines should be used to separate related sections of code and should be preceded by a comment explaining the section of code.
Functions, instance variables, and classes should be separated by blank lines.
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.
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.
Class names should be nouns and describe what the class represents.
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.
Do not use the string format operator, %.
Do not use list comprehension.
Do not use triple quoted strings.
Do not import modules other than those specified in assignment or activity.