<?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: Rachel Curioso :D</title>
    <description>The latest articles on DEV Community by Rachel Curioso :D (@rachc).</description>
    <link>https://dev.to/rachc</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%2F32207%2F33abb184-4826-4dd0-8b00-18c1103199c3.jpg</url>
      <title>DEV Community: Rachel Curioso :D</title>
      <link>https://dev.to/rachc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rachc"/>
    <language>en</language>
    <item>
      <title>Refactoring - What problem on your code do you want to fix?</title>
      <dc:creator>Rachel Curioso :D</dc:creator>
      <pubDate>Sat, 09 Nov 2019 15:42:35 +0000</pubDate>
      <link>https://dev.to/rachc/refactoring-what-problem-on-your-code-do-you-want-to-fix-487f</link>
      <guid>https://dev.to/rachc/refactoring-what-problem-on-your-code-do-you-want-to-fix-487f</guid>
      <description>&lt;p&gt;When we start to study refactor techniques, we came across several (anti)patterns that we can find in our code. Many of these patterns have names that aren't very clear and, at first sight, is hard to understand what they are.&lt;/p&gt;

&lt;p&gt;Having this difficulty, I decided to create a guide to identify which patterns are according to the pain that we feel when we are viewing the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Code that has grown way more than they should (Bloater)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The same group of data is repeated in several different places&lt;/strong&gt; = Data Clump&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;There are too many primitives, a lot of constants or primitives that should be an object&lt;/strong&gt; = Primitive Obsessions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Methods that are too long&lt;/strong&gt; = Long Method&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Classes that are too large&lt;/strong&gt; = Large class&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;We have way too many parameters&lt;/strong&gt; = Long parameter list&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  2. Problems arising for misuse of object-orientation (Object-Orientation Abusers)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A lot of ifs/switch case&lt;/strong&gt; = Switch Statement&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Temp Variable&lt;/strong&gt; = Temporary Field&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Two classes with the same function but different names&lt;/strong&gt; = Alternative Classes with Different Interfaces&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;My child class uses very little of the father class&lt;/strong&gt; = Refused Bequest&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3. Problems that make any code change way too hard, because we have to modify several different places to change something(Change Preventers)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;To make a simple change, I need to go through several different methods in a class&lt;/strong&gt; = Divergent Change&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In order to change something, I need to go through several different places in different classes&lt;/strong&gt; = Shotgun Surgery&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If I need to create a child class for class X, I also need to create a child class for class Y&lt;/strong&gt; = Parallel Inheritance Hierarchies&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  4. There is a lot of useless stuff in there. Their presence only difficult the code understanding (Dispensable)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Duplicate code&lt;/strong&gt;  = Duplicate code (duh)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;There is a class that doesn't do anything&lt;/strong&gt; = Lazy Class&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The piece of code was created thinking about the future (that probably never came)&lt;/strong&gt; = Speculative Generality&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I have a class that is a bunch of getters and setters, and it exists only to be manipulated. Probably it serves only to be a database interface&lt;/strong&gt; = Data Class&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comments&lt;/strong&gt; = Comments (duh2)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  5. The classes are too close. They are dependent on each other, and there is a lot of coupling (Coupling)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Methods that access a lot of gets of another object&lt;/strong&gt; = Feature Envy&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Object that calls another object that calls another object that calls anot...&lt;/strong&gt; = Message Chains&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A class acts only as an intermediary for another&lt;/strong&gt; = Middle man&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A class use methods and fields of another class&lt;/strong&gt; = Inappropriate Intimacy&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I Installed a lib that doesn't do everything that I need&lt;/strong&gt; = Incomplete Library Class&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These patterns only identify what to look when we want to refactor. For every pattern there is a recipe to fix that. You can find all &lt;a href="https://refactoring.guru/refactoring/smells"&gt;in this website&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to/can invest a little more, I advise you to buy the "refactoring" book, because they have (waaaaay) more examples than the website mentioned above, and is a good source for consultation.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;imagem do post de &lt;a href="https://unsplash.com/@alex_andrews"&gt;Alex Andrews&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>refactor</category>
      <category>productivity</category>
      <category>practices</category>
      <category>books</category>
    </item>
    <item>
      <title>Identifying the dirt in our code - third party, tests and classes</title>
      <dc:creator>Rachel Curioso :D</dc:creator>
      <pubDate>Tue, 15 Oct 2019 00:44:53 +0000</pubDate>
      <link>https://dev.to/rachc/identifying-the-dirt-in-our-code-third-party-tests-and-classes-5epd</link>
      <guid>https://dev.to/rachc/identifying-the-dirt-in-our-code-third-party-tests-and-classes-5epd</guid>
      <description>&lt;p&gt;When I started with the clean code series, the intention was not only to fix my knowledge about the subject but to pass on the knowledge that I acquired.&lt;/p&gt;

&lt;p&gt;This series also serves as a quick guide to the book. When I need to remember some topics, I can find faster what I'm looking for.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/rachc/identifying-the-dirt-in-our-code-2f3h"&gt;On the first post&lt;/a&gt;, I talked about names, functions, and comments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/rachc/identifying-the-dirt-in-our-code-pt2-3lj0"&gt;On the second post&lt;/a&gt; I talked about formatting, object and data structure, and how to handle errors.&lt;/p&gt;

&lt;p&gt;In this last post, I talk about how to handle third-party code, how to organize your tests and your classes.&lt;/p&gt;

&lt;p&gt;In addition to these 9 topics, the clean code book talks about dealing with systems, some more topics about Java, and it also covers how to identify some code smells.&lt;/p&gt;

&lt;p&gt;I decided not to cover these topics because I would like to make the series more generic and friendly to all languages. Also because I don't have so much practical knowledge about building systems from scratch, and I also believe that an exclusive series on refactoring could better address code smells since in the clean code book the theme is a bit superficial.&lt;/p&gt;

&lt;p&gt;That said, we can start with the topics of today's posts:&lt;/p&gt;

