<?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: le0nidas</title>
    <description>The latest articles on DEV Community by le0nidas (@le0nidas).</description>
    <link>https://dev.to/le0nidas</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%2F46283%2F16c4e4fa-23c1-490f-a32b-b254119c0e8d.jpeg</url>
      <title>DEV Community: le0nidas</title>
      <link>https://dev.to/le0nidas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/le0nidas"/>
    <language>en</language>
    <item>
      <title>The “Tell, don’t ask” principle</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Sun, 14 Feb 2021 09:30:00 +0000</pubDate>
      <link>https://dev.to/le0nidas/the-tell-don-t-ask-principle-e37</link>
      <guid>https://dev.to/le0nidas/the-tell-don-t-ask-principle-e37</guid>
      <description>&lt;p&gt;This is one of my favorite principles because it is easy to spot when violated and because it helps in having proper API surfaces when applied.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tell, don’t ask
&lt;/h4&gt;

&lt;p&gt;In essence this principle proposes that instead of &lt;em&gt;asking&lt;/em&gt; from an instance for its values in order to decide how the &lt;strong&gt;same instance&lt;/strong&gt; will execute something, just &lt;em&gt;tell&lt;/em&gt; the instance to execute it. It knows its own state, it can make its own decisions!&lt;/p&gt;

&lt;h5&gt;
  
  
  Asking
&lt;/h5&gt;

&lt;p&gt;By &lt;em&gt;asking&lt;/em&gt; we actually refer to accessing many of a class’s properties. For example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;in the code above we &lt;em&gt;ask&lt;/em&gt; the task to provide the values of three of its properties in order to decide if we are going to close it or not.&lt;br&gt;&lt;br&gt;
In the process we (a) might had to expose those properties just for this code (creation date and subscribers could be private) and (b) inevitably leaked business logic that involves a task (when a task is eligible for closing).&lt;/p&gt;
&lt;h5&gt;
  
  
  Telling
&lt;/h5&gt;

&lt;p&gt;Lets change the code in order to &lt;em&gt;tell&lt;/em&gt; the task to close itself if possible:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;There are two things that we gain from this change:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;we moved the “closing” logic in the &lt;code&gt;Task&lt;/code&gt; itself which allows us to contain future logic alterations in just one place.&lt;/li&gt;
&lt;li&gt;the class’s API exposes only the necessary helping us to avoid accidental couplings.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>designprinciples</category>
      <category>telldontask</category>
    </item>
    <item>
      <title>LSP and ISP: The LI of SOLID</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Sun, 07 Feb 2021 17:34:30 +0000</pubDate>
      <link>https://dev.to/le0nidas/lsp-and-isp-the-li-of-solid-d6m</link>
      <guid>https://dev.to/le0nidas/lsp-and-isp-the-li-of-solid-d6m</guid>
      <description>&lt;p&gt;I am under the impression that every time I come across a SOLID post, LSP and ISP are not given the same amount of attention as the other three. I guess its because they are the easiest to grasp but make no mistake, they are also the easiest to violate!&lt;/p&gt;

&lt;h4&gt;
  
  
  Liskov Substitution Principle (LSP)
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;In essence, LSP proposes that we create sub-classes that can be used &lt;strong&gt;wherever&lt;/strong&gt; their parents are being used without breaking or changing the client’s behavior.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means that in the code snippet below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;we should be able to pass instances from all sub-classes of &lt;code&gt;SoftwareEngineer&lt;/code&gt; without worrying that &lt;code&gt;calculateSeniority&lt;/code&gt; will break or change the behavior of &lt;code&gt;printSeniority&lt;/code&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  Ways that we tend to violate LSP
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;By throwing an exception
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the obvious one. If, for example, the subclass adds a check that will eventually throw an exception then the client will break.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;By returning undocumented results
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, the subclass returns something that the its parent never will.&lt;br&gt;&lt;br&gt;
This forces the client to know about the subclass which makes the code less scalable.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;By having side effects&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This is the subtle one since it does not change the client’s code but it does change the expected behavior. &lt;code&gt;printSeniority&lt;/code&gt; is expected to make a calculation and then print the result but know it also makes a network call!&lt;/p&gt;

