<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Maciej Posłuszny</title>
    <description>The latest articles on DEV Community by Maciej Posłuszny (@spalonytoster).</description>
    <link>https://dev.to/spalonytoster</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F23550%2F6c9de006-1b79-4ff1-8cbb-15edcd50bfb5.png</url>
      <title>DEV Community: Maciej Posłuszny</title>
      <link>https://dev.to/spalonytoster</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/spalonytoster"/>
    <language>en</language>
    <item>
      <title>Refactoring is not so scary</title>
      <dc:creator>Maciej Posłuszny</dc:creator>
      <pubDate>Sat, 19 Aug 2017 09:56:52 +0000</pubDate>
      <link>https://dev.to/spalonytoster/refactoring-not-so-scary</link>
      <guid>https://dev.to/spalonytoster/refactoring-not-so-scary</guid>
      <description>

&lt;h3&gt;
  
  
  What is refactoring?
&lt;/h3&gt;

&lt;p&gt;The answer to this question is very crucial for your understanding of the whole process. &lt;em&gt;Refactoring&lt;/em&gt; is making changes in the &lt;em&gt;structure of code&lt;/em&gt;, while not making changes to the &lt;em&gt;functionality&lt;/em&gt;.&lt;br&gt;
By the way did I say &lt;strong&gt;process&lt;/strong&gt;? Yes I did, because it is a continous process and we often forget about that. Lately I've asked a fellow programmer in my company &lt;em&gt;"So how do you integrate your development process with refactoring?"&lt;/em&gt;. He said &lt;em&gt;"Everytime after the sprint we are given some time to refactor"&lt;/em&gt;. So why is this a bad practice? Because after a whole sprint you don't have as much context as you did when you've been coding every functionality. You have to: read the code, understand the code, switch to the other functionality, repeat. Think about how efficient it would be when you do this &lt;strong&gt;while&lt;/strong&gt; you're coding this new fresh stuff.&lt;br&gt;
So once again, &lt;em&gt;refactoring&lt;/em&gt; is a process that you should apply in parallel to writing code!&lt;/p&gt;

&lt;h3&gt;
  
  
  Why do we want to refactor?
&lt;/h3&gt;

&lt;p&gt;The answer is simple - &lt;strong&gt;refactoring saves costs in the future&lt;/strong&gt;.&lt;br&gt;
Every project which is getting bigger is also getting more complex. If the code quality doesn't go along with the complexity, everyone will spend a lot more time reading it. Even for the new, greenfield projects the ratio of writing/reading code might be something like 30%/70% (and it goes up to something like 5%/95% in bigger projects). If you think about optimization of these 70% and multiply it by the amount of team members, it could lead to huge savings. Now, don't you think that making your code more readable would help a lot in the future?&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I refactor?
&lt;/h3&gt;

&lt;p&gt;Let's say you're implementing a new feature and you just wrote that fresh new class that handles some operations. As the class is getting bigger you're going to see more flaws. How can you spot them? Clean code to the rescue!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Functions length&lt;/strong&gt; should be approximately 7-20 lines of code. No more. In some specific situations you won't be able to achieve this, but as you spot some lines of code that could be given a name, just extract them to a new function (you don't have to count &lt;em&gt;if&lt;/em&gt;, &lt;em&gt;while&lt;/em&gt; etc. as whole line, just their bodies).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Function arguments&lt;/strong&gt; should be &lt;em&gt;approximately&lt;/em&gt; taking 3 arguments. Ideally you'll want to take &lt;em&gt;zero&lt;/em&gt; arguments and all data would be taken from class' properties. It's not always possible so at least try to not make them more than 3.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Class length&lt;/strong&gt; should be approximately 150 lines of code. Why? Because most probably it breaks SOLID's &lt;a href="https://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; - a class should do only &lt;em&gt;one&lt;/em&gt; thing and should do it well.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Indentation&lt;/strong&gt; should be approximately 2 levels deep. Let's show you an example just for clarity:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void doSomething() {
    while(conditionIsMet()) {
        if (anotherConditionIsMet()) {
            doSomeLogic();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Don't go beyond that and you're good to go. When you are about to go deeper - &lt;strong&gt;extract&lt;/strong&gt; to functions!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If you have to use the word &lt;em&gt;and&lt;/em&gt; in your function name, then there is something wrong with it. Once again &lt;a href="https://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; of SOLID.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Extract to variables and functions&lt;/strong&gt; everytime you can provide a clear information about its purpose. Especially extract the bodies of loops to functions and number/string literals to constants!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  product.grossValue = product.price * 1.23;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;product.grossValue = calculateGrossValue(product.price);

public double calculateGrossValue(int productPrice) {
  return productPrice * PRODUCT_FEE;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simplify conditions by extracting them&lt;/strong&gt;. Similar to previous one but now we are targeting conditions in &lt;em&gt;if&lt;/em&gt;, &lt;em&gt;while&lt;/em&gt;, &lt;em&gt;for&lt;/em&gt; statements etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's easy to see what is the condition for the &lt;em&gt;if&lt;/em&gt; statement in this particular scenario&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!inventory.cars.isEmpty()) {
  doSomething();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But wouldn't it be more readable like this?&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (carsInInventoryNotEmpty()) {
  doSomething();
}

private boolean carsInInventoryNotEmpty() {
  return !inventory.cars.isEmpty();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Never&lt;/strong&gt; allow duplication of code! If a bug ever happens in one place, you will have to cover every other and most probably you will miss one of them. Instead look for a smart abstraction that you can extract. &lt;a href="https://sourcemaking.com/design_patterns"&gt;Design patterns&lt;/a&gt; are very helpful in getting out of sticky situation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Don't use comments&lt;/strong&gt;. Instead try to name your functions and variables better. Comments are allowed only for &lt;strong&gt;TODO&lt;/strong&gt;, &lt;strong&gt;FIXME&lt;/strong&gt; (not on master branch though) and some special cases where you would want to describe your intentions instead of mechanism.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are my rules of thumb to keep my code clean and organized. You can &lt;a href="https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sr_1_1"&gt;read more&lt;/a&gt; about other rules in the Uncle Bob's book or in many other resources on the internet.&lt;br&gt;
It's also good to take a look at &lt;a href="https://refactoring.com/catalog/"&gt;catalog of refactorings&lt;/a&gt; and see more tools to make your code better. Also most basic refactorings should be available in your IDE. Use it whenever you can, because your IDE is smart and will not let you refactor if it will break anything!&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;The idea of refactoring is not to make everything perfect at once. Instead you want to make a lot of small changes that have any positive influence on your code. Every time you make a change, ask yourself: &lt;em&gt;will it be more readable to me and other people that might come across this?&lt;/em&gt; If the answer is &lt;em&gt;yes&lt;/em&gt;, then you should keep it.&lt;br&gt;
Remember, you produce code for humans after all, not machines. And yes, you can make a difference :) Good luck!&lt;/p&gt;

&lt;p&gt;What are your thoughts on this topic? Share your best practices in the comments!&lt;/p&gt;


</description>
      <category>refactoring</category>
      <category>cleancode</category>
      <category>languageagnostic</category>
    </item>
  </channel>
</rss>