&lt;h1&gt;
  
  
  Handle with third-party code
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f5m6FZuH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/amzbxl4m73rm3kpv7bo7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f5m6FZuH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/amzbxl4m73rm3kpv7bo7.gif" alt="two aliens partying"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We hardly make a code 100% alone. We usually use some lib to help us, and most of the times, some colleagues contribute to the code&lt;/p&gt;

&lt;p&gt;It's important to learn how to handle the code boundaries, identifying where our code ends and the other's code begins so we can maintain the system working more easily.&lt;/p&gt;

&lt;p&gt;One of the problems with using third-party code is that they usually have more features than we need and this can make our code behave strangely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encapsulate your library!
&lt;/h2&gt;

&lt;p&gt;The best way to solve this problem is to encapsulate the library we will use in a class, and pull only the methods that we will need.&lt;/p&gt;

&lt;p&gt;Another advantage of encapsulating a third-party library into its own class is by doing so, we can make it easier to change the lib versions or even swap the library itself.&lt;/p&gt;

&lt;p&gt;Let's suppose we have a library in our code that connects to a payment service. When we want to change the service, it is possible to make this change only in the class we created. We can adapt our method without the need to look through the entire code to change every reference from the old library to the new one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exploring third-party code
&lt;/h2&gt;

&lt;p&gt;When we want to add a new library to our code, we need to understand what it does and how it works. Using code we don't understand in production can cause some problems.&lt;/p&gt;

&lt;p&gt;The best way to find out the behavior of a library we want to add is to write tests for situations that we want the library to solve and get them passed. This way we can not only figure out how the code works, but these tests can be useful for any version or a library change.&lt;/p&gt;

&lt;p&gt;If you change the version and run the tests, you will find out which behaviors no longer work and correct them.&lt;/p&gt;

&lt;p&gt;If I can summarize the use of third party libraries in your code in some topics, they would be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maintain a clear separation of third party code and yours.&lt;/li&gt;
&lt;li&gt;Write good tests to see if the third party lib will behave the way that makes sense for your code. It is important to note that you are testing your code, not the library.&lt;/li&gt;
&lt;li&gt;Make sure that our code has no contact with third party codes. It is better to depend on something you control than something you do not control.&lt;/li&gt;
&lt;li&gt;Encapsulate and create adapters so that our code refers to it as little as possible, and that we can swap the library in case of any problems.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Organizing your tests
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rJ62Q5gh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/k4r7eqvykdv981518iew.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rJ62Q5gh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/k4r7eqvykdv981518iew.gif" alt="Some cartoon showing a scientist testing something"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The biggest concern we have when writing a test should be its readability.&lt;/p&gt;

&lt;p&gt;In test code, readability may be even more important than production code.&lt;/p&gt;

&lt;p&gt;When writing a test we should avoid writing details that are not part of it so that the person who needs to (re)fix this test in the future knows what is happening without the need to understand unnecessary code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use one assert per test
&lt;/h2&gt;

&lt;p&gt;Ideally, you only have one assert per test, so there is no doubt which part of the test has broken when this happens.&lt;/p&gt;

&lt;p&gt;This rule could result in many repeated tests. To solve this, you can use &lt;code&gt;before&lt;/code&gt; with the code block that repeats.&lt;/p&gt;

&lt;p&gt;If there is too much repetition, we may even use more than one assert per test, but very carefully to keep the number of asserts small.&lt;/p&gt;

&lt;p&gt;Or, giving my opinion - not Uncle Bob - you may not solve this problem, since too much &lt;a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself"&gt;DRY&lt;/a&gt; can impair code readability, and readability should always be your priority when it comes to testing.&lt;/p&gt;

&lt;p&gt;Perhaps a better rule is that each test should only test one concept.&lt;/p&gt;

&lt;p&gt;It is very important that there is not more than one test at a time to keep it concise, and you can keep track of what is happening.&lt;/p&gt;

&lt;p&gt;Uncle Bob uses an acronym to help to remember the rules of a clean test.&lt;/p&gt;

&lt;h2&gt;
  
  
  F.I.R.S.T
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;F&lt;/strong&gt;ast: Tests should be fast. If they take too long, we won't want to run the tests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I&lt;/strong&gt;ndependent: A test should never depend on another to run. It should be possible to run all the tests randomly without breaking.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;R&lt;/strong&gt;epeatable: A test should run in any environment, whether it is QA, Production or in a computer in your house.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;S&lt;/strong&gt;elf-validating: The test result should always be a boolean, informing if it passes or not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;T&lt;/strong&gt;imely: They have a right time to be written, which is before production code. If you leave it for later, you may find the test too difficult to write and not test (TDD).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Organizing your classes.
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wBaCcB_f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/99o6m83imxjld5syxm2u.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wBaCcB_f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/99o6m83imxjld5syxm2u.gif" alt="Cookie monster baking some cookie"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In java, the order that a class should be organized is this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Variables list.&lt;/li&gt;
&lt;li&gt;Public static constants.&lt;/li&gt;
&lt;li&gt;Private static variables.&lt;/li&gt;
&lt;li&gt;Instance private variables.&lt;/li&gt;
&lt;li&gt;Public Functions.&lt;/li&gt;
&lt;li&gt;Private functions called by public functions should be immediately below the calling public function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Ruby and other languages, however, all private methods must be grouped and written after all public code. Ideally, they follow the order they were called by public methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Classes should be small.
&lt;/h2&gt;

&lt;p&gt;When we say that a &lt;strong&gt;function&lt;/strong&gt; must be small, we say that it must have a few lines.&lt;/p&gt;

&lt;p&gt;When we say that a &lt;strong&gt;class&lt;/strong&gt; must be small, we mean that it must follow the principle of single responsibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  But how do we know if a class has too many responsibilities?
&lt;/h2&gt;

&lt;p&gt;The first sign that a class has too many responsibilities is its name.&lt;/p&gt;

&lt;p&gt;Classes should be named in an explanatory way so that we can understand what they do. If you need an "and", an "or", an "if" and a "but", they are already doing more than one thing.&lt;/p&gt;

