John Fitzpatrick
Engineer of the Future
How to untangle code
- Remove language-specific tricks
- For C++, replace preprocessor directives with C++ constructs
- For C++, use STL containers
- Isolate business logic
- Separate input and output from calculation
- Make classes immutable
- Favor composition over inheritance
- Split classes into smaller classes
- Split classes into multiple child classes to eliminate 'if' and 'case' statements
- Remove input and output from constructor and destructor
- Create small classes to perform low-level functions
- Re-factor to remove forward class references
- Identify and reduce dependencies between classes
- Use interfaces to eliminate cyclic dependencies
- Split functions into smaller functions
- Favor const functions that return a value
- Make functions static, then move them to appropriate class
- Make functions void or const
- Functions contain at most one loop
- Iterate over collections instead of loops
- Move 'for' loops down to worker classes
- Limit loops to one purpose
- Push loops down as far as possible
- Make variables private
- Limit variables to one purpose