Heuristics
Object Creation
- Inject dependencies and configuration values as constructor arguments.
- Inject only what you need, not where you can get it from.
- All constructor arguments are required.
- Only use constructor injection.
- There’s no such thing as an optional dependency.
- No
MyDependency = null
anywhere
- Make all dependencies explicit
- Turn static dependencies into object dependencies
- Turn complicated functions into object dependencies
- Make system calls explicit
new DateTime()
is a system call. When used, it will ask for the information from its environment.
- Data relevant for a specific task should be passed as method arguments instead of constructor arguments.
- Don’t let object behavior change after instantiation.