Miško Hevery on Writing Testable Code
Miško Hevery has written a great summary of some basic coding rules for testability in his post Writing Testable Code.
I love this quote because every time I introduce unit testing to someone who has an existing code base you can see this in their eyes:
"I understand how to write tests for your code, but my code is different"
He goes on to list the rules most often broken by developers that make unit testing hard in his top ten list:
- Mixing object graph construction with application logic
- Ask for things, Don't look for things (aka Dependency Injection / Law of Demeter)
- Doing work in constructor
- Global State
- Singletons (global state in sheep's clothing)
- Static methods: (or living in a procedural world)
- Favor composition over inheritance
- Favor polymorphism over conditionals
- Mixing Service Objects with Value Objects
- Mixing of Concerns