{"id":405,"date":"2015-10-05T15:50:05","date_gmt":"2015-10-05T15:50:05","guid":{"rendered":"http:\/\/mlapshin.com\/?p=405"},"modified":"2015-10-06T04:48:36","modified_gmt":"2015-10-06T04:48:36","slug":"writing-testable-code","status":"publish","type":"post","link":"https:\/\/mlapshin.com\/index.php\/2015\/10\/05\/writing-testable-code\/","title":{"rendered":"Writing Testable Code"},"content":{"rendered":"<p>My post is inspired by an\u00a0<a href=\"http:\/\/misko.hevery.com\/attachments\/Guide-Writing%20Testable%20Code.pdf\" target=\"_blank\">article<\/a>\u00a0of\u00a0Mi\u0161ko Hevery, who works for Google and coaches Googlers to maintain the high level of automated testing culture.\u00a0The article contains concentrated Mi\u0161ko&#8217;s experience and gives clear advice how to write testable code. I think, every software engineer should have it in the &#8220;must read&#8221; list.<\/p>\n<p>I read the article\u00a0a couple of years\u00a0ago and found its ideas very useful\u00a0in terms of personal growth as a developer. Of course, many\u00a0points were well-known to me,\u00a0however\u00a0I found several\u00a0tips\u00a0that\u00a0explained me in detail what is wrong with certain\u00a0code practices\u00a0that I just felt as a bad coding style, but could not verbalize it.<\/p>\n<p>In the <a href=\"https:\/\/mlapshin.com\/index.php\/projects\/pim\/\" target=\"_blank\">PIM project<\/a> we heavily use <a href=\"https:\/\/github.com\/google\/guice\/wiki\/Motivation\" target=\"_blank\">Guice<\/a>\u00a0&#8211; a Google framework that wires everything together using dependency injection. The article recommends Guice as the best tool to resolve dependency issues. So, recently I read it again having a good understanding\u00a0of Guice and found a couple of new points that I missed the first time. Now I agree &#8211; Guice forces programmers to write\u00a0a lot more testable\u00a0code.<\/p>\n<h2>Base Principles<\/h2>\n<p>The article considers four code\u00a0flaws that more or less exist in each project. Mi\u0161ko explains why each of them is a real flaw. How to detect and how to fix them. There a lot of clear examples of flawed code and how it could be fixed.<\/p>\n<p>All the fixes are based just on several fundamental principles:<\/p>\n<ul>\n<li>The Law of Demeter &#8211; loose coupling between components<\/li>\n<li>Declaring all dependencies in the API instead of\u00a0using hidden dependencies like global state<\/li>\n<li>Principle of single responsibility<\/li>\n<\/ul>\n<h2>Code flaws<\/h2>\n<h4>Flaw: Constructor does Real Work (something more than field assignment )<\/h4>\n<ul>\n<li>Drawbacks:\n<ul>\n<li>Hard to test<\/li>\n<li>Violates the Single Responsibility Principle<\/li>\n<\/ul>\n<\/li>\n<li>Solution:\n<ul>\n<li>Don\u2019t look for things,\u00a0ask for\u00a0them<\/li>\n<li>Do not create collaborators in your constructor<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>Flaw: Digging into Collaborators (using objects like &#8220;context&#8221;, &#8220;holder&#8221;, etc.)<\/h4>\n<ul>\n<li>Drawbacks:\n<ul>\n<li>Hidden dependencies<\/li>\n<li>Brittle code<\/li>\n<li>Hard to test<\/li>\n<\/ul>\n<\/li>\n<li>Solution:\n<ul>\n<li>Don\u2019t look for things, ask for them<\/li>\n<li>Only talk to your immediate friends<\/li>\n<li>Ask for the most specific object you really need<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>Flaw: Brittle Global State &amp; Singletons<\/h4>\n<ul>\n<li>Drawbacks:\n<ul>\n<li>Unnecessary coupling<\/li>\n<li>Spooky Action at a Distance<\/li>\n<li>Brittle Applications (and Tests)<\/li>\n<li>Dishonest API<\/li>\n<\/ul>\n<\/li>\n<li>Solution:\n<ul>\n<li>Dependency Injection is your Friend<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>Flaw: Class Does Too Much<\/h4>\n<ul>\n<li>Drawbacks:\n<ul>\n<li>Hard to debug<\/li>\n<li>Hard to\u00a0test<\/li>\n<li>Hard to understand<\/li>\n<li>Non-extensible system<\/li>\n<\/ul>\n<\/li>\n<li>Solution:\n<ul>\n<li>Split large class into several ones<\/li>\n<li>Add\u00a0new functionality to a large class as a new class<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>I hope after reading the article you will easily avoid all the flaws and make your life happier.<\/p>\n<p>Bye!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My post is inspired by an\u00a0article\u00a0of\u00a0Mi\u0161ko Hevery, who works for Google and coaches Googlers to maintain the high level of automated testing culture.\u00a0The article contains concentrated Mi\u0161ko&#8217;s experience and gives clear advice how to write testable code. I think, every&hellip; <br \/><a class=\"read-more-button\" href=\"https:\/\/mlapshin.com\/index.php\/2015\/10\/05\/writing-testable-code\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":413,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,16],"tags":[20,19,18,17],"class_list":["post-405","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-reading","tag-guice","tag-java","tag-programming","tag-reading"],"_links":{"self":[{"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/posts\/405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/comments?post=405"}],"version-history":[{"count":12,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/posts\/405\/revisions"}],"predecessor-version":[{"id":419,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/posts\/405\/revisions\/419"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/media\/413"}],"wp:attachment":[{"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/media?parent=405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/categories?post=405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/tags?post=405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}