<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Maciej Falski on Medium]]></title>
        <description><![CDATA[Stories by Maciej Falski on Medium]]></description>
        <link>https://medium.com/@maciej.falski?source=rss-8f238a28c8f5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Fu5srefH_v5Bxh-HXG6b1w.png</url>
            <title>Stories by Maciej Falski on Medium</title>
            <link>https://medium.com/@maciej.falski?source=rss-8f238a28c8f5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 19:31:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maciej.falski/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Monorepo Learnings]]></title>
            <link>https://blog.devgenius.io/monorepo-learnings-dbd2a83d5279?source=rss-8f238a28c8f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbd2a83d5279</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[monorepo]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Maciej Falski]]></dc:creator>
            <pubDate>Mon, 22 Jun 2020 13:05:47 GMT</pubDate>
            <atom:updated>2020-06-22T16:41:01.360Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Lessons learnt from working with the monorepo-style codebase</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*r5VzKS7gQibR_qodANsZrg.jpeg" /></figure><p>I was always intrigued by a <strong>monorepo</strong> as a way to organise the codebase. Famously, a bunch of internet giants like Google, Facebook, Twitter, or Dropbox store their whole code in a single repository. If they do it, it must be the way to go, right? Six months ago I landed a gig where the majority of the company’s code lived in a single repository, and I had a chance to experience it myself.</p><h3>What is monorepo?</h3><p>Monorepo (or monolithic repository) is a way to store the source code of two or more independent, or loosely coupled, applications or services in a single code tree. It’s an alternative to the common multi-repo approach, where each project lives in a separate code repository.</p><p>Monorepo is a widely discussed, and somehow controversial, subject. It has strong proponents and fierce enemies. It took me a while to decide if I like it or hate it. Thanks to my inclinations to <strong>DevOps</strong>, I spent a bit of time working on monorepo tooling. It helped me to crystalise my understanding and make a few observations.</p><h3>Why monorepo?</h3><p>Having all the source code in a single location brings a bunch of potential benefits:</p><p><strong>Code sharing and collaboration</strong></p><p>Everyone sees everything, and one team can contribute to another team’s code. It removes artificial boundaries between teams and projects.</p><p><strong>Large scale refactoring</strong></p><p>Having all the code in one place makes gradual refactoring and reorganisation of the code simpler. There are no hidden dependencies, and once refactoring is done, the system continues to function. Also, multiple projects can be changed atomically.</p><p><strong>First-party dependency management</strong></p><p>Changes to base libraries and shared code are instantly propagated through the dependency chain. No need to fork the code or support a multitude of versions.</p><p><strong>Third-party dependency management</strong></p><p>When you need to fix an issue with a library, ie. a security vulnerability, it’s easy to find all of its occurrences and make a lockstep upgrade. Moreover, monorepos naturally lead people to minimise the number of versions of third party dependencies towards one.</p><p><strong>Uniform code access</strong></p><p>Linters, CVE scanners, and other source code reporting tools can be applied once and for all.</p><p><strong>Shared testing strategies</strong></p><p>It’s easier to test multiple components together as there’s no need to pull source code or artefacts from different repositories.</p><h3>Monorepo gotchas</h3><p>The benefits don’t come for free. There are trade-offs, there are risks.</p><h3>Monorepo does not imply a mono-pipeline</h3><p>A lock-step release is a case when you have to deploy two or more services together to make sure the system keeps functioning, ie. a service is making a backwards-incompatible change to a contract, so it forces all its consumers to update accordingly.</p><p>In a multi-repo world, a service tends to have its own CI/CD pipeline. By design, it forces you to think about the build artefacts as independent deliverables, as each one can have a unique release cadence. If there are consumers of the API, the new version needs to be mindful of backward compatibility (ie. by using the <a href="https://martinfowler.com/bliki/ParallelChange.html"><strong>expand-contract pattern</strong></a>).</p><p>In a monorepo, having all the sources in a single tree, there is a temptation to build and deploy all the services at once. However, just because things change in tandem, that does not mean that they are all the same thing. Hence, if a monorepo is tied to a single build pipeline, it can quickly become a point of contention. The build times are increasing, and issues introduced by one team block other teams. Everyone ends up waiting for a green build, which in consequence generates lots of frustration.</p><p>This should be avoided. The teams working on a monorepo should be free to decide the release cadence of their projects, and should own their delivery pipeline. It’s the team’s responsibility to define, and own deployment boundaries and deliver the software independently. Monorepo model is not an excuse for not making backwards-compatible API changes.</p><blockquote>“Google has a single Repo for 25K developers. In that, there are many hundreds of separately buildable/deployable things. Each of those has its own release cadence. Some go into prod with every commit (Continuous Deployment style). Others have daily, weekly or monthly cadences.” — <a href="https://paulhammant.com/2017/03/25/repositories-vs-release-cadences/">Paul Hammant</a></blockquote><h3>Politics, Ownership and Trunk Based Development</h3><p>Monorepo style can only work with an engineering culture of transparency and collaboration. Everyone can see, or even change, everything. It won’t fit in a highly politicised environment. You can’t hide things. You have to regularly collaborate on shared code and library upgrades. Teams need to be ready to embrace <a href="http://wiki.c2.com/?CollectiveCodeOwnership"><strong>collective code ownership</strong></a> fully or at least to a certain extent. If autonomy is the case, you can encourage teams to treat their projects within the repo as their mini-repos. GitHub-style <em>CODEOWNERS</em> file, which defines users or groups that own certain files or paths in a repository, can help with it.</p><p>The monorepo model forces teams to work in <a href="https://trunkbaseddevelopment.com/"><strong>Trunk-Based Development</strong></a> style. There are no release branches, software gets built and deployed from the trunk. Any complex branching strategies won’t work here.</p><h3>Tooling</h3><p>The bigger the monorepo gets, the harder it is to work with. Monorepo of 5 services doesn’t need any special tooling. Monorepo of 500 services requires proper tools investment. This can be costly. Google, Twitter, or Dropbox have heavily invested in scalability and productivity tooling to support this model. Not every company is Google, however, and can throw significant sums into making it work flawlessly. On the other hand, the big companies keep open-sourcing their efforts or contributing to the existing free tools, so everyone can leverage it. Don’t forget to do research before committing to the model, and keep rolling existing projects gradually into the monorepo, solving scalability problems as they arise.</p><p>Common tools and techniques worth keeping in mind are:</p><ul><li>Controlling size code checkouts by expanding/contracting (ie. with git sparse checkouts) <a href="https://trunkbaseddevelopment.com/expanding-contracting-monorepos/">https://trunkbaseddevelopment.com/expanding-contracting-monorepos</a></li><li>Improving git performance <a href="https://dropbox.tech/application/speeding-up-a-git-monorepo-at-dropbox-with--200-lines-of-code">https://dropbox.tech/application/speeding-up-a-git-monorepo-at-dropbox-with--200-lines-of-code</a></li><li>Use of monorepo-specific build tools, like <a href="https://bazel.build/">Bazel</a>, <a href="https://buck.build/">Buck</a>, or <a href="https://www.pantsbuild.org/">Pants</a></li></ul><h3>Sharing too much</h3><p>It’s easier to define boundaries in a multi-repo approach. You tend to be more thoughtful as the change is more painful. On the other hand, there is the risk that you don’t do the right thing because of the amount of overhead.</p><p>In a monorepo model, however, there is a risk of oversharing. Common libraries are prone to become a <em>dumping ground</em> for code. It often results in poor code health, and increasing costs of building (every little change in shared code will trigger a build for all dependent projects). Monolithic codebase in no way implies monolithic software design. The incentive for stable, well-thought APIs and compact single-purpose libraries needs to remain. Investment in code linters, dependency analysis tools, as well as engineering craftsmanship is essential.</p><h3>Summary</h3><p>Although simplified refactoring and easy shared code management are great reasons to be lured into the concept of monorepo, it is not a silver bullet. It brings many benefits, but it can also quickly become a loathed antipattern if it reduces the ability of teams to move fast and independently. Investment in tools, as well as a healthy DevOps culture, is a must.</p><p>Thanks to David Denton, Matt McDonald, and Yufei Xie for comments, corrections, and discussion.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbd2a83d5279" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/monorepo-learnings-dbd2a83d5279">Monorepo Learnings</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Three Questions]]></title>
            <link>https://blog.devgenius.io/three-questions-75ba0568a44b?source=rss-8f238a28c8f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/75ba0568a44b</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[agile]]></category>
            <dc:creator><![CDATA[Maciej Falski]]></dc:creator>
            <pubDate>Thu, 18 Jun 2020 13:05:46 GMT</pubDate>
            <atom:updated>2020-06-19T11:27:52.711Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2uKX7FQ9kZRHAWFyctgFeg.jpeg" /></figure><p>Being a programmer in the modern age is not only about technical mastery and craftsmanship. It’s about making decisions and good choices. It’s about doing the right thing.</p><p>On one hand, it’s very easy to fall into a trap of procrastination. The world around us provides a multitude of stimuli which do not help with focus. I’m as easily distracted as a toddler on a walk outside. I’ve learnt that with a bit of discipline (phone in a flight mode, turning off Slack), and setting a Pomodoro timer on, I can relatively easily enter a deep and focused work state.</p><p>Having the focus sorted, how can I make sure that I’m barking up the right tree? How to guarantee my energy is spent right? This is where I like to ask myself <strong>Three Questions</strong>. It’s a little framework I follow in my daily developer’s life (and beyond). The questions define what I focus on, and help me decide the activities I’ll engage in. I like how they drive my day, my attention, my work.</p><h3>Question 1: What’s the most important thing to do?</h3><blockquote>“The future is uncertain and you will never know less than you know right now.” ― Sandi Metz</blockquote><p>Perhaps you’ve just made something good enough and wondering if you should make it even better. Perhaps it’s time to start something completely new. This is when I want to know what’s the most important thing to do next. What stands up the most? Is it the accumulating technical debt, or the business feature which will help to validate a potentially profitable idea? Or maybe there is a new business opportunity arising?</p><p>It’s time to step back and see the big picture. Why not go for a walk and let the brain turn into a diffused mode? Don’t just mindlessly grab the top task in the <em>“Next”</em> column. This might be a good start, but this is just the beginning of the story. Ask questions. Investigate, research. It might just take 5 minutes, but it will influence the next hours, days, or even weeks of your work life. It’s tempting to settle on what’s familiar. It’s easier to defer hard decisions and end up polishing what you’ve got without producing any value.</p><blockquote>“If it’s your job to eat a frog, it’s best to do it first thing in the morning. And if it’s your job to eat two frogs, it’s best to eat the biggest one first.” ― Mark Twain</blockquote><p>You’ve identified what to do next, the next big thing, the next problem to solve. Now, it’s time to ask another question.</p><h3>Question 2: How can I find out more about it?</h3><blockquote>“The secret to doing good research is always to be a little underemployed. You waste years by not being able to waste hours.” ― Michael Lewis</blockquote><p>It’s time to immerse yourself with a task, or a problem. You won’t find a solution without understanding it. Study it thoroughly. Reading and researching the subject help. Talking to someone is even better. Go and pull all the possible strings. Distil knowledge, bounce ideas, criticise. Get into the context.</p><p>Once you acquire a critical mass of understanding you’ll be able to decide what the next steps are. Your options are:</p><ul><li><strong>Abandon it<br></strong>This might be the best outcome. Deciding that something is not needed saves you needless work. Lessons should be learnt early. There’s nothing less demotivating than the realisation that you’re doing the wrong thing. We often make up tasks as we don’t understand the underlying problem. Failing fast is failing smart. Great, let’s find another tree to bark up and come back to <em>Question 1</em> to figure out what’s the most important thing now.</li><li><strong>Break it down<br></strong>You’ve started digging and you don’t see the end of it. It’s too big to take on in one go. Fine, identify a few first tasks, leave the rest fuzzy (you’ll clarify more when the time comes). Run it through someone. Pick the first one. But wait. Is it still the most important thing, considering the scope? You might want to reconsider your options. If it still feels like a top priority, go ahead, it’s time to start coding.</li><li><strong>Start coding<br></strong>This is the last resort. So far you’ve done everything to avoid this but can’t ignore it anymore. The best programmers I know are the ones who will do anything not to write code. It’s good when thinking comes first. In the end, the code is not an asset, it’s a liability. Ok, time to get your toys lined up, let’s code. It’s the moment when you should ask yourself the next question…</li></ul><blockquote>“The hard part of programming is the thinking, not the typing” — Michael (GeePaw) Hill</blockquote><h3>Question 3: Where is my failing test?</h3><p>By now you know, more or less, what you need to do. It’s tempting to jump into the solution. You know how to bang it in. But do you? This is where I pull one of my favourite tools: <strong>Test-Driven Development</strong>. I write a test, a failing test initially.</p><p>By writing a test first you set your intention. While setting your outcomes you clarify your thinking. It helps you to know what exactly needs to be done. It’s about quality, but furthermost, it’s about the accuracy of the solution. Why would you engineer anything generic and complex? You want to solve a specific problem, with the simplest solution possible.</p><blockquote>“Perfection is reached not when there is nothing left to add, but when there is nothing left to take away” — Antoine de Saint Exupéry</blockquote><p>Thinking about a test sets you off for exploration. It helps you understand the domain better. It also brings confidence in the solution. Write a failing test, make it pass, refactor. Simple, and easy to follow, Test-Driven Development cycle. You do it once, you repeat it. You’re moving, you’re making progress. Dopamine keeps dripping.</p><p>There is a chance that in due course you get enlightened and, by doing, you attain another level of understanding. Perhaps the problem is way different from what you originally anticipated? Perhaps it’s not even a problem? Perhaps you don’t even need to code?</p><p>The three questions define my day-to-day rhythm. They drive my day, giving me peace of mind as I know that a pragmatic enquiry helps with the decision process. Asking them routinely in conjunction with pairing on a problem, brings even more benefits. I’m less prone to end up in a rabbit hole. I’m more confident that I am working on the right thing. And if at any point I realise I’m not, I know what the next question to ask is.</p><blockquote>“I’m not a great programmer; I’m just a good programmer with great habits.” — Kent Beck</blockquote><p>Thanks to Kaya Regula, and Matt McDonald for comments, corrections, and discussion.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75ba0568a44b" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/three-questions-75ba0568a44b">Three Questions</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Detroit and London Schools of Test-Driven Development]]></title>
            <link>https://blog.devgenius.io/detroit-and-london-schools-of-test-driven-development-3d2f8dca71e5?source=rss-8f238a28c8f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d2f8dca71e5</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[mock-objects]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <dc:creator><![CDATA[Maciej Falski]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 16:12:32 GMT</pubDate>
            <atom:updated>2020-06-18T15:40:25.157Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p><strong>Test-Driven Development (TDD)</strong> has become one of the core tools in the toolbox of a modern developer. Its practice is often considered as a manifestation of professionalism in software.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*QnnqbhOh_VaNpJXsNyURfg.jpeg" /><figcaption>Test-Driven Development cycle</figcaption></figure><p>I’ve been doing TDD for over a decade. Every project I worked on acquired a slightly different approach. I can easily name specific techniques, like state or interaction testing, test doubles, mocking, growing application bottom-up, or broad-brush design. But there was always something missing, a common term which would group these sometimes contradictory techniques, and put clarity on benefits or risks associated with a given technique. One conversation (hi David!) has changed it all. This is how I’ve learnt about <strong>Detroit and London Schools (or styles) of TDD</strong>, and decided to dive deeper to discover more. A practice of TDD is a straightforward technique, although the devil is in the details.</p><p>In this post, I will outline the main differences between the two schools of Test-Driven Development, look into specific techniques forming the practice, analyse how they approach problem-solving, and suggest how to mitigate risks arising from using them. I aim to stay unopinionated, hoping it’s not a futile endeavour (who has no strong opinions?). Feel free to challenge me, and show where I’m wrong.</p><h3>A bit of history or “location, location, location”</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*pNvNrtEonSjOa-EObUIGHg.jpeg" /></figure><p>Test-Driven Development originated in the late nineties in Detroit, out of the ashes of the Chrysler Comprehensive Compensation System. Kent Beck, the lead engineer on the project, codified the technique and outlined the practice in <strong><em>Test-Driven Development By Example</em></strong> book. TDD became an essential best practice in <strong>Extreme Programming (XP)</strong> methodology, which built the foundations of the emerging Agile movement. Specifics of the approach promoted by Beck became recognised as <strong>the Detroit, or the Classicist, School of TDD</strong>. Prominent representatives are also Robert C. Martin (Uncle Bob), Ron Jeffries, and Martin Fowler.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*OtGEdiEE2CmwF_4MTD0yvw.jpeg" /></figure><p>Test-Driven Development quickly gained popularity. London for centuries provided fertile soil for thought-provoking ideas. In the early 2000s, London’s Extreme Tuesday Club (XTC) was formed. It attracted the avant-garde of UK’s TDD/XP practitioners and provided a great community to share experiences, techniques, and ideas. It helped Steve Freeman and Nat Pryce to distil their vision of TDD which resulted in publishing <strong><em>Growing Object-Oriented Software Guided By Test</em></strong>. Next to Kent Beck’s book, it quickly became the second most important publication on Test-Driven Development. The style promoted in the book is known as <strong>the London School of TDD</strong>, sometimes labelled as <strong>the Mockist</strong>. It codifies and clarifies the technique of mock objects, not widely known before.</p><h3>Two paths to enlightenment</h3><p>Both the Detroit and the London schools praise the benefits of TDD practice: steady incremental progress, elimination of fear, simplicity of design and quality of code. Nevertheless, they provide distinct guidelines on reaching TDD nirvana. I’ve grouped them into three areas:</p><ul><li>definition of the unit</li><li>design and the test writing practice</li><li>use of test doubles</li></ul><p>Let’s dive in to see how different they are.</p><h3>The unit</h3><p>Unit testing is a signature activity of TDD. What is the unit in unit testing?</p><p>In the Classicist world, <strong>the unit is a module</strong>. A module is a single class, just a function, or a set of closely related classes, which implement a particular functionality. It doesn’t matter how small and simple, or complex and full of inside collaborations it is. A module’s functionality is exposed by public exports (API). Hence, in the Detroit style, you write a test against the module.</p><p>In the London school, on the other hand, a unit test is often written with a single class in mind, hence<strong> the unit tends to be a class</strong>. If a class has collaborators they are mocked (the Mockist style, remember?), making dependent application layers detached. Use of mock objects enable this approach, and this is how tests likely to be written. On the other hand, nothing stops you from embracing the unit as a module if you think it’s more beneficial.</p><p>In the Detroit style, the <strong>System Under Test (SUT)</strong> grows with the application, making the unit test to operate on an expanding context. In the London style, the use of mocks in place of real collaborators makes the test setup small and the context limited.</p><h3>Emerging design and the practice of test writing</h3><p>Application design is a complex subject. Traditionally teams approach it with a set of non-coding activities, like in-depth analysis, UML sketching, or extensive design sessions. TDD approach is almost the opposite. It is a design technique by itself. It lets the tests dictate the design of the system. It doesn’t mean that the architecture only comes from tests. The good architecture is designed over time, through iterations. It takes a while before you’ll finally settle with a good one. The way the schools of TDD approach the design is distinct and is expressed in the way you write your tests as well.</p><p>In the Detroit sense, I like to see it as digging deep. Focus on an exploration of the core logic with all its complexities from the very first moment. Don’t shy away from diving straight into it. If you need a collaborator, go and implement it, in the simplest possible way to get your test passing. Work on the vertical slices. Discover what lies behind and integrate early. Dig into the heart of the onion. Don’t worry about the breadth, it will come in time. Observe how the design emerges.</p><p>In the London style, your exploration starts by gaining a wider view first. Do a broad-brush design. Draw a few things first, not too many, just enough to get a mental model of the system. Explore API entry points, identify roles, responsibilities and key interactions between collaborators. Don’t dig deep, that’s not the point. Start from the top and work your way inwards. Focus on writing a test for the first layer. Mock your collaborators, committing to fleshing out their behaviour later. Once ready to move deeper, identify the next layer and repeat the process. Explore the onion’s layers, one by one. Eventually, you’ll get to the heart.</p><figure><img alt="The onion" src="https://cdn-images-1.medium.com/max/205/1*7dIUtkC0su4CPgtjaU2pzw.jpeg" /></figure><p>The Detroit style is an<strong> inside-out </strong>design process, while the London, <strong>outside-in</strong>. Can they influence the final design? There’s no evidence for it. The way the system is tested shapes its design in a short time. The more complete it gets the testing style’s influence fades. On the other hand, one could argue that <em>digging deep</em><strong><em> </em></strong>vs <em>working in layers</em> results in a distinctly laid down system. This only could happen if the feature cycle is long. However, if the cycle is short and keeps producing small, but complete deliveries, I can’t see a big divergence in the design. Also, there’s much more to design than just the way you write your tests. More on this soon.</p><h3>Test doubles</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*VWyDE-VZ-LCYvtRXnsXh8A.png" /></figure><p>The Detroit school argues: <em>if an object is hard to test, then it’s hard to use</em>. The London school goes a step further saying: <em>if a dependency is hard to mock, then it’s hard to use</em>. Both complement each other, setting some distinctions too.</p><p>While focusing on vertical slices, the Detroit style is not particularly concerned with the collaborations. Collaborations, as long as they are internal, are embraced and fully initialised in the testing harness. Test fixtures produce an almost complete system. This results in <strong>no need for the extensive use of mocks</strong> and it enables <strong>state verification</strong>. In effect, as unit tests expand over the full system, they become mini-acceptance tests. Although it does not mean you won’t need test doubles, ie mocks, if a collaborator gets awkward.</p><p>In the London approach, where the layer discovery takes precedence, mocks become bread and butter of the style. In the world where a unit is a class, it’s <strong>more convenient to mock collaborators</strong>. You draw only parts of interaction which are required for a specific test to pass. While it’s sensible to assert on the state, often you rely on <strong>verifying interactions</strong>. You want to make sure the code makes all relevant calls to its collaborators to achieve the desired goal, the results may manifest only down the line.</p><p>The Detroit style makes only a minimal need for end-to-end tests. Usually, you will need just a few to demonstrate the correct wiring of the integrated system. In the London style, if you end up with heavily mocked tests, the need for end-to-end tests is greater. They will provide much-needed confidence in the system, which can’t be guaranteed otherwise.</p><p>It’s worth mentioning that <strong><em>mock objects</em></strong> is a widely misunderstood technique and its use stirs the most heated debates. Mocks are not about ring-fencing code dependencies. It’s a tool in the process of test-driving system design. They help in delaying decisions you don’t want to take now. One way or another, below points are equally fair and don’t contradict each other.</p><blockquote>“Programmer tests should be sensitive to behaviour changes and insensitive to structural changes. (…) If I care about the order of operations, I’ve designed the system wrong” — Kent Beck (the Detroit School)</blockquote><blockquote>“If I have an object that has behaviour and I’m doing “Tell, Don’t Ask” then I can only test interactions.” — Steve Freeman (the London School)</blockquote><h3>You’re Doing It Wrong!</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/269/1*hHAkG1_09X79FocLUHdl6Q.jpeg" /></figure><p>Any tool or technique, no matter how smart, can be misused. It’s very easy to see everything as a nail if all you have is a hammer. The critique arises quickly. Instead let’s have a look on a few anti-patterns associated with styles and how to mitigate them. As it’s not about the technique but the fact that <strong><em>you’re doing it wrong!</em></strong></p><h3>Detroit: My fixtures are too complex</h3><p><em>Having so much under test you get to copy fixture code into each test case. Your test examples are getting out of hands. Your tests become inflated, delicate, hard to read. Each little production code tweak forces you to change many tests. </em><strong><em>You’re doing it wrong!</em></strong></p><ul><li>It’s time to spend some time with your test code. Introduce common fixtures (factories, builders, object mothers) and reuse them across your tests. They should be expressed in the domain language and read well. You want them to tell you a story about how the system sticks together. Maybe you can not only simplify the test setup but the system itself?</li><li>If it’s still too complex, perhaps your module is trying to do too much. Is there a problem with the design? Bring another pair of eyes and review it again.</li><li>The quality of the test code is as important as the quality of the production code. It’s your documentation. Tests must be cheap to write, easy to read, simple to change.</li></ul><h3>London: I spend more time fixing tests than writing production code</h3><p><em>You’re using mocks. Great. One mock here, another mock there. This mock returns another mock, hell yeah! You’re moving fast. Your tests are green. Everything is awesome. One day you have this great idea on how to make your app even simpler, without changing the functionality. It’s the day you don’t want to talk about. Everything falls. You can’t move. Every little change causes hundreds of tests to fail. </em><strong><em>You’re doing it wrong!</em></strong></p><ul><li>You’re suffering from the test-induced damage. Refactoring is a nightmare, the feature delivery dramatically slows down. Your mock-heavy tests are so tied to the implementation that every little change is a chunky sidequest. Time to grab your sword and face the beast!</li><li>There’s one little detail someone forgot to tell you about mocks. Mocks are awesome for deferring decisions, but you shouldn’t leave too many mocks behind. Once you’ve got it all sorted, understood, implemented, why not to introduce a real collaborator instead of a mock? It might require a bigger refactoring, but hey, you’re gaining confidence here. Your unit tests become mini-acceptance tests. This is a win! It’s also a good occasion to review and maybe even delete some tests as you’re gaining more coverage with less. Oh, and you might not need so many end-to-end tests. What do you say about that?</li><li>Ever scratched your head wondering what does the test actually assert? It’s so heavily mocked it’s hard to tell if it brings any value. Kill it. A friend you can’t trust is not a friend.</li><li>Rule of thumb is: fewer mocks leads to more evolvable and bug-free system. Use mocks as long as they bring value. They should not stop you from moving forward. They should not stop you from refactoring. Keep them, as long as they help. The moment they start coming in the way, step back and have another look.</li></ul><h3>Detroit or London: My design is not emerging, it’s a ball of mud…</h3><p><em>Test after test your system is getting more complicated, slipping out of your control. Where is this design supposed to emerge? I don’t see it! It’s only getting harder and harder to write the next test and keep other tests passing. I hate this Detroit/London school of TDD. </em><strong><em>You are doing it wrong!</em></strong></p><ul><li>If architecture only comes from the tests you’re asking for trouble. Just because you’re writing lots of tests it doesn’t mean your design will blossom. On the other hand, if your design sucks your tests will tell you that. They will become harder to write, too delicate to maintain, too complex to comprehend. Time to step back, think, sketch a bit and go for a refactoring rampage.</li><li>Time spent on learning about architecture and design will pay back. To observe emerging design you need to be sensitive to a good design. Those design patterns can be useful here. But don’t over-engineer. Don’t pull in a big architecture from day one. Just let the tests guide you and surprising things will happen.</li></ul><h3>A little detour on mocks and the price of fame</h3><p>For many, using mocks has become synonymous with Test-Driven Development. I’ve even seen job ads where a candidate was required to demonstrate her fluency of TDD using mock frameworks. Perhaps the popularity comes from the fact that the style is easier to start with: you don’t have to build up the context that much, layers are easier to work on, there’s less team contention, and plenty of mock libraries available to use. While the idea of mock objects is great, it’s a tool much prone to misuse. Many accusations come to the London School of TDD of being heavily mock driven and having a bad influence on system design. This couldn’t be farther from the truth. The London school embraces the benefits of mocking while defining the best practices so the application won’t suffer from over-mocking. When committing to mocks it’s worth keeping in mind a few key points:</p><h3>The heavily mocked test is not its final state</h3><p>Use mocks for discovery, for deferring writing all the prod code in one go. Once you’ve got it all sorted, review if mocks are still serving the purpose, or just tying your tests to the implementation. Some mocks will be useful, get rid of the rest.</p><h3>Use mocks for awkward collaboration</h3><p>Test doubles are invaluable to remove slowness or non-determinism when talking to external services, file system, or database. Mocks provide the edge you need for your tests to be fast and stable. You should also consider using fakes (objects with working but simplified implementations) which can bring similar benefits, but also be reused. Fakes are especially useful in happy-path scenarios.</p><h3>Use mocks to induce errors</h3><p>Application complexity arises the moment when it needs to handle errors arising from external interactions. How to make your application resilient? Test for errors. There are many ways to induce errors in tests, mocks being one of the prominent examples.</p><h3>Only mock types that you own</h3><p>Don’t mock external, third-party code. It’s a risky business to imitate intricacies of external API which you don’t fully understand. It gives you a false sense of confidence and makes your test setup harder. Introduce an adapter layer. You can reuse it all across your app without leaking external API details. When mocking adapter, you’re in control — you know the contract. Moreover, you can assess the correctness of adapter implementation with an integration test.</p><h3>Use the right mocking tool</h3><p>Go and find the right tools for a job. Don’t take it for granted. Play with a few before you settle on one. If it’s hard to work with or there’s too much to remember about, go and try another one. The most popular don’t need to be the best. If your language has first-class, succinct lambda support, and it’s easy to redefine a function or a method then consider not using a mocking library. Embrace the functional programming style. In my many years with Scala and Clojure I have never had to pull in an external mocking tool.</p><h3>Summary</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*4iYJmMxwRQrxoL2OChMEuA.jpeg" /></figure><p>I hope you had a chance to grasp the differences between the Detroit and the London Schools of TDD. By now you should understand the differences of using one style or another, their distinct take on unit testing, approach to test writing and test doubles. I hope you can see how both styles are equally useful, and not necessarily contradict each other. Which approach would you go with in the next project? Perhaps a combination of techniques can bring benefits? Choose whatever works for you. Be pragmatic, stay open-minded. As long as you test on the right level, wisely use proper tools for the job, and let your ideas materialise cleanly and consistently, you should be good. If something doesn’t work, listen to your tests and refactor. Keep your tests fast, easy to read, cheap to change. Your test harness is the living documentation of your system. It gives you confidence. It lets you sleep better at night. Praise your tests. If they don’t give you the courage to move forward… <strong><em>you’re doing it wrong</em>.</strong></p><p>Thanks to Duncan McGregor, Matt McDonald, Ben Usher, Augusto Rodriguez, and James Anders for comments, corrections, and discussion.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d2f8dca71e5" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/detroit-and-london-schools-of-test-driven-development-3d2f8dca71e5">Detroit and London Schools of Test-Driven Development</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>