<?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[The way of the Craftsman - Medium]]></title>
        <description><![CDATA[A blog about Clean Code and Test-Driven Development among other stuff - Medium]]></description>
        <link>https://medium.com/thomas-benard?source=rss----35bee5ecb4bf---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>The way of the Craftsman - Medium</title>
            <link>https://medium.com/thomas-benard?source=rss----35bee5ecb4bf---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 14:10:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/thomas-benard" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Refactoring is stupid ! (Part 2)]]></title>
            <link>https://medium.com/thomas-benard/refactoring-is-stupid-part-2-5c524d0d376c?source=rss----35bee5ecb4bf---4</link>
            <guid isPermaLink="false">https://medium.com/p/5c524d0d376c</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[refactoring]]></category>
            <dc:creator><![CDATA[Thomas Benard]]></dc:creator>
            <pubDate>Mon, 15 Apr 2019 09:13:19 GMT</pubDate>
            <atom:updated>2019-04-15T15:27:16.077Z</atom:updated>
            <content:encoded><![CDATA[<h3>Refactoring is stupid ! (part 2)</h3><p><em>The discussion reported here happens the day after the one in </em><a href="https://medium.com/thomas-benard/refactoring-is-stupid-454ba11c31e1"><em>my last post</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uaZeBiGRIjjrguvK5NcJpg.jpeg" /><figcaption>What have you done ? Now, things that work together are in different places !</figcaption></figure><blockquote>Ok, Thomas. I kind of see the interest of the refactoring we talked about yesterday. But some things are still bothering me.</blockquote><p>Hello, Jean-Michel ! Sure, what’s the problem ?</p><blockquote>If the code is all split up like that, isn’t it going to be harder to understand ? I mean, just compare my solution :</blockquote><pre>public double computeElectricalResistance() {<br>  ParticularElectricApi particularElectricApi = new ParticularElectricApi();<br>  double tension = particularElectricApi.readTension();<br>  double current = particularElectricApi.readCurrent();<br>  return tension / current;<br>}</pre><blockquote>With yours :</blockquote><pre>public Resistance computeElectricalResistance() {<br>  Tension tension = voltmeter.readTension();<br>  Current current = ammeter.readCurrent();<br>  return Resistance.createUsingOhmLaw(tension, current);<br>}</pre><blockquote>All the related code is in the same place. It’s a lot easier to understand what’s actually happening with what I proposed. With your solution, I have to browse 3 different classes and 2 interfaces in order to have a complete view of what’s happening. Such a bother !</blockquote><p>Yes, I think I understand what you mean. You prefer your proposal rather than mine because everything is in the same place, right ?</p><blockquote>Exactly !</blockquote><p>Do you remember what happened to your code when the new feature request came ?</p><pre>public double computeElectricalResistance() {<br>  ParticularElectricApi particularElectricApi = new ParticularElectricApi();<br>  double tension = particularElectricApi.readTension();<br>  double current;<br>  if (useTeslaWasBetterThanEdison) {<br>    TeslaWasBetterThanEdison teslaWasBetterThanEdison = /* instanciate API here */<br>    current = teslaWasBetterThanEdison.current()<br>  } else {<br>    current = particularElectricApi.readCurrent();<br>  }<br>  return tension / current;<br>}</pre><p>Everything is still in the same place. And yet, you don’t like it, right ?</p><blockquote>Right. In this case, I agree with your solution with the interfaces becomes better. But to be honest, it feels like all this added complexity in the initial case is just not worth it because we sacrifice readability…</blockquote><p>Remember, the purpose of code is to be changed. Therefore, good code is easy to change.</p><p>Readability matters because a readable code tends to be easier to change than code that is hard to understand. But, if making the code more readable also makes it harder to change, that defeats the original purpose of why you wanted to make that code easier to understand in the first place.</p><blockquote>Right, I get that. But, on the practical side, wouldn’t it be easier to leave it like I did and then refactor it if needed later ?</blockquote><p>Sometimes, it may be a good idea. Sometimes, it may not.</p><p>Often, there are plenty of things that could change in the code in the future. Making the code supple enough to handle all the possible cases would render it unreadable and therefore unchangeable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w-P3aRpPMypNtOjoqEw4ZQ.jpeg" /><figcaption>Checkmate !</figcaption></figure><blockquote>Ah Ah ! Checkmate ! See ? Even you ended up saying always doing this refactoring would harm maintanability !</blockquote><p>On the contrary, my friend !</p><p>What I mean is that making the code as easy to change as possible is what actually matters, because it is what the business actually wants. These guys love changing their mind ! It gives them a huge advantage on the market.</p><p>In that regard, readability and flexibility both matter, but we must be careful not to harm one in the pursuit of the other.</p><blockquote>Uh… This conversation is becoming way to theoretical for me, let’s go back to the initial problem if you don’t mind.</blockquote><blockquote>How do you know when to introduce these interfaces, then ?</blockquote><p>That’s an excellent question ! I think it takes time and experience. It’s one of those things you learn by doing, in my opinion. That’s why doing those code katas is so important: it provides us a place where we can safely try new things.</p><p>But still, as a guidance, there are two questions I like to ask myself:</p><ol><li>Is the code testable ? If not, can introducing an interface make it testable ? If so, go ahead and introduce that interface. In our case, the hard-coded dependency to ParticularElectricApi makes the code untestable without linking with the associated library.</li><li>How likely is a change to happen here ? And how hard would it be to introduce that interface while refactoring later if needed ? If it would be easy to add and a change is unlikely to happen, don’t bother. On the contrary, if it would hard and a change is very likely, you should probably introduce it right now.</li></ol><blockquote>By testable, do you mean unit testable ?</blockquote><p>Exactly !</p><p>For example, let’s imagine the function computeElectricalResistance is part of the Foo class:</p><pre>@Test<br>public void resistance_is_tension_divided_by_current() {<br>  Foo foo = new Foo(<br>    new VoltmeterThatReturns(Tension.createInVolts(1.0)),<br>    new AmmeterThatReturns(Current.createInMilliAmpere(1.0)));<br>  Resistance actualResistance = foo.computeElectricalResistance();<br>  assertEquals(Resistance.createInKiloOhm(1.0), actualResistance);<br>}</pre><p>Now I can test my code behaves as expected given any possible input defined by the interfaces I chose and run those tests every time I want, which is probably once every few minutes because unit tests are VERY fast. Isn’t that amazing ?</p><blockquote>Oh right ! I see it ! Because we have desined an interface that describes what its implementations can do, I can test what happens with this code for every possible input !</blockquote><p>That’s it ! We can write a test to see what happens if the Ammeter return 0, or null or even if it throws an exception.</p><p>We couldn’t do that with the original solution because the code was coupled with a piece of hardware, and so, untestable.</p><p>Even if the code ended up “split up in different places”, our trust in it increased tremendously !</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vu65LOLZA1JhJW9KmP9VRQ.jpeg" /><figcaption>YaY !</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c524d0d376c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/thomas-benard/refactoring-is-stupid-part-2-5c524d0d376c">Refactoring is stupid ! (Part 2)</a> was originally published in <a href="https://medium.com/thomas-benard">The way of the Craftsman</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Refactoring is stupid !]]></title>
            <link>https://medium.com/thomas-benard/refactoring-is-stupid-454ba11c31e1?source=rss----35bee5ecb4bf---4</link>
            <guid isPermaLink="false">https://medium.com/p/454ba11c31e1</guid>
            <dc:creator><![CDATA[Thomas Benard]]></dc:creator>
            <pubDate>Tue, 02 Apr 2019 08:23:20 GMT</pubDate>
            <atom:updated>2019-04-01T14:44:38.251Z</atom:updated>
            <content:encoded><![CDATA[<h3>Refactoring is stupid !</h3><p><em>The following is a reworked and anonymised transcript of a discussion I had a short while after I published </em><a href="https://thomasbenard.github.io/2017/05/27/a-simple-dependency-breaking-technique.html"><em>one of my last article</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_nOjQCF7QspsNcf0hVpFAg.jpeg" /><figcaption>I don’t really look like this…</figcaption></figure><blockquote>Thomas, there you are. I just finished reading <a href="https://thomasbenard.github.io/2017/05/27/a-simple-dependency-breaking-technique.html">your article</a> and I think it’s stupid ! Dumb, dumb dumb ! That refactoring you are talking about is dumb. Here, let me explain it to you. Let’s take this piece of code for example:</blockquote><pre>public double computeElectricalResistance() {<br>  ParticularElectricApi particularElectricApi = new ParticularElectricApi();<br>  double tension = particularElectricApi.readTension();<br>  double current = particularElectricApi.readCurrent();<br>  return tension / current;<br>}</pre><blockquote>I like this code ! It’s easy to understand, concise and clear. It is the very definition of Clean Code. Don’t you agree ?</blockquote><p>Hey Jean-Michel ! Indeed, this code looks concise and clear to me but it is definitly not clean. I can see static dependency issues along with some <a href="https://refactoring.guru/smells/primitive-obsession">primitive obsession</a>. Can you explain to me what is bothering you with what I wrote in this context ?</p><blockquote>Well, you said that here, I should introduce an interface, let’s say IElectrical and have a concrete implementation ParticularElectric that I can then inject into this code. Can’t you see the amount of code doing that would add to the code base ? More code means more bugs. It’s programming 101. That’s as simple as that.</blockquote><p>Oh dear god, no ! I would never add an interface IElectrical. Instead in this case, I would probably prefer adding two interfaces: one for Voltmeter and one for Ammeter with their corresponding implementation ParticularElectricVoltmeter and ParticularElectricAmmeter respectivly. That sounds better to me. Ideally, I would thrive to achieve something like this:</p><pre>public Resistance computeElectricalResistance() {<br>  Tension tension = voltmeter.readTension();<br>  Current current = ammeter.readCurrent();<br>  return Resistance.createUsingOhmLaw(tension, current);<br>}</pre><blockquote>What. The. Hell ??? Now you tell me I would have to add two interfaces ??? That’s insane ! That’s twice the amount of code, which means twice the number of bugs ! And I’m not even talking about those Tensionand Current classes…</blockquote><p>We will leave the discussion about Primitive Obsession for later. For now, why do you think there would be more bugs in this code than what you came up with ?</p><blockquote>Because there is more code. The greater the number of lines is, the higher the probability of introducing a bug is as well.</blockquote><p>Ok, now I get where you’re going. Now tell me, what do you spend the most time doing between adding new code and changing existing code ?</p><blockquote>Changing existing code, obviously. Even when adding new code, because we work iterativly, we end up always changing the code we just wrote.</blockquote><p>Right ! I agree with you. So, what we should focus on primarily between reducing the probability of adding a bug when writing new code versus changing already existing code ?</p><blockquote>Limiting the probability of introducing a bug when changing code should be the priority, logically. But I don’t see where you’re going with this…</blockquote><p>Have some patience, we are almost there. For now, let’s go back to your example. We received a new feature request from a customer. They are not satisfied with the Ammeter provided by the ParticularElectric company because the values read are too volatile. Instead, they want to replace it another Ammeter provided by the TeslaWasBetterThanEdison Corporation. However, because the customer don’t plan to upgrade all its systems, the code should be compatible with both Ammeters. How would you do it ?</p><blockquote>I guess I would change it to something like this:</blockquote><pre>public double computeElectricalResistance() {<br>  ParticularElectricApi particularElectricApi = new ParticularElectricApi();<br>  double tension = particularElectricApi.readTension();<br>  double current;<br>  if (useTeslaWasBetterThanEdison) {<br>    TeslaWasBetterThanEdison teslaWasBetterThanEdison = /* instanciate API here */<br>    current = teslaWasBetterThanEdison.current()<br>  } else {<br>    current = particularElectricApi.readCurrent();<br>  }<br>  return tension / current;<br>}</pre><p>Alright, that would work. Now, do you think your code is still easy to understand, concise and clear ?</p><blockquote>Well, no…</blockquote><p>Now, let’s go back to what I proposed. What needs to change in order to handle this new Ammeter ?</p><blockquote>Mmmmmh… Well, nothing. I mean, we just have to change somewhere the injectied instance of Ammeter to a TeslaWasBetterThanEdisonAmmeter but that’s it.</blockquote><p>Exactly ! Because we decoupled how we read current and tension from what we do with them, we made the code much more independant from the detail of how we fetch those values. You see, while I agree your code was <em>easy to understand, concise and clear</em>, a simple functional changed that. It was not robust to change.</p><p>Yes, Clean Code should be easy to understand, but all code that is easy to understand is not Clean. Clean Code is also easy to change. So, when writing code, you always have to wonder what changes and for what reasons. Put together the things that change for the same reason and split the things that change for different reasons (like how we read the current value and what we do with it). And that, my friend, is called the <a href="https://drive.google.com/file/d/0ByOwmqah_nuGNHEtcU5OekdDMkk/view">Single Responsibility Principle</a>, the first of the <a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">SOLID principles</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=454ba11c31e1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/thomas-benard/refactoring-is-stupid-454ba11c31e1">Refactoring is stupid !</a> was originally published in <a href="https://medium.com/thomas-benard">The way of the Craftsman</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The DRY principle is NOT about code duplication]]></title>
            <link>https://medium.com/thomas-benard/the-dry-principle-is-not-about-code-duplication-32984c7cfa7e?source=rss----35bee5ecb4bf---4</link>
            <guid isPermaLink="false">https://medium.com/p/32984c7cfa7e</guid>
            <category><![CDATA[software-craftsmanship]]></category>
            <category><![CDATA[dry-principle]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Thomas Benard]]></dc:creator>
            <pubDate>Sun, 17 Mar 2019 14:06:06 GMT</pubDate>
            <atom:updated>2019-03-17T14:28:45.678Z</atom:updated>
            <content:encoded><![CDATA[<p>DRY is one of the most famous principles in the field of software programming. And yet, it is heavily misunderstood. We often think it is about removing code duplication.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*iiaRUKK_Slho653cVe82tg.jpeg" /></figure><p>Let’s take a look at what it actually means.</p><h3>The Don’t Repeat Yourself (DRY) principle</h3><p>Enunciated by Andy Hunt and Dave Thomas in their oh-my-you-should-definitely-read-it book The Pragmatic Programmer, the DRY principle states that:</p><blockquote><em>Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.</em></blockquote><p>The DRY principle is actually about knowledge duplication. Not code duplication. It doesn’t even mention code.</p><p>For example, it may mean that:</p><ul><li>you can’t have a specification document as well as acceptance tests. They both serve the same purpose, they both hold the same information, they both say how the system should behave (and by the way, tests are doing a much better job at that).</li><li>you should stop typing all those git commands when trying to commit something. Write a script for that.</li></ul><p>It does not mean you should blindly remove all code duplication in your system. Remove code duplication only when those bits of code actually represent the same thing, the same knowledge, the same rules. Let’s take an example. Consider the two following functions:</p><pre>public class UserDescription {<br>    // ...<br>    public bool isLongEnough() {<br>        String words[] = description.split(&#39; &#39;);<br>        int numberOfWords = words.length;<br>        return numberOfWords &gt; 10;<br>    }<br>    // ...<br>}</pre><p>Here the function isLongEnough checks that this UserDescription has at least 10 words.</p><pre>public class RemoteApiResponse {<br>    // ...<br>    public bool containsEnoughElements() {<br>        String elements[] = description.split(&#39; &#39;);<br>        int numberOfElements = elements.length;<br>        return numberOfElements &gt; 10;<br>    }<br>    // ...<br>}</pre><p>While here, the function containsEnoughElements checks that the response received from a remote API has the minimum number of elements (10) to be considered viable.</p><p>Although these two functions have exactly the same code, it would probably be a terrible idea to remove the duplication.</p><p>If you did so, what would happen if the minimum number of words in the description of a user changes ? Or, if the remote API programmers decide to change how the response is formatted ? Then, you can’t change one without changing the other. Even if you can work around it, you still have to modify, recompile and redeploy the module containing the UserDescription class whenever the format of the response sent by the remote API changes.</p><p>Indeed the code is duplicated, but both functions actually represent very different knowledge: in one case, it represents the validation rules of a user description while in the other case it holds the validation rules for the response of an API.</p><h3>Don’t make your system more complex by removing too much duplication</h3><p>Code duplication is bad. Well, most of the time. It can make your system hard to change.</p><p>But, as we just saw, removing too much duplication can make your system harder to change in the same way ! Let’s not blindly remove code duplication anymore. We have to think before doing so.</p><p>We have to ask ourselves if those bits of code actually represent the same knowledge, if they implement the same rule, or if they are the same by pure coincidence.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32984c7cfa7e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/thomas-benard/the-dry-principle-is-not-about-code-duplication-32984c7cfa7e">The DRY principle is NOT about code duplication</a> was originally published in <a href="https://medium.com/thomas-benard">The way of the Craftsman</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>