&lt;h4&gt;
  
  
  Interface Segregation Principle (ISP)
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;In essence, ISP proposes that interfaces should not play the role of “&lt;em&gt;methods bucket”&lt;/em&gt; because eventually there will be a client that will not need to implement all of them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means that interfaces like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;should break in more meaningful parts and allow every client to implement only the part that it needs:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Its worth mentioning that this way, in addition from avoiding ISP violation we also:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Keep our code from violating the SRP.
In the first implementation, our cache depends in two things so it has more that one reasons to change (ex: a new parameter in the post method would force us to change our cache too)&lt;/li&gt;
&lt;li&gt;Keep our code from violating the LSP. By having one interface, the first implementation of our cache couldn’t be used in code that expects repositories since its API methods would break the client.&lt;/li&gt;
&lt;li&gt;Keep our code clean and scalable (the cache does not have to know about talking to the API)&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Ways that we tend to violate ISP
&lt;/h5&gt;

&lt;p&gt;Although there is not much to say here, since the only way to do it is by creating those buckets we mentioned above, beware of the creation since it comes in two flavors.&lt;br&gt;&lt;br&gt;
The first is by having all methods in one file. Easy to catch it in a PR review. The second though is by having an hierarchy in our interfaces.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;By the time there is an implementation that does not need all methods, it might too late!&lt;/p&gt;

</description>
      <category>designprinciples</category>
      <category>isp</category>
      <category>lsp</category>
      <category>solid</category>
    </item>
    <item>
      <title>Avoid primitive obsession and build your own context</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Sun, 24 Jan 2021 16:07:00 +0000</pubDate>
      <link>https://dev.to/le0nidas/avoid-primitive-obsession-and-build-your-own-context-3m60</link>
      <guid>https://dev.to/le0nidas/avoid-primitive-obsession-and-build-your-own-context-3m60</guid>
      <description>&lt;p&gt;Primitive obsession is a known code smell that describes the usage of primitives for representing domain values. For example:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this class both the user’s name and age are being represented by a string and an int respectively.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How else could we represent a name and an age?&lt;/em&gt;&lt;br&gt;&lt;br&gt;
Well, the primitives are the correct ones but not for direct usage. We can build value objects upon them and encapsulate both the meaning of the value and the business logic:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;So what? You just wrapped a primitive and moved the check.&lt;/em&gt;&lt;br&gt;&lt;br&gt;
True but now we have:&lt;/p&gt;
&lt;h5&gt;
  
  
  1. A reusable object
&lt;/h5&gt;

&lt;p&gt;When the time comes and we need to have a new named entity we just use the class above and we can be sure that the business logic will follow along and be concise in the entire project.&lt;/p&gt;
&lt;h5&gt;
  
  
  2. Always valid instances
&lt;/h5&gt;

&lt;p&gt;Whenever we see an instance of name or age we are certain that the instance holds a valid value. This means that an entity that consists from those value objects can only create valid instances and this means that the code that uses those entities (and value objects) does &lt;strong&gt;not&lt;/strong&gt; need to have unnecessary checks. Cleaner code.&lt;/p&gt;
&lt;h5&gt;
  
  
  3. Code that scales more easily
&lt;/h5&gt;

&lt;p&gt;Lets say that our business logic changes and we need to support users with invalid names too but without the need to deal with the name itself.&lt;br&gt;&lt;br&gt;
Having a value object can help implement the change easily. We just change the &lt;code&gt;Name&lt;/code&gt; class. All other code remains the same:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h5&gt;
  
  
  4. Code that is more robust
&lt;/h5&gt;

&lt;p&gt;Lets say that our entities have the notion of an id and that after a few years the underline value needs to change from an integer to a long.&lt;br&gt;&lt;br&gt;
By having a value object to represent the &lt;code&gt;Id&lt;/code&gt; all changes will take place in the outer layers of our architecture where we load/fetch ids from databases/network and create the id instances. The rest of project will remain untouched, especially the domain layer that holds our business logic.&lt;br&gt;&lt;br&gt;
If we had chosen the path of having an integer property in every entity then all of our entities, and the code that uses them, would need to change too.&lt;/p&gt;