&lt;p&gt;In the example below we can see that the &lt;code&gt;SuperDashboard&lt;/code&gt; class looks for the last focused component &lt;strong&gt;and&lt;/strong&gt; changes the build version, thus having two responsibilities.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SuperDashboard&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="nf"&gt;getLastFocusedComponent&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getMajorVersionNumber&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getMinorVersionNumber&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getBuildNumber&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;One way to solve this problem is to create a class for each responsibility.&lt;/p&gt;

&lt;p&gt;Some people complain that when we have many classes, it's hard to find what we want.&lt;/p&gt;

&lt;p&gt;But when we stop to think that if we have a class with many things, we will also have difficulty finding what we are looking for. When we have many classes, it is as if we have several drawers labeled with their contents. But when we have just a few classes we have few stuffed drawers.&lt;/p&gt;

&lt;h2&gt;
  
  
  A little about cohesion
&lt;/h2&gt;

&lt;p&gt;In an ideal world, the functions inside classes should speak only with the variables created in that class, but as this is impossible, we need our classes to be cohesive.&lt;/p&gt;

&lt;h2&gt;
  
  
  New classes as a result of refactoring.
&lt;/h2&gt;

&lt;p&gt;We can observe the existence of a flow when we are refactoring a code, which can result in the creation of more classes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When we break a large function into several smaller ones, we feel the need to share variables.&lt;/li&gt;
&lt;li&gt;We want to create class instance variables so that functions don't need parameters, but why do we create instance variables that the minority of functions will use? That doesn't look very interesting.&lt;/li&gt;
&lt;li&gt;Is this not an indication that new classes could be created? : D&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Organizing a class for changes.
&lt;/h2&gt;

&lt;p&gt;We need to remember that a class must follow the "open-closed" principle, being open to extension and closed to change.&lt;/p&gt;

&lt;p&gt;Sometimes our class encapsulates a third-party library that is constantly changing. When this happens, we can solve this with a parent class to connect to the library, and create multiple child classes with the new library implementations as they happen.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;post image from &lt;a href="https://unsplash.com/@sarahdorweiler"&gt;Sarah Dorweiler&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>cleancode</category>
      <category>practices</category>
      <category>productivity</category>
      <category>books</category>
    </item>
    <item>
      <title>Identifying the dirt in our code - formatting, objects and data structures and how to handle errors</title>
      <dc:creator>Rachel Curioso :D</dc:creator>
      <pubDate>Mon, 07 Oct 2019 12:56:22 +0000</pubDate>
      <link>https://dev.to/rachc/identifying-the-dirt-in-our-code-pt2-3lj0</link>
      <guid>https://dev.to/rachc/identifying-the-dirt-in-our-code-pt2-3lj0</guid>
      <description>&lt;p&gt;This post is the second in a series of 3 about clean code.&lt;/p&gt;

&lt;p&gt;We will talk about code formating, object and data structure, and how to handle errors.&lt;/p&gt;

&lt;p&gt;I know that some of those things may seem silly, but a lot of interesting things are discussed within these topics.&lt;/p&gt;

&lt;p&gt;In formating I talk, among other things, about &lt;strong&gt;the best order for your functions and variables&lt;/strong&gt;. When I talk about object and data structure, I talk about how you &lt;strong&gt;protect the inside of your object&lt;/strong&gt;. And about errors, I talk about &lt;strong&gt;prepare your code to deal with problems&lt;/strong&gt; when they happen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/rachc/identifying-the-dirt-in-our-code-2f3h"&gt;In the first post&lt;/a&gt; I talked about &lt;strong&gt;names, functions and comments&lt;/strong&gt; I advise you to read (: (Or not! You are not obliged! Rebel!)&lt;/p&gt;

&lt;p&gt;The intention is to finish the series in the next post, talking about how to work with third-party code, how to organize your unit test and, finally, how to organize better your classes.&lt;/p&gt;

&lt;p&gt;Let's Go&lt;/p&gt;

&lt;h1&gt;
  
  
  Formatting
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1pyqh2bj4345rq5iuhe9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1pyqh2bj4345rq5iuhe9.gif" alt="Beyoncé Video Formation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we look at a code that doesn't have good formatting, the first impression is that all code is not very good, and we end up not taking it seriously.&lt;/p&gt;

&lt;p&gt;When the subject is formatting, we look not only at the indentation but also at how code blocks are organized.&lt;/p&gt;

&lt;p&gt;We can divide the subject &lt;code&gt;formatting&lt;/code&gt; into two parts: Vertical formatting and horizontal formatting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Vertical Formatting
&lt;/h2&gt;

&lt;p&gt;A newspaper story begins with a good headline that tells the reader what the story is about. The first paragraph is the equivalent of a synopsis, so you can get an idea of the tone of the story and as you read it, you'll discover more details.&lt;/p&gt;

&lt;p&gt;A good code should be like that too. The module name should be clear so people know what it is about, soon after comes higher-level concepts along with algorithms, and as the code unfolds, there should come lower-level functions with the source code details.&lt;/p&gt;

&lt;p&gt;Ideally, the code should be divided into modules, with a space between each module, and they should follow an order, for the code to tell a story.&lt;/p&gt;

&lt;p&gt;First, the variables must be declared and then the dependent functions. The only thing that shouldn't follow this rule are functions that have a conceptual affinity.&lt;/p&gt;

&lt;p&gt;In the example below, I show the order that functions should be called, and when I speak of functions that have a conceptual affinity, I speak of &lt;code&gt;chocolate?&lt;/code&gt; and &lt;code&gt;not_chocolate?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(People, focus on the order that the functions appear and not the names)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;something&lt;/span&gt;
&lt;span class="n"&gt;somewhere&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;some_place&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ice_cream&lt;/span&gt;
  &lt;span class="n"&gt;banana&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;something_1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;cheese&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;something_2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;banana&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;something_3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"something"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;chocolate?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;something_3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;avocado&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;something_3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;chocolate?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;something_4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;#some code&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;chocolate?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;something_4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;#Another code&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;avocado&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;something_5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;#One more code&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;cheese&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;something_6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;#One more of another code&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Still talking about the order in which things appear in code, remember that loop control variables are declared within the loop itself:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for(var i = 0; i &amp;gt; something; i++)&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Horizontal
