
Clean Code
As technical debt grows and the cost of change grows, the responsiveness to change decreases which in turn may increase technical debt further.
A team may try to avoid technical debt by developing as cleanly as possible.
A simple method to write clean code is:
Link to original
- Write something that works
- Refactor your code
Naming Things
Naming Things (Clean Code)
Link to original
- Use intention revealing names, it is obvious what the variable is used for
- Avoid disinformation, for example
Account[] accountList;(not a list) It’s better to useAccount[] accountArray;or betterAccount[] accounts;- Use British English everywhere
- Make meaningful distinctions between variables
- Do not include “noise” words such as “variable”, “the”, “data”.
- Use searchable names
- Avoid “magic values”; hard-coded constant values
Comments
Comments (Clean Code)
- When possible, explain yourself in code not comments (meaningful names)
- Useful for documentation generation
- Required for legal comments
- May be useful to explain intent (i.e. when using regex)
- Warnings are useful
- To-do comments should not be used as an excuse for bad code
Bad comments include:
Link to original
- Noise: no meaningful information
- Redundant information
- Commented out code
- Too much or wrong type of information
Clean Functions
Functions (Clean Code)
Functions should do one thing, do it well, do it only
Link to original
- One level of abstraction per function
- Functions must not have side effects
- Functions should have as few arguments as possible
- Avoid flag arguments, i.e. “boolean isModerator” Instead define two operations if possible.
- DRY Code
- Stepdown Rule
- Command Query Separation Principle