&lt;h5&gt;
  
  
  5. Code that is more readable and reveals its usage
&lt;/h5&gt;

&lt;p&gt;I’ve written a couple of posts in the past that showcase both the &lt;a href="https://le0nidas.gr/2019/08/15/use-sealed-classes-for-better-domain-representation/"&gt;readability&lt;/a&gt; aspect and the &lt;a href="https://le0nidas.gr/2019/08/26/make-your-code-reveal-its-usage/"&gt;revelation&lt;/a&gt; one.&lt;/p&gt;

&lt;h5&gt;
  
  
  6. A blueprint of our domain
&lt;/h5&gt;

&lt;p&gt;When we open our project and see files like Invoice, Price, Quantity, Amount, Currency we get an immediate feel of what this project/package deals with and what are its building blocks.&lt;br&gt;&lt;br&gt;
We consume information that we would otherwise need to dig inside each file to find out.&lt;/p&gt;

&lt;h5&gt;
  
  
  7. A context
&lt;/h5&gt;

&lt;p&gt;The final and most important part of having value objects is that now we can complete our entities and build a context for our domain. A common language that we can use to communicate with other engineers and stake holders in general.&lt;br&gt;&lt;br&gt;
Primitives are essential and they are the building blocks of a language but not of our business. The rest of company does not build its workflows upon integers and strings. It uses the businesses’ building blocks like age, name etc. It is vital that we do the same too in order to keep our code base in sync with the business.&lt;/p&gt;

</description>
      <category>codesmells</category>
      <category>domaindrivendesign</category>
      <category>primitiveobsession</category>
      <category>valueobjects</category>
    </item>
    <item>
      <title>IoC: Inversion of Control principle</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Sun, 17 Jan 2021 12:02:40 +0000</pubDate>
      <link>https://dev.to/le0nidas/ioc-inversion-of-control-principle-1363</link>
      <guid>https://dev.to/le0nidas/ioc-inversion-of-control-principle-1363</guid>
      <description>&lt;p&gt;This is the one principle that, chances are, you have applied even if you didn’t do it on purpose.&lt;br&gt;&lt;br&gt;
In essence, if you have written code that does not have any control over the execution flow, then you most probably have applied the &lt;code&gt;IoC principle&lt;/code&gt;. How?&lt;/p&gt;
&lt;h4&gt;
  
  
  IoC through design patterns
&lt;/h4&gt;

&lt;p&gt;If you have implemented the strategy or the template pattern then you have applied the IoC principle. For example, having a report generator and feeding it with the necessary filtering code.&lt;br&gt;&lt;br&gt;
All the code you write for filtering data, does not have any control over the execution’s flow. The generator is the one that decides when and if it will be invoked:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
Strategy pattern




&lt;p&gt;The same goes with the template pattern too. The code you write in the template’s hooks is being controlled by the template and you don’t get to control it:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
Template pattern




&lt;p&gt;Both of these examples might seem weird since we are the ones that wrote both the filtering code and the generator so we feel that we control the flow. The thing is that we need to separate them in our heads and observe them individually. The generator is the one that controls the flow and dictates the actions that will take place. The filtering code is one of the actions. We just write them, provide them to the generator and that’s it.&lt;/p&gt;

&lt;h4&gt;
  
  
  IoC through frameworks
&lt;/h4&gt;

&lt;p&gt;Another way of applying IoC is by using frameworks that have adopted it. Most popular are the &lt;code&gt;IoC containers&lt;/code&gt; that are used to inject dependencies.&lt;br&gt;&lt;br&gt;
Another example is the android’s framework. In android you don’t have control over an activity’s lifecycle and you simply extend the &lt;code&gt;Activity&lt;/code&gt; class and override hooks to run your code.&lt;/p&gt;
&lt;h4&gt;
  
  
  IoC vs DI