&lt;/h2&gt;

&lt;p&gt;It's a good practice to avoid very long lines of code. Some people like to use a limit of 80 characters, others 100, and that's fine, but over 120 characters may be a sign of carelessness.&lt;/p&gt;

&lt;p&gt;Another good practice is to group things that we don't want to separate and not separate things that we want to group together. It seems obvious to say so, but sometimes this is not what happens:&lt;/p&gt;

&lt;p&gt;in the expression &lt;code&gt;def something(args)&lt;/code&gt; we want that &lt;code&gt;something&lt;/code&gt; and &lt;code&gt;(args)&lt;/code&gt; to be together because they are the same thing. If we put a space between them, &lt;code&gt;def something (args)&lt;/code&gt;, we get an idea that they are different things.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;int something = another_something&lt;/code&gt; we are talking that &lt;code&gt;something&lt;/code&gt; and &lt;code&gt;another_something&lt;/code&gt; are different things, and when we write &lt;code&gt;int something=another_something&lt;/code&gt;, we lose a little of this distinction.&lt;/p&gt;

&lt;p&gt;Another important thing to note about horizontal formatting is how we align our variables.&lt;/p&gt;

&lt;p&gt;Somethime we feel the need to align this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Socket&lt;/span&gt;        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;InputStream&lt;/span&gt;   &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;OutputStream&lt;/span&gt;  &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;       &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;      &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt;       &lt;span class="n"&gt;hasError&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;          &lt;span class="n"&gt;requestParsingTimeLimit&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;          &lt;span class="n"&gt;requestedProgress&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But we have three problems when we do this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We take away the intention of what matters. In the example above, we end up not looking for the type of the variable. We only look at your name&lt;/li&gt;
&lt;li&gt;Usually, if we use the editor tools to format our code, it takes all these spaces, and we lost our formatting&lt;/li&gt;
&lt;li&gt;If we realize that we should align the variables, it's a sign that we have too many variables and, probably, we can divide what we have in more pieces.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This the most appropriate way to align the variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Socket&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;InputStream&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;OutputStream&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;hasError&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;requestParsingTimeLimit&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;requestedProgress&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Objects and data structure
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmb0hjxrxjycn730qbei6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmb0hjxrxjycn730qbei6.gif" alt="Miss Piggy, from muppets, forcing the prison bars to get out"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we like private variables, why we always write getters and setters in our code? How to handle access to our classes?&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Abstraction
&lt;/h2&gt;

&lt;p&gt;When we use a method or a class, we don't need to know how they are implemented. What interests us is their returns.&lt;/p&gt;

&lt;p&gt;In the example below, we can see the difference between a class with public variables and an interface with various access policies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nf"&gt;Point&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interface with various access policies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nf"&gt;Point&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
  &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getX&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getY&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setCartesian&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getR&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getTheta&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setPolar&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;theta&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that on the second example, we don't have any method like &lt;code&gt;setX&lt;/code&gt; or &lt;code&gt;setY&lt;/code&gt;. If we want to create a point, we need to pass both like a couple, using &lt;code&gt;setCartesian&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The first example forces us to deal with each variable separately, even though they are a couple.&lt;/p&gt;

&lt;p&gt;Hide the implementation it's not only a matter of hiding variables. It's a matter of abstraction. A class is not just a tangle of getters and setters. They manipulate data and that should be their essence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Structure or Objects?
&lt;/h2&gt;

&lt;p&gt;Objects hide behind abstractions, and its public functions serve to manipulate their data.&lt;/p&gt;

&lt;p&gt;As a counterpoint, in data structures, everything is exposed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Shape&lt;/span&gt;
  &lt;span class="no"&gt;Point&lt;/span&gt; &lt;span class="n"&gt;top_and_left&lt;/span&gt;
  &lt;span class="n"&gt;side&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Shape&lt;/span&gt;
  &lt;span class="no"&gt;Point&lt;/span&gt; &lt;span class="n"&gt;center&lt;/span&gt;
  &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Geometry&lt;/span&gt;
  &lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.141592&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;geometric_form&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;geometric_form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_a?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Square&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="c1"&gt;# calculate the area of a square&lt;/span&gt;
    &lt;span class="k"&gt;elsif&lt;/span&gt; &lt;span class="n"&gt;geometric_form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_a?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="c1"&gt;# calculate the area of a circle using PI&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we have a data structure with this problem:&lt;/p&gt;

&lt;p&gt;Any new &lt;code&gt;shape&lt;/code&gt; that we want to create, we need to go to the &lt;code&gt;Geometry&lt;/code&gt; class and add one more &lt;code&gt;if&lt;/code&gt; to the &lt;code&gt;area&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;In this particular case, we could use inheritance to implement the &lt;code&gt;area&lt;/code&gt; function in each object, not the &lt;code&gt;Geometry&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;In Object Orientation, it's easy to create new classes without modifying functions, but on the other hand, it's difficult to add new functions in Object Orientated Programming (OOP), as this impacts all the child classes.&lt;/p&gt;

&lt;p&gt;Occasionally, it's more important to add new functions than to create child classes. In such cases, don't be afraid to use procedural code.&lt;/p&gt;

&lt;p&gt;The important is not to mix data structure with objects, because in that case, we would have the worst of both worlds. The result is structured that are hard to inherit, and difficult to modify existing functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  The law of Demeter
&lt;/h2&gt;

&lt;p&gt;The law of Demeter states that a module must not know the inside of the module it handles.&lt;/p&gt;

&lt;p&gt;A method shouldn't invoke methods or objects of another class.&lt;/p&gt;

