It is with great pleasure that I have been able to finally open-source the Guide to Writing Testable Code.
I am including the first page here for you, but do come and check it out in detail.
To keep our code at Google in the best possible shape we provided our software engineers with these constant reminders. Now, we are happy to share them with the world.
Many thanks to these folks for inspiration and hours of hard work getting this guide done:
- Jonathan Wolter
- Russ Ruffer
- Miško Hevery
Flaw #1: Constructor does Real Work
Warning Signs
- new keyword in a constructor or at field declaration
- Static method calls in a constructor or at field declaration
- Anything more than field assignment in constructors
- Object not fully initialized after the constructor finishes (watch out for initialize methods)
- Control flow (conditional or looping logic) in a constructor
- Code does complex object graph construction inside a constructor rather than using a factory or builder
- Adding or using an initialization block
Flaw #2: Digging into Collaborators
Warning Signs
- Objects are passed in but never used directly (only used to get access to other objects)
- Law of Demeter violation: method call chain walks an object graph with more than one dot (.)
- Suspicious names: context, environment, principal, container, or manager
Flaw #3: Brittle Global State & Singletons
Warning Signs
- Adding or using singletons
- Adding or using static fields or static methods
- Adding or using static initialization blocks
- Adding or using registries
- Adding or using service locators
Warning Signs
- Summing up what the class does includes the word “and”
- Class would be challenging for new team members to read and quickly “get it”
- Class has fields that are only used in some methods
- Class has static methods that only operate on parameters
9 responses so far ↓
Hello Misko,
Thanks for your work. However, I am a little bit confused on how to get futher than the content of http://misko.hevery.com/code-reviewers-guide/ who looks to me exactly the same than this page….
Best regards,
Franco
@Franco
Don’t the links work you? Each Flow is a link you can click on.
Sorry, yes they work. It is just that I didn’t get the “link text” convention and didn’t let my cursor above the Flaw titles ;o)
Thank you for this great guide.
It’s very helpful even though I’m not a Java developer / GUICE user. Your guidelines apply to many languages and I feel like my code has improved a lot thanks to you.
Good stuff. Thanks.
Great guide and a great blog. I subscribed to your RSS feed immediately after reading this article and I’m looking forward to reading more of your posts in the future.
Cheers!
Adam
One Unit Test should have Prevented Google from Categorizing the Entire Internet as Malware at JAW Speak // Jan 31, 2009 at 2:30 pm
[...] leadership and further promote Unit Testing, and even the opportunity for Test Driven Development. Miško, care to take that post [...]
In discussing the Constructor function doing as little as possible, you’re not including the Client are you? This is just for building the program classes, no?If you’re including the Client, where are requests generated? Their own methods with the Constructor just setting up the UIs with event handlers?Thanks,Bill
Test Driven Development // Nov 17, 2009 at 9:31 am
[...] excellent articles that changed the way I approach programming. When to use Dependency Injection, Guide to Writing Testable Code and Dependency Injection Myth: Reference Passing are some of the blog posts that I would definitely [...]
Leave a Comment