&lt;/h4&gt;

&lt;p&gt;Because of the aforementioned IoC containers, many people assume that dependency injection and IoC are the same. They are not. DI is just a way to help in applying the IoC principle. For example:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;we can change the generator and have the filtering code being injected to it by constructor. The inversion is already happening, DI is simply used to provide the extra code.&lt;/p&gt;

</description>
      <category>designprinciples</category>
      <category>ioc</category>
    </item>
    <item>
      <title>The memento design pattern in Kotlin</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Thu, 07 Jan 2021 14:08:27 +0000</pubDate>
      <link>https://dev.to/le0nidas/the-memento-design-pattern-in-kotlin-31m3</link>
      <guid>https://dev.to/le0nidas/the-memento-design-pattern-in-kotlin-31m3</guid>
      <description>&lt;p&gt;I started playing with the memento pattern for a use case I was researching when I realized that the Kotlin implementation had a, potentially, show stopper in comparison with the Java one:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I could not use a private property from within the same file&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Why was that a show stopper? We’ll see, but first, what is the memento pattern?&lt;/p&gt;

&lt;h4&gt;
  
  
  Memento pattern
&lt;/h4&gt;

&lt;p&gt;This pattern is a good way to implement a functionality that helps in restoring previous states. One good example is the undo in our text editors. You can write, edit, delete and then, by hitting undo, take each action back.&lt;/p&gt;

&lt;p&gt;There are three main ingredients for this pattern:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the &lt;strong&gt;originator&lt;/strong&gt; that holds the current state and creates snapshots of itself,&lt;/li&gt;
&lt;li&gt;the &lt;strong&gt;memento&lt;/strong&gt; that, in essence, is the snapshot with perhaps some additional metadata and&lt;/li&gt;
&lt;li&gt;the &lt;strong&gt;caretaker&lt;/strong&gt; that orchestrates the backup/restore of the state&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So in our example the originator is the editor which knows what the text is, the carets position etc, the memento a copy of those values and the caretaker can be the interface between the user and the editor.&lt;/p&gt;

&lt;h4&gt;
  
  
  Java implementation
&lt;/h4&gt;

&lt;p&gt;Lets try to have an overly simplified version of the above example in Java:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here the editor, besides manipulating text, is able to produce snapshots of its state in a way that only itself can access the state’s values. The &lt;code&gt;Memento&lt;/code&gt; class might be public, in order to allow the caretaker to handle instances of it, but its fields are private and &lt;strong&gt;only&lt;/strong&gt; the originator can read them.&lt;br&gt;&lt;br&gt;
A great way to copy something while having the smallest possible API surface and maximum privacy.&lt;/p&gt;

&lt;p&gt;As a matter of fact, here is the caretaker and its usage:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see the &lt;code&gt;UI&lt;/code&gt; uses the editor to write, edit, delete but before that it saves a backup with the editor’s state in order to restore it every time the user hits undo!&lt;/p&gt;

&lt;h4&gt;
  
  
  Kotlin implementation
&lt;/h4&gt;

&lt;p&gt;So lets move originator and memento to Kotlin. &lt;code&gt;Ctrl+Alt+Shift+K&lt;/code&gt; and boom.. we have a problem:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--So25ZD1T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://le0nidasgr.files.wordpress.com/2021/01/screenshot-from-2021-01-07-15-11-39.png%3Fw%3D342" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--So25ZD1T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://le0nidasgr.files.wordpress.com/2021/01/screenshot-from-2021-01-07-15-11-39.png%3Fw%3D342" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Kotlin, in contrast with Java, does not allow accessing private properties when in the same file.&lt;/p&gt;