&lt;p&gt;Looking at the example bellow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;outputDir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctxt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOptions&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getScratchDir&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getAbsolutePath&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;ctxt&lt;/code&gt; was an object, to access &lt;code&gt;getAbsolutePath()&lt;/code&gt; it would need the &lt;code&gt;getOptions()&lt;/code&gt; return in order to work. That is, the functions are dependent on each other in order to function.&lt;/p&gt;

&lt;p&gt;We could solve this problem this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Options&lt;/span&gt; &lt;span class="n"&gt;opts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctxt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOptions&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="n"&gt;scracthDir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getScratchDir&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;outputDir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stracthDir&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAbsolutePath&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, we should be aware that if the example above were a data structure, it wouldn't be a problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;full_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_downcase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first example given, we have an information access problem and we have a violation of Demeter's law. But in the example above, we are transforming a piece of information, and there is no problem with this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data transfer
&lt;/h2&gt;

&lt;p&gt;When we talk about data transfer, we are referring to objects that have only one variable, but no method.&lt;/p&gt;

&lt;p&gt;Some people like to leave all attributes private, and access them through &lt;code&gt;getters&lt;/code&gt; and &lt;code&gt;setters&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The only real advantage in this is to makes the code more object-oriented, and ready for possible growth.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;getAge&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Active Record
&lt;/h2&gt;

&lt;p&gt;The active record works as direct database translations. Usually, they are &lt;code&gt;Data Transfer Objects&lt;/code&gt; with some navigational methods like &lt;code&gt;save&lt;/code&gt; or &lt;code&gt;find&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It turns out that when we try to add business rules and make it more robust, we end up creating a hybrid structure, half object, half data structure that, as we saw above, is always bad.&lt;/p&gt;

&lt;p&gt;The solution to this problem is to treat the Active Record as a data structure and create separate objects that have business rules and other internal data.&lt;/p&gt;

&lt;h1&gt;
  
  
  Error Handling
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fibxnuvwcqun8b6eehp5g.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fibxnuvwcqun8b6eehp5g.gif" alt="Several error popups on a windows screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Dealing with error is important, but when error handling leaves logic obscure and confusing, it is being done wrong.&lt;/p&gt;

&lt;p&gt;In the old days, people tried to check for all kinds of mistakes at once, and that brought many problems besides those mentioned above.&lt;/p&gt;

&lt;p&gt;Another problem in this approach is that you need to think in all scenarios before writing code, which not only impairs readability, but it is easy to forget about handling the error later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"scenery 01"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;#...&lt;/span&gt;
&lt;span class="k"&gt;elsif&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"scenery 02"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;#(...)&lt;/span&gt;
&lt;span class="k"&gt;elsif&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"scenery 234334134134"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;#...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, it would be interesting to encapsulate the code with a more generic error handling, but clear enough that you can find the root of the problem. For example, if you have a class that fetches users on another system via HTTP, and you have an HTTP error, you don't want to have a generic HTTP exception, but an exception that says you couldn't find the user.&lt;/p&gt;

&lt;p&gt;When we throw exceptions, we need to be careful to make the messages clear so that you, the developer, know how to identify the problem after it occurs.&lt;/p&gt;

&lt;p&gt;Remember that not every error is an exception. Many errors can be treated as normal code flow. Well done logging is often better than throwing an exception.&lt;/p&gt;

&lt;p&gt;One tip to writing error-handling code is to write your &lt;code&gt;try-catch&lt;/code&gt; first.&lt;/p&gt;

&lt;p&gt;Think that all that is inside the &lt;code&gt;try-catch&lt;/code&gt; block should be independent and decoupled from the rest of the application, because if that piece of code goes wrong, probably we don't want to broke everything else.&lt;/p&gt;

&lt;p&gt;Writing the &lt;code&gt;try-catch&lt;/code&gt; block first helps to make it independent.&lt;/p&gt;

&lt;p&gt;It is important to note that not all places need exception handling: those most in need are those where there is uncertainty about the outside world, such as network connections, database interactions, etc.&lt;/p&gt;

&lt;p&gt;Another point that needs to be raised is knowing where in the stack you will want to leave the error. The higher above the stack and closer to the user, the better, because the farther from the interaction layer you leave, the harder it is to find the error. However, leaving it close to the user is different from popping the error for the user to see. Sometimes we don't want the user to know what went wrong, but we always need to know what the user did to make such an error happen.&lt;/p&gt;

&lt;p&gt;This is a very long topic &lt;a href="http://exceptionalruby.com/" rel="noopener noreferrer"&gt;that would make a book&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A preview about dealing with someone else's code
&lt;/h2&gt;

&lt;p&gt;When we use third-party code, it is best not to treat its errors directly in your code.&lt;/p&gt;

&lt;p&gt;The ideal is to encapsulate this lib in a class of its own and handle the error inside.&lt;/p&gt;

&lt;h2&gt;
  
  
  Null/Nil
&lt;/h2&gt;

&lt;p&gt;When we work with &lt;code&gt;null&lt;/code&gt; in our code, we create a difficult problem to debug.&lt;/p&gt;

&lt;p&gt;When something goes wrong, it throws a generic exception, informing us that something wrong is not right. This exception is so generic that finding its root is very difficult.&lt;/p&gt;

&lt;p&gt;Sometimes we need to check if something doesn't exist and we are tempted to verify if &lt;code&gt;something == null&lt;/code&gt;. In this case, look to see if there are no other ways of checking. Probably there is a way.&lt;/p&gt;

&lt;p&gt;If it's a list, we can verify if the list is empty. If it's a String, we can verify if it's blank and so on.&lt;/p&gt;

