<?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 Henrique do Amaral on Medium]]></title>
        <description><![CDATA[Stories by Henrique do Amaral on Medium]]></description>
        <link>https://medium.com/@hen.amaral?source=rss-4c6c960d05d0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1H469iKMsrKTSo-G-vrOgQ.jpeg</url>
            <title>Stories by Henrique do Amaral on Medium</title>
            <link>https://medium.com/@hen.amaral?source=rss-4c6c960d05d0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 11:40:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hen.amaral/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[Tips to write better code: Side Effects]]></title>
            <link>https://medium.com/@hen.amaral/tips-to-write-better-code-side-effects-19faa4601934?source=rss-4c6c960d05d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/19faa4601934</guid>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[code-smells]]></category>
            <dc:creator><![CDATA[Henrique do Amaral]]></dc:creator>
            <pubDate>Tue, 25 Sep 2018 13:30:17 GMT</pubDate>
            <atom:updated>2018-09-27T18:01:40.978Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XpduAXTdIHi9_HiiBd4iJg.jpeg" /></figure><p>My idea today is to show how to avoid the Side Effects using real examples. I know there are lots of articles talking about it, but it’s very difficult to have real examples.</p><p>Here we have a simple example of Side Effect, where we have a lot of calls without no return.</p><p><strong>Example:</strong></p><pre>public void saveClient(final String name, final Date birthDate) {</pre><pre>final Client client = new Client(name, birthDate);<br>  calculateIdByBirthDate(client);<br>  db.save(client);<br>}</pre><pre>public void calculateIdByBirthDate(final Client client) {</pre><pre>if(client.name.startsWith(&quot;A&quot;)) {<br>    client.setId(calculateIdWithNameStartsA(client));<br>  } else {<br>    client.setId(calculateNormalId(client.birthDate));<br>   } <br>}</pre><pre>public void calculateIdWithNameStartsA(final Client client) {<br>  if(client.birthDate.year &lt; 1990) {<br>    client.setId(calculateCoolId(client.birthDate));<br>  } else {<br>    client.setId(calculateNormalId(client.birthDate));<br>  }<br>}</pre><p>There are 2 problems in the code:</p><p>First: It isn’t known what happened after the method <strong>calculateIdByBirthDate </strong>is called. We can’t be sure if the <strong>ID</strong> was successfully generated or not. Additionally, there is a chance of error after the save method is called, because of a failure in generating the <strong>ID</strong>.</p><p>Second: If the calling order of both <strong>save </strong>and <strong>calculateIdByBirthDate </strong>methods are altered, the code should fail again.</p><p>So we can refactor the code to avoid the problem.</p><p><strong>Refactored Example:</strong></p><pre>public void saveClient(String name, Date birthDate) {<br>  Client client = createClient(name, birthDate);<br>  db.save(client);<br>}</pre><pre>public Client createClient(String name, Date birthDate) {<br>  Client client = new Client(name, birthDate);<br>  client.setId(calculateIdByBirthDate(client));<br>}</pre><pre>public int calculateIdByBirthDate(Client client) {<br>  int calculatedId = calculateNormalId(client.birthDate);<br>  if(client.name.startsWith(&quot;A&quot;) &amp;&amp; client.birthDate.year &lt; 1990) {<br>    calculatedId = calculateCoolId(client.birthDate);<br>  }</pre><pre>return calculatedId;<br>}</pre><p>Here’s another way to do it, where firstly the <strong>ID </strong>is<strong> </strong>generated<strong> </strong>and then used it with the class constructor:</p><pre>public void saveClient(String name, Date birthDate) {<br>  int id = calculateIdByBirthDate(name, birthDate);<br>  Client client = new Client(id, name, birthDate);<br>  db.save(client);<br>}</pre><pre>public Integer calculateIdByBirthDate(String name, Date birthDate) {<br>  if(client.name.startsWith(&quot;A&quot;) &amp;&amp; birthDate.year &lt; 1990) {<br>    return calculateCoolId(client.birthDate);<br>  }<br>  return calculateMimiId(client.birthDate);<br>}</pre><p>Now let’s take a look at a different issue, where we have a class which calls two or more void methods from other classes. Let’s try to use the client example:</p><pre>class CreateClient {</pre><pre>DefineClienStatus defineClienStatus;<br>    ProcessClientPhoto processClientPhoto;<br>    ClientDatabase clientDatabase;</pre><pre>public void create(final Client client) {</pre><pre>defineClienStatus.define(client);<br>        processClientPhoto.process(client);<br>        clientDatabase.save(client);<br>    }<br>}</pre><p>As we can see, there could have a lot of issues, because we are calling two or more classes without no returning value. If there is another change in the order in which the calls occur, some issues can happen.</p><p>Let’s figure out a way to fix the problem:</p><p>A good approach is where each class has the return of what is doing and the next process depends on the conclusion of the previous one:</p><pre>class CreateClient {</pre><pre>DefineClienStatus defineClienStatus;<br>    ProcessClientPhoto processClientPhoto;<br>    ClientDatabase clientDatabase;</pre><pre>public void create(Client client) {<br>        client.statusClient = defineClienStatus.define(client);<br>        client.photoClient = processClientPhoto.process(client);<br>        if(canSaveClient(client)) {<br>           clientDatabase.save(client);     <br>        }<br>    }</pre><pre>public boolean canSaveClient(Client client) {<br>        return client.statusClient != null &amp;&amp; client.photoClient != null;<br>    }</pre><pre>}</pre><p>As you can see, now there is a guarantee that we have all the values returning.</p><p>Ok, here is the first part of real examples and how we can write better code using clean code and code smell.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19faa4601934" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>