&lt;p&gt;What do we do? Well we can always make the properties public:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;but this way we, indirectly, expose the editors state:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x1osdv9q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://le0nidasgr.files.wordpress.com/2021/01/screenshot-from-2021-01-07-15-20-18.png%3Fw%3D320" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x1osdv9q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://le0nidasgr.files.wordpress.com/2021/01/screenshot-from-2021-01-07-15-20-18.png%3Fw%3D320" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another way to implement the pattern is to have &lt;code&gt;Memento&lt;/code&gt; as an interface with no state for the public API and have a private implementation of it for internal usage:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;this way we do not expose any state but we do open a bit our API. We now have an interface that can be implemented and given to the &lt;code&gt;restore()&lt;/code&gt; function.&lt;/p&gt;

&lt;h5&gt;
  
  
  Inner classes
&lt;/h5&gt;

&lt;p&gt;Fortunately Kotlin has inner classes. An inner class can access the outer class’s members but, most importantly, can be extended only from within the outer class. This means that this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;checks all our boxes. We keep the originator’s state private and our overall API small!&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Test doubles: dummies, stubs, mocks, fakes</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Sat, 02 Jan 2021 16:19:52 +0000</pubDate>
      <link>https://dev.to/le0nidas/test-doubles-dummies-stubs-mocks-fakes-3c18</link>
      <guid>https://dev.to/le0nidas/test-doubles-dummies-stubs-mocks-fakes-3c18</guid>
      <description>&lt;p&gt;While testing we tend to replace some of the unit’s collaborators with &lt;em&gt;mocks&lt;/em&gt; as it is accustomed to call them. The problem with that name is that it is not accurate. The real name of those mocks is &lt;code&gt;test doubles&lt;/code&gt; and there are four of them with mock being one of the types.&lt;/p&gt;