&lt;p&gt;And, the most important, don't pass null as an argument. When we do this, the chances of receiving a &lt;code&gt;null pointer exception&lt;/code&gt; (and equivalents) are much greater.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;cover image by &lt;a href="https://unsplash.com/@jazminantoinette" rel="noopener noreferrer"&gt;Jazmin Quaynor&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>cleancode</category>
      <category>practices</category>
      <category>productivity</category>
      <category>books</category>
    </item>
    <item>
      <title>Identifying the dirt in our code -  names, functions, and comments</title>
      <dc:creator>Rachel Curioso :D</dc:creator>
      <pubDate>Sun, 29 Sep 2019 00:17:21 +0000</pubDate>
      <link>https://dev.to/rachc/identifying-the-dirt-in-our-code-2f3h</link>
      <guid>https://dev.to/rachc/identifying-the-dirt-in-our-code-2f3h</guid>
      <description>&lt;p&gt;The problem of having code that is not very clean is that, as we maintain it, the code will get &lt;strong&gt;more and more complex&lt;/strong&gt; and its understanding become compromised.&lt;/p&gt;

&lt;p&gt;When I talk about complex code, I don't mean business rules, but the complexity of &lt;strong&gt;understanding and code legibility&lt;/strong&gt;. I'm talking about spending some time trying to understand what is &lt;code&gt;p&lt;/code&gt; or what the &lt;code&gt;read(p, i)&lt;/code&gt;function do when you don't have any context. What does it read? what are &lt;code&gt;p&lt;/code&gt; and &lt;code&gt;i&lt;/code&gt;? Aaaaahhh /o\&lt;/p&gt;

&lt;p&gt;When we are in a place that we spend more time trying to understand an old code than thinking on the new code implementation, it could be a sign that something could be better. &lt;strong&gt;Spending more time in a task than we liked to, leave us frustrated&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When is difficult to maintain code, we stop to care about its quality and consequently we write "bad" code. We leave the code even more complex and, the more complex it gets, and more difficult it is to maintain. Our productivity drops almost to 0 and it becomes risky to fix a problem without developing 3 more on this process. &lt;strong&gt;We become afraid to touch a code that already works "magically"&lt;/strong&gt; (because we, obviously don't have a clue about what's happening)&lt;/p&gt;

&lt;p&gt;The consequences of this are code so difficult to maintain that &lt;strong&gt;companies end up closing&lt;/strong&gt;. I've seen it happen once. The only product had several bugs, it was impossible to fix a bug without creating a few more and the customer satisfaction has reached a level that they didn't want use the product anymore, and the company closed&lt;/p&gt;

&lt;h2&gt;
  
  
  If we know that is so harmful, why we do this?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Hurry.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Usually, we have such tight deadlines that we deliver our tasks poorly to skip to the next.&lt;/p&gt;

&lt;h2&gt;
  
  
  I want to avoid this from happening. How I do this?
&lt;/h2&gt;

&lt;p&gt;This is the &lt;strong&gt;first part&lt;/strong&gt; of a series about clean code. The idea, in this first topic, is talk about naming things, write better functions and a little about comments.&lt;/p&gt;

&lt;p&gt;Next, I intend to talk about formatting, objects and data structures, how to handle errors, about boundaries (how to deal with another's one code), unit testing and how to organize your class better.&lt;/p&gt;

&lt;p&gt;I know that it'll be missing an important topic about code smells, but this is a huge topic that has the potential for another series. &lt;/p&gt;

&lt;p&gt;So, let's begin?&lt;/p&gt;

&lt;h1&gt;
  
  
  Give good names!
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Hi4-8btP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://rachc.dev/images/cleancode01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Hi4-8btP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://rachc.dev/images/cleancode01.gif" alt='gif escrito "hello, my name is"'&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Use names that show what your code does:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;int d&lt;/code&gt; could be &lt;code&gt;int days&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;instead &lt;code&gt;while x == 7&lt;/code&gt;, we could use &lt;code&gt;while time == DAYS_OF_WEEK&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid misinformation.
&lt;/h2&gt;

&lt;p&gt;if &lt;code&gt;accountsList&lt;/code&gt; is not a list, it should have a better name.&lt;/p&gt;

&lt;p&gt;Varible that has very subtle name differences also fall in this topic, such as &lt;br&gt;
&lt;code&gt;VeryNiceControllerToDealWithStrings&lt;/code&gt; and &lt;code&gt;VeryNiceControllerToStoreStrings&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;How long did you take to spot the difference between the two variables?&lt;/p&gt;
&lt;h2&gt;
  
  
  Make a good distinction of your variables.
&lt;/h2&gt;

&lt;p&gt;If we have two variables, one called &lt;code&gt;banana&lt;/code&gt; e another called &lt;code&gt;theBanana&lt;/code&gt;, how can we differentiate which one is which? This same rule applies for &lt;code&gt;CustomerData&lt;/code&gt;, &lt;code&gt;Costumer&lt;/code&gt; and &lt;code&gt;CostumerInfo&lt;/code&gt;. What's the difference between then?&lt;/p&gt;
&lt;h2&gt;
  
  
  Use names that you can pronounce
&lt;/h2&gt;

&lt;p&gt;It facilitates internal team communication. If you pair, you don't need to refer to a variable as &lt;code&gt;wpdw&lt;/code&gt;. A &lt;code&gt;workDaysPerWeek&lt;/code&gt; sounds better (:&lt;/p&gt;
&lt;h2&gt;
  
  
  Use searchable names
&lt;/h2&gt;

&lt;p&gt;Have you ever tried to use &lt;code&gt;ctrl+f&lt;/code&gt; on a variable that you couldn't remember the name? Or that it was so common that you just couldn't? Have you tried to search for a variable called &lt;code&gt;f&lt;/code&gt;?&lt;/p&gt;
&lt;h2&gt;
  
  
  Avoid prefixes!
&lt;/h2&gt;

&lt;p&gt;It's another thing that makes the search difficult. Not only the search but mainly autocomplete. if the variables start with &lt;code&gt;icsd&lt;/code&gt;, like &lt;code&gt;icsd_name&lt;/code&gt; or &lt;code&gt;icsd_date&lt;/code&gt; we'll have a hard time to find what we are looking for.&lt;/p&gt;
&lt;h2&gt;
  
  
  Avoid mind mapping
&lt;/h2&gt;

&lt;p&gt;When we use a huge quantity of variables that don't make much sense, we need to memorize what they are meanwhile we are trying to figure out what the code does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;(this code block has another problem with so much each inside each inside each insi.... but we'll talk about that later)&lt;/p&gt;

