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:



  1. Mixing object graph construction with application logic

  2. Ask for things, Don't look for things (aka Dependency Injection / Law of Demeter)
  3. Doing work in constructor

  4. Global State

  5. Singletons (global state in sheep's clothing)

  6. Static methods: (or living in a procedural world)

  7. Favor composition over inheritance

  8. Favor polymorphism over conditionals

  9. Mixing Service Objects with Value Objects

  10. Mixing of Concerns