&lt;p&gt;One reason for this misnaming is the wide usage of mocking frameworks that do not separate the types between them (I am looking at you &lt;a href="https://site.mockito.org/"&gt;mockito&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;So, lets try to define the four types and see when it is best to use them. We will be using a made up browser and its history and will not use any framework. Just theory:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Dummies
&lt;/h4&gt;

&lt;p&gt;A &lt;code&gt;dummy&lt;/code&gt; is the test double that we use whenever we know that the collaborator will not be used:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;For example in the tests above we just need to check the browser’s active URL. We know that this does not evolve the browser’s history so we pass a collaborator that does nothing on every method call.&lt;/p&gt;

&lt;h4&gt;
  
  
  Stubs
&lt;/h4&gt;

&lt;p&gt;A &lt;code&gt;stub&lt;/code&gt; is the test double that we use whenever the collaborator is being used to query values:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;For example in the test above we feed the browser with a pre-populated history since we know that the browser will need to peek for the last visited URL.&lt;/p&gt;

&lt;h4&gt;
  
  
  Mocks
&lt;/h4&gt;

&lt;p&gt;A &lt;code&gt;mock&lt;/code&gt; is the test double that we use whenever the collaborator is being used to perform an action:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;For example in the test above we need to make sure that the browser saves the provided URL to its history so we use a collaborator that can verify this behavior.&lt;/p&gt;

&lt;h4&gt;
  
  
  Fakes
&lt;/h4&gt;

&lt;p&gt;A &lt;code&gt;fake&lt;/code&gt; is the test double that we use whenever we need the collaborator to provide us a usable business logic:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;For example in the test above we need a history instance that works as expected (a simple stack) but without the hassle of having a database or using the file system.&lt;/p&gt;

&lt;h4&gt;
  
  
  Final thoughts
&lt;/h4&gt;

&lt;p&gt;Having your own test doubles per case makes the code simpler and more readable but does that mean that we should remove our mocking frameworks? In my opinion no. Having a framework saves you a lot of time and keeps things consistent, especially in big projects with lots of developers.&lt;/p&gt;

&lt;p&gt;Knowing the theory behind something is always good since it lays a common foundation for discussions and decisions. A mix of the two, framework and theory, could be achieved and help the test code in readability.&lt;br&gt;&lt;br&gt;
For example, we can keep using &lt;a href="https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#mock-java.lang.Class-"&gt;Mockito’s mock&lt;/a&gt; but name the variable &lt;code&gt;stubBlahBlah&lt;/code&gt; if is used as a stub. This way the reader will know what to expect.&lt;/p&gt;

&lt;p&gt;PS #1: &lt;a href="http://spockframework.org/"&gt;Spock&lt;/a&gt; testing framework, besides being a great tool, provides a way to separate stubs from mocks not just in semantics but in usage too (ex: you cannot verify something when using a stub)&lt;/p&gt;

&lt;p&gt;PS #2: There is another type of test double called &lt;code&gt;Spy&lt;/code&gt; which is a toned down mock that helps in keeping state when a certain behavior takes place but does not verify it.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>dummy</category>
      <category>fake</category>
      <category>mock</category>
    </item>
    <item>
      <title>SLAP: Single Level of Abstraction Principle</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Sat, 26 Dec 2020 16:30:00 +0000</pubDate>
      <link>https://dev.to/le0nidas/slap-single-level-of-abstraction-principle-4o98</link>
      <guid>https://dev.to/le0nidas/slap-single-level-of-abstraction-principle-4o98</guid>
      <description>&lt;p&gt;Even thought this acronym is quite catchy, &lt;code&gt;SLAP&lt;/code&gt; is the one principle that you don’t find many people talking about.&lt;/p&gt;

&lt;h4&gt;
  
  
  Single Level of Abstraction
&lt;/h4&gt;

&lt;p&gt;In essence, the principle proposes that each block of code should not mix &lt;strong&gt;what&lt;/strong&gt; the code does with &lt;strong&gt;how&lt;/strong&gt; it does it. Another way of thinking about it is, whenever possible, the code should describe in steps the actions that will take and then each step can elaborate on that.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;this class has one method that showcases both what it does and how it does it.&lt;/p&gt;

&lt;p&gt;If we want to &lt;code&gt;SLAP&lt;/code&gt; it we need to delegate the &lt;em&gt;how&lt;/em&gt; of each step to its own method:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;here the &lt;code&gt;invoke&lt;/code&gt; method simply describes what will happen upon its invocation. A new task will be created, then saved and finally passed to any observers.&lt;br&gt;&lt;br&gt;
For knowing how each step gets implemented we need to drill down one level. For example, creating a new task requires us to normalize the provided description and then create the task. For knowing how the normalization gets implemented we yet again move one level deeper!&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;Keep hiding how something gets implemented in new methods until you can no longer avoid it!&lt;/p&gt;

</description>
      <category>cleancode</category>
      <category>designprinciples</category>
      <category>slap</category>
    </item>
    <item>
      <title>I prefer not to use the keyword “it”</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Wed, 16 Dec 2020 15:13:17 +0000</pubDate>
      <link>https://dev.to/le0nidas/i-prefer-not-to-use-the-keyword-it-i43</link>
      <guid>https://dev.to/le0nidas/i-prefer-not-to-use-the-keyword-it-i43</guid>
      <description>&lt;p&gt;And the reason is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I want to be as explicit as possible and allow the reader of my code to have an uninterrupted flow.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Think about it. Every time you encounter the &lt;code&gt;it&lt;/code&gt; keyword you do, a quick, conversion between what you see and what it represents. Personally I do it even in very small lambdas, imagine if you are two or three lines deep in a lambda and you see an &lt;code&gt;it&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It might not look much in this simple example but read it now with an explicit value:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You don’t have to do a mental translation and it also provides some details regarding the format of username.&lt;/p&gt;

&lt;p&gt;This last part can make the code even more readable since it allows us to describe the values we use:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;this hints that (a) values list does not contain usable data and (b) the &lt;code&gt;of&lt;/code&gt; function will perform some kind of cleaning&lt;/p&gt;

</description>
      <category>cleancode</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Don’t share constants between production and test code</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Thu, 10 Dec 2020 20:40:17 +0000</pubDate>
      <link>https://dev.to/le0nidas/don-t-share-constants-between-production-and-test-code-1l7n</link>
      <guid>https://dev.to/le0nidas/don-t-share-constants-between-production-and-test-code-1l7n</guid>
      <description>&lt;p&gt;Building upon my &lt;a href="https://dev.to/le0nidas/your-tests-can-also-be-your-documentation-583b"&gt;previous post&lt;/a&gt; and the trick of being specific in the values the code respects, one pattern that I’ve noticed which can easily lead in many false positive tests is sharing a constant value between production and test code.&lt;/p&gt;

&lt;p&gt;If the test code reads the value from the production, any change that was done by mistake will not affect the test which will continue to pass!&lt;/p&gt;

&lt;h4&gt;
  
  
  21 yeas of age
&lt;/h4&gt;

&lt;p&gt;Lets say that we have two services, one checks if a customer can enter a casino and the other if she can buy alcohol. For both cases the law states that the minimum legal age is 21 years old.&lt;/p&gt;

&lt;p&gt;The code has a configuration file, a domain and two modules for each service:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see the tests consume the minimum age directly from the production code but the test suite passes, life is good.&lt;/p&gt;

&lt;p&gt;Then one day, the law changes and the minimum legal age for entering a casino drops to 20 years! Simple change, not much of a challenge for the old timers so the task is being given to the new teammate who does not know all modules yet and is also a junior software engineer.&lt;br&gt;&lt;br&gt;
She sees the test, changes the value in the name to 20, sees the config, changes the constant’s value to 20, runs the test suite, everything passes, life is good! Only that it isn’t because the casino’s software now allows selling alcohol to 20 year olds!&lt;/p&gt;
&lt;h4&gt;
  
  
  Keep them separate
&lt;/h4&gt;

&lt;p&gt;If the test code did not use the production’s code&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;then, after changing the constant’s value, the test suite would fail alerting the software engineer that something has broken forcing her to figure it out and craft another solution.&lt;/p&gt;

</description>
      <category>goodpractices</category>
      <category>testing</category>
    </item>
    <item>
      <title>Your tests can also be your documentation</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Wed, 09 Dec 2020 21:34:24 +0000</pubDate>
      <link>https://dev.to/le0nidas/your-tests-can-also-be-your-documentation-583b</link>
      <guid>https://dev.to/le0nidas/your-tests-can-also-be-your-documentation-583b</guid>
      <description>&lt;p&gt;Tests help as make sure that our code works, provide us a safety net when we need to refactor and, when having proper test names, can be a good documentation describing what the code does.&lt;br&gt;&lt;br&gt;
The last one can be especially helpful for both newcomers that need to understand the system and old timers that haven’t visited the code for a while!&lt;/p&gt;

&lt;p&gt;A couple of &lt;em&gt;tricks&lt;/em&gt; for achieving good names are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Avoid describing &lt;em&gt;how&lt;/em&gt; the code does something and try to describe what it does&lt;/strong&gt;
For example:
&lt;code&gt;calling add(item) results in calling recalculate&lt;/code&gt;
is way too specific without providing anything meaningful, or anything that we wouldn’t get from reading the code.
On the other hand:
&lt;code&gt;a recalculation of the order's value takes place every time a new item gets added&lt;/code&gt;
shares an important information about the &lt;code&gt;Order&lt;/code&gt;‘s behavior when adding an item.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoid being too abstract&lt;/strong&gt;
For example:
&lt;code&gt;a customer can buy alcohol when she is of legal age&lt;/code&gt;
can help the reader understand how the code behaves but in a documentation you need specific values.
So:
&lt;code&gt;a customer can buy alcohol when she is older than 21 years of age&lt;/code&gt;
is much better because it also provides the exact threshold that our code considers for allowing someone to buy alcohol&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>testing</category>
    </item>
    <item>
      <title>Two reasons why you should add a return type in your functions in Kotlin</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Tue, 08 Dec 2020 20:14:51 +0000</pubDate>
      <link>https://dev.to/le0nidas/two-reasons-why-you-should-add-a-return-type-in-your-functions-in-kotlin-308b</link>
      <guid>https://dev.to/le0nidas/two-reasons-why-you-should-add-a-return-type-in-your-functions-in-kotlin-308b</guid>
      <description>&lt;p&gt;Kotlin’s compiler is clever enough to figure out on its own what is the return type of a function but this does not mean that we should over use it and here is why:&lt;/p&gt;

&lt;h4&gt;
  
  
  Help the compiler to help as
&lt;/h4&gt;

&lt;p&gt;By adding a return type in a function we instruct the compiler to expect and force that type (by a compiler error). On the other hand if we allow the compiler to infer the type, if something changes in the function’s body and the return type is not the one intended by the author, the compiler will follow along thinking that we know what we are doing!&lt;/p&gt;

&lt;h4&gt;
  
  
  Help the reader [to help as]
&lt;/h4&gt;

&lt;p&gt;We write something once but it gets read multiple times. So it is our responsibility to make it as readable, explicit and quick in the eye as we can. In every case that we omit a return type the reader of our code has to do the calculations and extract the type on her own which, depending on the complexity, will take time and effort. You might say that a few seconds is not a big deal but in comparison with the zero seconds of having a type it is a lot.&lt;/p&gt;

&lt;p&gt;Also not all reading takes place in an IDE that provides hints and colorful help. Our PR reviewers will probably read out code directly from GitHub or GitLab. By making them change context to figure something out we break their flow and concentration.&lt;/p&gt;

&lt;h4&gt;
  
  
  So, when should we use it
&lt;/h4&gt;

&lt;p&gt;&lt;del&gt;Never!&lt;/del&gt; In my humble opinion the only valid place is in small (one line), private methods that either construct something, so the use of the constructor along side the &lt;code&gt;=&lt;/code&gt; sign trick the mind:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;or the return type is clearly obvious:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


</description>
      <category>cleancode</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Don’t force your objects to construct what they need</title>
      <dc:creator>le0nidas</dc:creator>
      <pubDate>Mon, 07 Dec 2020 21:29:05 +0000</pubDate>
      <link>https://dev.to/le0nidas/don-t-force-your-objects-to-construct-what-they-need-49fd</link>
      <guid>https://dev.to/le0nidas/don-t-force-your-objects-to-construct-what-they-need-49fd</guid>
      <description>&lt;p&gt;Let’s say we have an object that handles instances of &lt;code&gt;Person&lt;/code&gt;. For example &lt;code&gt;PeopleScreen&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;PeopleScreen&lt;/code&gt; renders instances of &lt;code&gt;Person&lt;/code&gt; so this should be the only format we provide to it. Let me explain.&lt;/p&gt;

&lt;h4&gt;
  
  
  Forced construction
&lt;/h4&gt;

&lt;p&gt;There is a new flow that ends in opening &lt;code&gt;PeopleScreen&lt;/code&gt; but all the information for the list of people are in a &lt;code&gt;Map&amp;lt;String, String&amp;gt;&lt;/code&gt;. There is &lt;strong&gt;no reason&lt;/strong&gt; to alter &lt;code&gt;PeopleScreen&lt;/code&gt; in order to support this new format:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
the new format is passed through an overloaded constructor but the same goes if we use a setter method




&lt;h4&gt;
  
  
  Why we shouldn’t do it
&lt;/h4&gt;

&lt;p&gt;We could argue that by doing so we tie the object with each special format making the code hard to maintain and scale but the real reason is that we violate the &lt;a href="https://en.wikipedia.org/wiki/Single-responsibility_principle"&gt;SRP&lt;/a&gt; principle since &lt;code&gt;PeopleScreen&lt;/code&gt; will have more than one reasons to change. One if something changes in the way we render and two if something changes in &lt;code&gt;Person&lt;/code&gt;‘s construction.&lt;/p&gt;

&lt;h4&gt;
  
  
  What we should do
&lt;/h4&gt;

&lt;p&gt;We should keep &lt;code&gt;PeopleScreen&lt;/code&gt; only consuming &lt;code&gt;Person&lt;/code&gt; and move all transformations to their own objects allowing a coordinator to transform and pass data around.&lt;/p&gt;

</description>
      <category>designprinciples</category>
      <category>goodpractices</category>
      <category>srp</category>
    </item>
  </channel>
</rss>
