<?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 Steven Kok on Medium]]></title>
        <description><![CDATA[Stories by Steven Kok on Medium]]></description>
        <link>https://medium.com/@nl-stevenen?source=rss-d6894f2dc981------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wJk4z8_LoFWWmeXW9iIw6w.jpeg</url>
            <title>Stories by Steven Kok on Medium</title>
            <link>https://medium.com/@nl-stevenen?source=rss-d6894f2dc981------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 01:50:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nl-stevenen/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[Testing REST APIs]]></title>
            <link>https://nl-stevenen.medium.com/testing-rest-apis-486ea864f119?source=rss-d6894f2dc981------2</link>
            <guid isPermaLink="false">https://medium.com/p/486ea864f119</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[Steven Kok]]></dc:creator>
            <pubDate>Mon, 12 Sep 2022 12:36:37 GMT</pubDate>
            <atom:updated>2022-09-12T12:36:37.627Z</atom:updated>
            <content:encoded><![CDATA[<p>How <strong>not</strong> to use object mappers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UmMfdi_vdfruiRLkFsnU_w.jpeg" /><figcaption>Copyright: Fotostudiokok, <a href="http://www.fotostudiokok.nl/">www.fotostudiokok.nl</a></figcaption></figure><h3>Object mapper vs JSON String Literal</h3><p>Web services are often built around REST APIs. And there are different approaches available to test them. In this article we’re going to look at the juxtaposition of <em>object mappers and </em>JSON <em>string literals</em>. Specifically the way I have seen them being used for numerous projects over the last 10 years. There is a way of using the <em>object mappers</em> in a better way than described in the scenarios of this post, but since I haven’t encountered them I’ll address them separately.</p><p>Let’s start by saying: I think it’s a good practice to test software behavior over implementation details. If applied correctly, this means that we are able to refactor code without having to change the tests. This is in accordance with the definition of refactoring:</p><blockquote><em>[refactor] (the source code of an application or piece of software) so as to improve operation without altering functionality.</em></blockquote><p>Let’s put this into practice. The first strategy is going to use an <em>object mapper</em> to parse json into an object and vice versa. The second test is going to use <em>string literals</em> with a <em>JSON</em> request and response.</p><p>First things first, we will have to set up the test fixtures.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ece9b0fc2823a4e4bcc9260fe9c7c462/href">https://medium.com/media/ece9b0fc2823a4e4bcc9260fe9c7c462/href</a></iframe><p>In this example we will use <em>Spring</em> with <em>@WebMvcTest</em> to prevent a complete spring context from starting, all we need are the <em>Spring Web MVC Components</em>. Furthermore, we will use <em>MockMvc </em>to do HTTP calls to our REST endpoint. And the <em>KotlinModule</em> is necessary for the <em>object mapper</em> to be able to work with Kotlin’s <em>data classes</em>.</p><p>The <em>object mapper</em> test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/672d035b4283354e79bc8e572e5e40ee/href">https://medium.com/media/672d035b4283354e79bc8e572e5e40ee/href</a></iframe><p>And the s<em>tring literal </em>test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a253de70d26f32556b672eabed5fbd6/href">https://medium.com/media/6a253de70d26f32556b672eabed5fbd6/href</a></iframe><p><em>Note here that I choose not to check the full JSON response but only the fields important to this specific test. More about this later.</em></p><p>Both tests call this endpoint:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0950620fa2286efb30ec140f9bcb137/href">https://medium.com/media/b0950620fa2286efb30ec140f9bcb137/href</a></iframe><p>Alright, now we have a small application with a REST endpoint. Which is tested by the two strategies mentioned. We will now look at multiple scenarios where we will expand on this application to compare which testing strategy gives us the best result.</p><h3>Scenario: add a non-required field</h3><p>A non breaking change which adds a not-required field to the request and response. The field <em>age</em> was added to the request.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2c11e5c5182064075e5fbc087ba0c02/href">https://medium.com/media/d2c11e5c5182064075e5fbc087ba0c02/href</a></iframe><p><strong>Conclusion: </strong>All <em>object mapper</em> tests that call this endpoint will have compile errors. It’s expecting <em>age</em> to be set in the constructor or the request.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48a2547df1ea62aa862ed673362e62f8/href">https://medium.com/media/48a2547df1ea62aa862ed673362e62f8/href</a></iframe><p>If the creation of this object was done with the <em>factory pattern</em>, then it would probably have resulted in ‘only’ one change, otherwise it might mean many changes throughout the test code; all the places where the constructor is called.</p><p>However, all the s<em>tring literal</em> tests are compiling and still running green without any change necessary! We only have to <strong>add</strong> a test to cover this new feature. In other words, our tests are open to extension and closed to modifications; <em>Open-Closed Principle.</em></p><p><em>Note that if you really want your tests to fail when a field was added that is not required, then it’s possible to not limit the checks to the specific fields in your tests but to check that the complete JSON response is equal to what is expected.</em></p><p>The exact same situation occurs when adding a non-breaking, non-required field to the response object. What I mean in this case is that the <em>endpoint</em> doesn’t mind if <em>age</em> is ignored by the client in the <em>request</em> and the <em>response, </em>it will hold the default value:<em> 0. </em>Thus, this <em>endpoint</em> can be extended and clients can keep using the previous version (without <em>age) </em>until they’re ready to update.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5c1e4436d8d97f03a3778df051ae1fb/href">https://medium.com/media/d5c1e4436d8d97f03a3778df051ae1fb/href</a></iframe><p><strong>Conclusion: </strong>All the o<em>bject mapper</em> tests need changes again, while the s<em>tring literal</em> tests don’t need any modifications.</p><h3>Scenario: change the name of an existing field</h3><p>Changing the name of an existing field is a breaking change. It’s important to be careful with these changes because it can unexpectedly cause interoperability issues with clients once the software is deployed. It might happen that this is <strong>not</strong> seen until it’s deployed to production, depending on the testing strategy applied. And the sooner we find the bug the less impact it has on our customers and how quicker it probably will be fixed. So let’s take a look at a minor change in the code. <em>Note: I don’t qualify this change as a refactoring in the strictest sense of the definition, because our API changes which causes a functionality change for external clients.</em> This is a change that might be intentional or accidental (i.e. by not overseeing the repercussions of other code depending on it) but let’s look at the consequences for our tests.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0963d260637ad20172f6a2347fd377da/href">https://medium.com/media/0963d260637ad20172f6a2347fd377da/href</a></iframe><p><strong>Conclusion: </strong>Interestingly all the <em>object mapper</em> tests are green after this breaking change, not even a compile error. They completely fail to find this breaking change we made. However, the s<em>tring literal</em> tests are failing, they recognise a breaking change has occurred and warn the programmer. Good thing we added that s<em>tring literal</em> test, or we might have noticed it only when it was deployed to production.</p><h3>Scenario: using dates</h3><p>For this scenario I’ve added a <em>created</em> field in the response of the endpoint:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16b11e076e835090c16c3ada2f802120/href">https://medium.com/media/16b11e076e835090c16c3ada2f802120/href</a></iframe><p>And here are the updated test cases:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/baf73a8738b18f75dc619c8070a3b429/href">https://medium.com/media/baf73a8738b18f75dc619c8070a3b429/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55c95a67a5f0e3585834d50765994580/href">https://medium.com/media/55c95a67a5f0e3585834d50765994580/href</a></iframe><p>Let’s now change how the date is formatted, again: this can be accidental (i.e. meant to be a change only in the domain or another layer like the database) or on purpose:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/428c4546bdcf39480ddba327490e3e81/href">https://medium.com/media/428c4546bdcf39480ddba327490e3e81/href</a></iframe><p><strong>Conclusion: </strong>Strange, all o<em>bject mappers</em> tests are still green, even though we have introduced yet again breaking changes to the API! The more reliable s<em>tring literal</em> tests however, have noticed the breaking change and the test fails to warn the programmer, excellent.</p><h3>Scenario: Changing object mapper settings</h3><p>Custom serializers can be added to the object mapper. It can help to parse a certain class to <em>json</em> or how <em>Enum</em> values should be represented.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fa72840faae739b67e8b80b827f473d/href">https://medium.com/media/3fa72840faae739b67e8b80b827f473d/href</a></iframe><p>What happens here is that the JSON response returned to the caller contains two fields: <em>upper</em> and <em>lower</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f9985b9b63bc76bba82587f3c78eaa9/href">https://medium.com/media/9f9985b9b63bc76bba82587f3c78eaa9/href</a></iframe><p>But the test deserializes the response back to an object <em>Type</em>; that means it doesn’t know anymore about those fields. Here is the <em>object mapper</em> test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73b80cc600d8628d246aee51f972304a/href">https://medium.com/media/73b80cc600d8628d246aee51f972304a/href</a></iframe><p>So it’s not even possible at this point to test the exact message received or sent by a client. All we have is our <em>Enum class Type.</em></p><p>And the <em>String literal</em> test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c66af274b9619207cae55bc123cfd6a4/href">https://medium.com/media/c66af274b9619207cae55bc123cfd6a4/href</a></iframe><p>This <em>String Literal</em> can now verify exactly the message sent and received from a client.</p><p>Furthermore, if we now make a small change to the serializer and the deserializer, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8eeade318ab0cd220f3aeabd8b185add/href">https://medium.com/media/8eeade318ab0cd220f3aeabd8b185add/href</a></iframe><p>Then we see the same problem as in the other examples. <br><strong>Conclusion: </strong>The o<em>bject mapper</em> tests stay green after these breaking changes are made, while the s<em>tring literal</em> tests fail, thus show the developer that the contract has changed in a breaking way.</p><h3>Scenario: (accidental) leaking of domain values</h3><p>In the case that the view layer not only depends on <em>logic</em> from, for example the domain layer, but also on <em>values, </em>then this can cause accidental breakage of APIs. For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/237fe27e2a2c69d9458bf5951576c091/href">https://medium.com/media/237fe27e2a2c69d9458bf5951576c091/href</a></iframe><p>Here we have an <em>Enum: Type </em>in the domain layer of the application (considering a Clean / Hexagonal / Onion Layered / Ports and Adapters Architecture). This <em>Enum</em> is used by different modules of the application, for example the <em>View</em><strong><em> </em></strong>layer that contains an Endpoint and a <em>Repository</em> that connects to a MYSQL database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/336a9cdc54e2af9829e327768d5851ae/href">https://medium.com/media/336a9cdc54e2af9829e327768d5851ae/href</a></iframe><p>In this response an error was made, because encapsulation is broken. We are now exposing a domain value <em>AMIN or SUPER_USER</em> to external clients. Which means we cannot change our domain Enum value any more without breaking our clients. Let’s change it a little bit to show you what I mean:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a98c786d2d21d2ebf00136ffeeee86d/href">https://medium.com/media/2a98c786d2d21d2ebf00136ffeeee86d/href</a></iframe><p><strong>Conclusion: </strong>If we run our tests again, we see that all our <em>Object Mapper</em> tests have succeeded. Again it doesn’t notice that our API has a breaking change! Luckily the s<em>tring literal</em> tests do notice it, since it expects SUPER_USER to be returned and not POWER_USER , and so do our clients.</p><p>These changes can be very subtle, it’s just a simple refactoring of a domain level value, but if testing isn’t done correctly, it can go by unnoticed!</p><h3>Testing behavior with Object mappers</h3><p>Like mentioned at the start of this post, I’ll now go over an example with an <em>object mapper</em>, that works equally well compared to the <em>string literal</em> tests. Let’s first ask the question: why is it so that the <em>object mapper</em> fails to find breaking changes in our API? It’s not because of the <em>object mapper</em> per se, it’s because we are telling the <em>object mapper</em> in our tests to use the: <em>PostUserRequest </em>and<em> PostUserResponse </em>objects. Which are implementation details, not behavior! It would be great if we can change the whole implementation without changing the tests, so we shouldn’t use those <em>request</em> and <em>response</em> objects. The <em>string literal</em> test is not using those implementation details, and therefore it fails the test when there is a breaking change. Let’s create a test, with an <em>object mapper </em>without using implementation details:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0bb633e6da25768daab57d5e9a298eb/href">https://medium.com/media/f0bb633e6da25768daab57d5e9a298eb/href</a></iframe><p>I’ve added two new <em>data classes</em>: <em>TestPostUserRequest </em>and <em>TestPostUserResponse. </em>Let’s try Scenario 2 again, by changing the name of a field causing a breaking change.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31330e3874f421e8c676c1c405239d3d/href">https://medium.com/media/31330e3874f421e8c676c1c405239d3d/href</a></iframe><p>Note that this is the <em>data class</em> used by the controller <strong>not </strong>the <em>data class</em> used by the test (<em>TestPostUserRequest)</em>.</p><p><strong>Conclusion: </strong>The old <em>object mapper</em> test succeeds, like before, failing to recognise a breaking change. However, the <em>string literal</em> test and the new <em>object mapper </em>test with its own <em>data classes </em>also fails, so it recognises a breaking change! Perfect.</p><h3>Conclusion</h3><p>I hope you have also come to the conclusion that testing behavior over implementation details is a good idea. We don’t want to bind our tests to our internal view logic (<em>data transfer objects)</em> or <em>database implementations</em> or any other implementation details. We just want to verify, in this case that the API, works correctly with a certain input and output. So we treat it as a black box. It’s easy to find out if this is applied correctly in your application with a simple experiment. Do some refactoring, and remember to only change <em>operations without altering functionalities </em>and if the tests are written well, they will all succeed without having to change them!</p><h3>Corollary: Hyrum’s law</h3><blockquote>With a sufficient number of users of an API,<br>it does not matter what you promise in the contract:<br>all observable behaviors of your system<br>will be depended on by somebody.</blockquote><p>Colloquially speaking this is M<em>urphy’s law</em> applying to APIs. So any change we make to our APIs might cause unexpected problems to some of our clients, even with what we consider to be non-breaking changes, like mentioned earlier in the scenarios. What this means for our purposes is that we want our tests to catch as much changes as possible. We’ve already seen how often an <em>object mapper </em>test fails to recognise changes, so that’s not helpful. What we can do, is make the <em>string literal</em> tests even better, in at least two ways:</p><ol><li>Verify the complete response body, not just specific fields. So that we recognise new fields as well, some clients do break on it.</li><li>Use real data. By limiting the test on only the fields returned and not on the actual data, we can still break clients; as we have seen with the date parser in one of the scenarios.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=486ea864f119" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Compound effect in software]]></title>
            <link>https://nl-stevenen.medium.com/the-compound-effect-in-software-9619bb1753d3?source=rss-d6894f2dc981------2</link>
            <guid isPermaLink="false">https://medium.com/p/9619bb1753d3</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Steven Kok]]></dc:creator>
            <pubDate>Sat, 01 May 2021 07:33:09 GMT</pubDate>
            <atom:updated>2021-05-01T07:42:34.836Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PWdJ9gTVnW5w_dzEHOxq2Q.jpeg" /></figure><p>A common problem among software projects is its increasingly growing time to maintain and add new features. The balance between adding new features and fixing bugs seems to change over time. In a brand new project it’s all about implementing new ideas but as time passes by the code is slowing us down. This is due to all the (small) decisions that were made over the duration of the project: the compound effect.</p><p>As developers we make decisions about our code all the time. Should we write a quick fix or solve it the right way? Do we write automated tests or will we skip it because it takes longer than writing the production code? Do we only do the minimal necessary work to implement a new feature or do we also apply the boy scout rule; <em>to leave the code in a better condition than we found it.</em></p><p>The answers to these questions depend on an array of circumstances. Are we under time pressure or are we just tired after a long day? We might pick the easy way out. However, every small decision we make will influence other decisions that will be made in the future.</p><p>Before we continue with some examples, let me give you a short definition. Compounding, compound interest or the compound effect is the aggregation of changes over time. Every change leads to more changes, the more changes there are the more additional changes will happen. This can go in two directions: positive or negative, but they tend to grow exponentially. Let me show you some examples.</p><h3><strong>The power of 1 percent¹</strong></h3><p>In 110 years the British cyclists had never won in the Tour de France. Their results and reputation got so bad, that one of the top bike manufacturers refused to sell bikes to them because they were afraid of bad publicity. At some point the Brits decided to hire Dave Brailsford. Who stuck to a strategy he called: “the aggregation of marginal gains”. He looked at all the aspects of bike riding, and tried to improve everything by only 1%. At first little could be noticed, but after 5 years of marginal improvements the results started to compound. The British cyclists “suddenly” won 60% of all the gold medals at the olympic games in 2008, set 9 Olympic records and 7 world records and won the Tour de France 5 times in 6 years. During ten years they won 178 world championships and 66 Olympic or Paralympic gold medals.</p><p>The compound effect can work for us, but it can also work against us. Let’s take a look at the other side of the coin when it’s spiralling out of control.</p><h3><strong>Broken window theory³</strong></h3><p>Let ’s say we have a street somewhere in a city that’s in good condition, it’s clean, well maintained and all the buildings are in use. One day one of the shop owners moves away from the street, the building becomes vacant. So far all of this is still fine, until by chance a window gets broken; let it be by an accident, storm or violence. Normally, if the shop is occupied the owner fixes the window as soon as possible otherwise it makes it less attractive for customers to shop there. But in this case it might not be fixed at all.</p><p>So what’s the big deal? It’s only a broken window. This is however, where the compound effect starts to kick in. People walking there at night might feel it’s not so bad to put some graffiti on the building, it’s not like it’s used anyway, right? A window is already broken, how bad can it be? But now, with a broken window and graffiti on the walls it probably won’t take long until another window gets broken, more graffiti will be added and this might even open the doors for squatters to occupy the building. The worse the appearance of the building is, the more trouble it will attract, people might start avoiding this street altogether. The other shops in the street get less customers, and might decide to move away as well! Then things get worse and worse exponentially over time; this is the compound effect too.</p><h3><strong>Compounding Software</strong></h3><p>The same thing happens with software applications. We get assigned to work on a project that’s in horrible condition; hard to read, untested and difficult to change. If we have to do a minor bug fix, we tend to feel like it’s way too much work to clean it up as well. The fix might take 15 minutes, but cleaning it up and testing it, the rest of the day, if not more. So it’s tempting to just apply the fix without testing and to forget about the boy scout rule: <em>to leave the code in a better condition than you found it.</em></p><p>It didn’t feel so bad making that small fix, it was quick, the manual test went well, maybe you even got some positive feedback from your manager for handling it so quickly. However, the consequences of this approach might not become apparent until weeks or months later.</p><p>The challenge with this approach is that bugs are more common in untested, hard to read code. It’s only a matter of time before another minor bug fix needs to be done on this piece of code. Every addition makes it more complex and being untested and hard to read doesn’t help. It’s likely that over time more parts of the system depend on this part, making the whole system increasingly unstable. A colleague or your future self might want to refactor it in a later stage. Which has become harder to do, because without those tests, how do you know if everything still works as it should? Without code that is easy to read, it is harder to oversee the consequences. The problems don’t stop there. It might become a big frustration to work on this particular project, lowering team morale. Fixing bugs costs more time, motivation is lost to clean up other parts and the time it takes to implement a new feature becomes longer. The compound effect is hurting us.</p><p>It is not so obvious to predict the consequences of writing that small bug fix. But look at it from a higher level, ask yourself, how might this affect me, the team or the application in a month from now, or a year, or 3 years?</p><p>Working on badly structured code where every time you fix a problem something else breaks, is frustrating. Demotivated developers spend less time on improving the system leading to more problems in the future. Small decisions matter.</p><p>Albert Einstein once said:</p><blockquote>“Compound interest is the eighth wonder of the world. He who understands it, earns it … he who doesn’t … pays it.”</blockquote><p>Do you want to pay it, by being unproductive, frustrated and costing the company money? Or do you perhaps want to earn it, by being productive, happy and saving the company money?</p><p>By breaking the cycle of bad habits and consistently applying good habits, we can let the compound effect work in our favor. Make small steps of improvements every day, so that it will compound over time. Just like Dave Brailsford did when he helped the British Cyclists to win a lot of races and set new world records. “The aggregation of marginal gains” is all he did.</p><p>The Japanese would call this approach: Kaizen⁴:</p><blockquote>“The message of the Kaizen strategy is that not a day should go by without some kind of improvement being made somewhere in the company.”¹</blockquote><h3><strong>The plateau of latent potential</strong></h3><p>Certainly, the benefits of this approach do not always appear to be clear from the start. This is called: The plateau of latent potential¹.</p><p>When a feature is implemented we often get feedback very quickly. We can show our (preliminary) work to our team and stakeholders and then we can make additional changes to it, to receive additional feedback. We have a linear function of effort and result. This is not always the case with improving the quality of the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*v3Bo2br5hsZCu7Pr" /></figure><p>As illustrated in the image above, when you start with small improvements, you might expect results right away, but they are still hard to find. Quite some effort is put in, but with little reward; this is the valley of disappointment.</p><p>Only by consistently applying good habits; small code improvements, over a longer period of time, will the results become visible and start to compound. The easier it is to read code, the easier it is to add a new feature. The better the code is tested, the more bugs are prevented and the safer it is to clean up the code. The cleaner the code, the easier it becomes to clean the rest. Time spent on building new features increases, while time spent on fixing bugs decreases. This improves the mood of the developers as well. Leading to more energy and motivation to keep the system clean and to improve it even more. Not to mention that it becomes easier for a new developer to start working on the project.</p><h3><strong>Where to begin</strong></h3><h4><em>Readability</em></h4><p>Naming is important. Give classes, functions and variables good names. Be clear and precise. Make the intention jump out. This also applies to higher level concepts like packages, modules and services. Another name for this is: “Screaming Architecture”⁵. It simply means that just by looking at the architecture, it’s clear what its purpose is. Make sure to rename any text, name or title that is not clear right away, the moment you encounter it. Make code read like a book.</p><p>Remove unnecessary indentations and empty lines. Your IDE can do some of the work for you. Comments that don’t add anything, or worse, commented out code, should be removed.</p><p>It’s easy to add logic to an existing function like a procedural script. But it makes code hard to understand. Extract logic to classes and functions so that they have only one reason to change. It should have a clear purpose with a name that indicates exactly what this is. Another benefit is that it makes code reusable!</p><h4><em>Extensibility</em></h4><p>Code must be able to be extended with minimal impact on the current code base. If you find yourself changing existing code, especially with conditional logic (i.e. if-statements) then this is often a sign of bad design. It’s good practice to improve the structure of the code so that the next time something has to be added, it will be straight forward. Think about this when writing a new feature. The ‘correct’ solution is often the one that can be extended easily.</p><h4><em>Testing</em></h4><p>Reproduce a bug first with an automated test before writing a fix. This ensures three things. First, that the bug is actually fixed. Second, that the test correctly catches the problem. Third, that it will never come back.</p><p>Writing a test for a new feature should be done for at least two reasons: Firstly, to prove the new feature works as you intended. Secondly that the feature keeps working correctly in the future. And this is a very important, often overlooked point. Sometimes it looks like the test isn’t adding any value or you have seen it work manually. But who’s to say it still works correctly after some refactoring or adding new functionality to it?</p><h3><strong>Conclusion</strong></h3><p>The benefits of small improvements are way bigger than you might predict from the start. Apply above mentioned points for everything you touch, to improve the code at least 1% every time and see your results grow exponentially over time.</p><h3><strong>Sources</strong></h3><p>1 Atomic Habits — James Clear<br>2 The Compound Effect — Darren Hardy<br>3 <a href="https://en.wikipedia.org/wiki/Broken_windows_theory">https://en.wikipedia.org/wiki/Broken_windows_theory</a><br>4 <a href="https://en.wikipedia.org/wiki/Kaizen">https://en.wikipedia.org/wiki/Kaizen</a><br>5 Clean Architecture — Uncle Bob</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9619bb1753d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>