&lt;h2&gt;
  
  
  Class names
&lt;/h2&gt;

&lt;p&gt;Should be nouns, and not verbs, because classes represent concrete objects. They are the representation of things.&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods names
&lt;/h2&gt;

&lt;p&gt;Should be verbs, and not nouns, because methods represent actions that objects must do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add context!
&lt;/h2&gt;

&lt;p&gt;If you bump on a variable &lt;code&gt;state&lt;/code&gt; alone, probably will have a hard time to figure out what it means.&lt;/p&gt;

&lt;p&gt;But if this variable is near &lt;code&gt;street&lt;/code&gt; and &lt;code&gt;number&lt;/code&gt; it's easier to understand what it does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add context, but not for free.
&lt;/h2&gt;

&lt;p&gt;Let's suppose that we are working on a project called &lt;code&gt;chocolate ice cream&lt;/code&gt;. We don't need to add &lt;code&gt;CIC&lt;/code&gt; in front of all variables.&lt;/p&gt;

&lt;p&gt;At the same time, inside a &lt;code&gt;User&lt;/code&gt; class, we don't need to call all the variables &lt;code&gt;userAdress&lt;/code&gt;, &lt;code&gt;userAge&lt;/code&gt; or &lt;code&gt;userFavoriteColor&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Functions
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SeZC0sb7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://rachc.dev/images/cleancode02.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SeZC0sb7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://rachc.dev/images/cleancode02.gif" alt="The swedish chef drumming on the pan"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  They should be small
&lt;/h2&gt;

&lt;p&gt;They should be small and readable, with few nests. In the ideal world, they should be 1 or 2 levels of indentation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do only one thing.
&lt;/h2&gt;

&lt;p&gt;They should make only one thing and that's it.&lt;/p&gt;

&lt;p&gt;But how do I know that it does only one thing?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;verifyNameAndLogout&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;rightName?&lt;/span&gt;
    &lt;span class="n"&gt;showMessage&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;logout&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you look at the code, it seems that it does 3 things&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Verify if the name is right&lt;/li&gt;
&lt;li&gt;If yes, it displays a message&lt;/li&gt;
&lt;li&gt;logout, regardless the name is right or wrong.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We understand that it does what is proposed in only one level inside the function, so we can say that this function do only one thing.&lt;/p&gt;

&lt;p&gt;The only modification that we could do in this code would be relocating the if statement to another function, but in this case, it would be only a relocation, without any additional advantage&lt;/p&gt;

&lt;p&gt;Functions can't be divided into sections. If you can divide, it's a sign that it could be more than one function&lt;/p&gt;

&lt;h2&gt;
  
  
  One abstraction level per function
&lt;/h2&gt;

&lt;p&gt;When we talk about abstraction levels, we can classify the code in 3 levels:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;high: &lt;code&gt;getAdress&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;medium: &lt;code&gt;inactiveUsers = Users.findInactives&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;low: &lt;code&gt;.split(" ")&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The ideal is not to mix the abstraction levels in only one function. When we mix, the function becomes a little harder to understand, and we can't read the code step by step, from top to bottom, like an article&lt;/p&gt;

&lt;p&gt;The best scenario is that you can read code from top to bottom, like a narrative.&lt;/p&gt;

&lt;p&gt;ex:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To include the setup and the teardown, we first include the setups, then we include the page content, and after that, the teardown.&lt;/li&gt;
&lt;li&gt;To include the setup, we add the setup suite and then the default setup.&lt;/li&gt;
&lt;li&gt;To include the test setup, we search for the hierarchy...&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;(and so on...)&lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid switch statements and excessive if/else
&lt;/h2&gt;

&lt;p&gt;The motive that we should avoid extensive switch/if-else is that beside them beeing to big and doing many things, they violate the open-closed principle of object-oriented programing or. also known as the &lt;code&gt;O&lt;/code&gt; in S.O.L.I.D .&lt;/p&gt;

&lt;p&gt;One entity should be closed for modification and open for expansion. When I talk about entity, I mean class, module, function and so on&lt;/p&gt;

&lt;p&gt;When there are several if/else/switch in a single module, we have to modify the code if we want to add any extra case, what makes the code more fragile and difficult to test.&lt;/p&gt;

&lt;p&gt;Sometimes we need to verify differents situations and take different actions depending on the situation.&lt;/p&gt;

&lt;p&gt;One way to decrease the switch/if/else is to use polymorphism.&lt;/p&gt;

&lt;p&gt;Using a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;foreach&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;zoo&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typeof&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"dog"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;bark&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"cat"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;meow&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;this could be refactored in this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;foreach&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;zoo&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;speak&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;On this example, under the hood, we created an animal class, that has the method &lt;code&gt;speak&lt;/code&gt;. Then we created a dog and a cat object, we give then the behavior to speak and that's it.&lt;/p&gt;

&lt;p&gt;Sometime, polymorphism could be a little too much to solve this problem. Sometimes we rearrange the logic, create a few more function and fix our problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Less arguments is the way to go
&lt;/h2&gt;

&lt;p&gt;When we talk about arguments, Less is more&lt;/p&gt;

&lt;p&gt;When we have many arguments, the complexity of our function and, consequently, our tests greatly increases.&lt;/p&gt;

&lt;p&gt;Another thing that happens is mistaking the argument order when we call the function.&lt;/p&gt;

&lt;p&gt;Occasionally we can decrease the argument size creating a class.&lt;/p&gt;

&lt;p&gt;A function with several arguments could be transformed in a class, and this function could be a method in this class. The arguments, much of the time, could be their properties and, this way, we don't need to pass any argument.&lt;/p&gt;

&lt;p&gt;In some cases, we don't need this much, so we could use other ways to decrease the number of arguments, like grouping then.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Circle makeCircle(double x, double y, double radius)&lt;/code&gt; could be transformed in &lt;code&gt;Circle makeCircle(Point center, double radius)&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Function are verbs, arguments nouns
&lt;/h2&gt;

&lt;p&gt;When we name function as verbs, and arguments as nouns, it becomes more legible and easier to understand the behavior that we expect, like &lt;code&gt;write(name)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The argument order is also important so you don't forget when you'll use the function.&lt;/p&gt;

&lt;p&gt;Just imagine to get the order right if the arguments were changed: &lt;code&gt;assertExpectedEqualsActual(expected, actual)&lt;/code&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid flag as arguments.
&lt;/h2&gt;

&lt;p&gt;Avoid passing a boolean as an argument, because it makes the refactor hard.&lt;/p&gt;

&lt;p&gt;Try to verify the truth or falseness when you call the function, and not inside it.&lt;/p&gt;

&lt;h2&gt;
  
  
  A function shouldn't have collateral effects.
&lt;/h2&gt;

&lt;p&gt;If we have a function called &lt;code&gt;checkPassword&lt;/code&gt;, it shouldn't do a &lt;code&gt;login&lt;/code&gt; inside its body.&lt;/p&gt;

&lt;p&gt;We were not prepared to login at any time and this could cause undesirable side effects, besides test de function becomes more difficult.&lt;/p&gt;

&lt;h2&gt;
  
  
  Output arguments
&lt;/h2&gt;

&lt;p&gt;Output functions should be avoided. If a function needs to change the state of something, it should change the object.&lt;/p&gt;

&lt;p&gt;Sometimes we bump into functions like &lt;code&gt;appendFooter(s)&lt;/code&gt; and we are not sure if the argument is an input (&lt;code&gt;s&lt;/code&gt; is the footer and we are attaching it somewhere?) or output (we will attach &lt;code&gt;s&lt;/code&gt; to the footer?).&lt;/p&gt;

&lt;p&gt;If the first case is the correct one, if &lt;code&gt;s&lt;/code&gt; is the footer, the best thing to do is &lt;code&gt;objectInstance.appendFooter&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Command Query Separation
&lt;/h2&gt;

&lt;p&gt;A function should change the state of an object, or return some info about it.&lt;/p&gt;

&lt;p&gt;This is an example of a function that doesn't follow the command Query Separation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;
  &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;change&lt;/span&gt;
  &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="kp"&gt;false&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A function that changes something and returns true or false should be replaced by two. One that verifies, and another that makes the change.&lt;/p&gt;

&lt;h2&gt;
  
  
  DRY - Don't repeat yourself
&lt;/h2&gt;

&lt;p&gt;The problem with repeated code is, besides that it inflates the code, any change that we need to make we have to change in several places.&lt;/p&gt;

&lt;p&gt;However, we must be aware that too much DRY can be harmful. We often confuse code duplication with business rule duplication. Sometimes it's okay to duplicate a code as long as you're not duplicating a business rule that should be unique.&lt;/p&gt;

&lt;p&gt;There is another maxim also that says: you must write the same code a maximum of 3 times. The third time you should consider refactoring and reducing duplication.&lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid returning an error code
&lt;/h2&gt;

&lt;p&gt;When methods return error messages, it violates subtly the Command Query Separation. When we put an error inside an if block, it may cause some confusion, since it may be returning something or an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;
  &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;change&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="s1"&gt;'An error has occured'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the example above, if it works, it performs an action. If it went wrong, it pops a mistake&lt;/p&gt;

&lt;h1&gt;
  
  
  Comments
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mYBPxQef--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://rachc.dev/images/cleancode03.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mYBPxQef--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://rachc.dev/images/cleancode03.gif" alt='Dog beeing interviwed. Subtitle says "no coments"'&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Avoid&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Comments on a code usually bring more evil than good. Most of the times, a comment about what a variable does, or details of how the things work could:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Become outdated, confusing your future self (Yesterday I lost some precious time because of an outdated comment)&lt;/li&gt;
&lt;li&gt;Could be replaced for some better named variable/function/class.&lt;/li&gt;
&lt;li&gt;They pollute the code unnecessarily.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It should be clear that avoid != forbid. If you are a java programmer and is doing something public, Javadocs is important. Sometimes it's interesting to explain some code (How many time you spend trying to figure out a regex pattern when you see one?), but, in 99% of the time, comments could be avoided.&lt;/p&gt;

&lt;h1&gt;
  
  
  How I will write the code if I have to pay attention to all these rules?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jgSitoJt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://rachc.dev/images/cleancode04.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jgSitoJt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://rachc.dev/images/cleancode04.gif" alt="Surprised Pikachu"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a good article you put the ideas on a paper and goes refining the text. coding functions in the same way.&lt;/p&gt;

&lt;p&gt;First, you make it work, then you refactor.&lt;/p&gt;

&lt;p&gt;Uncle Bob, in clean code, defends that the best order to write code is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Write unit tests.&lt;/li&gt;
&lt;li&gt;Create code that works.&lt;/li&gt;
&lt;li&gt;Refactor to clean the code.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Write all the tests, make it work and, when you are sure that everything is the way that's supposed to be, refactor removing all the dirty code and applying design patterns.&lt;/p&gt;

&lt;p&gt;Particularly, I don't have the habit to write tests before the code (TDD), however, test the code before doing a refactoring is indispensable. Only then we can be sure that your code will always work, even after so much modification.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Clean code&lt;/code&gt; book is focused on bad practices. It details several problems and does not delve into ways to solve them.&lt;/p&gt;

&lt;p&gt;If you want to delve more into how to fix the dirty-code-problem, we could look into the book &lt;code&gt;refactoring&lt;/code&gt;. In this book, there are details and several different ways to eliminate the dirty in the code&lt;/p&gt;

</description>
      <category>cleancode</category>
      <category>practices</category>
      <category>productivity</category>
      <category>books</category>
    </item>
  </channel>
</rss>
