<?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: Ricardo Costeira</title>
    <description>The latest articles on DEV Community by Ricardo Costeira (@rcosteira79).</description>
    <link>https://dev.to/rcosteira79</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%2F53589%2Fa9b1a7ed-6cf2-4df7-bf83-84067ca72dfa.JPG</url>
      <title>DEV Community: Ricardo Costeira</title>
      <link>https://dev.to/rcosteira79</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rcosteira79"/>
    <language>en</language>
    <item>
      <title>Encapsulate and abstract for future-proof software</title>
      <dc:creator>Ricardo Costeira</dc:creator>
      <pubDate>Mon, 22 Apr 2019 13:23:14 +0000</pubDate>
      <link>https://dev.to/rcosteira79/encapsulate-and-abstract-for-future-proof-software-59b3</link>
      <guid>https://dev.to/rcosteira79/encapsulate-and-abstract-for-future-proof-software-59b3</guid>
      <description>&lt;p&gt;Change will always affect your software. No matter the domain, the uses cases, the developers, or even the users. Change is the one constant in software development.&lt;/p&gt;

&lt;p&gt;This is one of the first topics addressed by the authors of the renowned &lt;a href="http://shop.oreilly.com/product/9780596007126.do"&gt;Head First Design Patterns&lt;/a&gt;. They approach it as one reason for the importance of design patterns.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“No matter how well you design an application, over time an application must grow and change or it will die.“&lt;/em&gt;&lt;br&gt;
— Head First Design Patterns, Chapter 1, page 8&lt;/p&gt;

&lt;p&gt;Along with design patterns, the authors also introduce a bundle of design principles. While the patterns are outside the scope of this article, I want to focus on the first two principles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Encapsulate what varies.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Program to interfaces, not implementations.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first principle is the basis for all design patterns, and most of them also make use of the second one. The first one dictates that if you have code that keeps changing, pull it out and isolate it. The second principle complements this through the use of interfaces.&lt;/p&gt;

&lt;p&gt;Now, a word of caution. As Vasiliy Zukanov explained in &lt;a href="https://medium.com/@techyourchance/i-havent-read-head-first-design-patterns-yet-but-i-heard-that-it-s-a-worthy-book-b53f72e9b495"&gt;this comment&lt;/a&gt;, this “interface” does not refer to the &lt;em&gt;interface&lt;/em&gt; construct seen in some OOP languages. Well, it can refer to it, but it has a broader meaning. Here, “interface” refers to a component’s external point of interaction. It is what other components can use to interact with the specific component. So, this “interface” can be an &lt;em&gt;interface&lt;/em&gt;, an abstract class, a normal class or even a function. It can be anything as long as it serves as a communication point with the component. With it, we need not know the inner details of the component. It lets us &lt;strong&gt;abstract&lt;/strong&gt; from the component’s implementation. So, whenever there’s a change, you only need to refactor the corresponding code. The outside code will never even notice it. The purpose of the principle is indeed to focus on &lt;strong&gt;what&lt;/strong&gt; the code does, and not &lt;strong&gt;how&lt;/strong&gt; it does it.&lt;/p&gt;
&lt;h2&gt;
  
  
  A ticking time bomb: Android Libraries
&lt;/h2&gt;

&lt;p&gt;The Android open source community is awesome. No matter the complexity of what you need, a library implementing it is likely to exist already. This not only makes our jobs easier but also lets us focus on the true business logic problems.&lt;/p&gt;

&lt;p&gt;Yet, things change (I know). Libraries become obsolete. Sometimes, new versions introduce breaking changes. Requirements change, and we no longer need a library. External changes force us to change our code. We’re left with a huge codebase full of deprecated dependencies or code built around them. This is where the design principles mentioned above come in handy.&lt;/p&gt;

&lt;p&gt;Suppose that you need to store/retrieve a Configuration object on/from disk in JSON format. You have experience with Gson from previous projects, so you use it. You defined Configuration as:&lt;/p&gt;


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


&lt;p&gt;You first start by creating an abstraction for Gson. Here, a simple class will do (unless you’re using &lt;a href="http://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html"&gt;Clean Architecture&lt;/a&gt;: in that case, you would have this class implement an &lt;em&gt;interface&lt;/em&gt;):&lt;/p&gt;


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


&lt;p&gt;Then, you use it along with the rest of your product:&lt;/p&gt;


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



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


&lt;p&gt;Time goes by, and your abstraction gets sprinkled throughout your code. One day, you come across this hip library called Moshi, that also deals with json parsing. Moshi seems to be faster, more flexible, and works like a charm when used together with Retrofit. You got to use it.&lt;/p&gt;

&lt;p&gt;Luckily, you saw this coming. You use Gson everywhere in your code. But since you have it encapsulated, you can swap it with Moshi almost for free!&lt;/p&gt;

&lt;p&gt;Simply replace Gson with Moshi:&lt;/p&gt;


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


&lt;p&gt;And you're done  -  all the code that used Gson now uses Moshi. Just by changing this class.&lt;br&gt;
You can later change libraries again. You can even ditch json and use something else. As long as you create the proper abstraction (which is actually the hard part), you're good to go. Your code is now robust and flexible, and your future self will be proud.&lt;/p&gt;

&lt;p&gt;Note that the codebase is further improved by injecting the dependencies. Even if you don’t use Dagger or any other framework — the dependency injection itself is what matters. This way, you keep your classes decoupled and set yourself up for easy testing. Here, if you inject a mock or fake storage handler, you can test &lt;em&gt;MagicBusiness&lt;/em&gt; in isolation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;“Abstractions Live Longer than Details“&lt;/em&gt;&lt;br&gt;
— The Pragmatic Programmer, chapter 7, page 209&lt;/p&gt;

&lt;p&gt;I showed you an example of how you can create boundaries around your code. These boundaries protect your code from external dependencies. Still, it goes much deeper than this. Recipes like design patterns or architectural patterns such as Clean Architecture are great. They’re battle tested, and their usefulness is more than proven. Using these design principles is one reason for their greatness. You can (and should!) apply these design principles even if you don’t use external code. Use them with caution, though. We know that design principle abuse increases code complexity. It’s a commitment you must consider, and balance with care.&lt;/p&gt;




&lt;p&gt;If you got this far, thank you very, very much :) if you want to keep in touch, you can find me on &lt;a href="https://twitter.com/rcosteira79"&gt;Twitter&lt;/a&gt; or check out my &lt;a href="https://github.com/rcosteira79"&gt;GitHub&lt;/a&gt;. This article was also published on &lt;a href="https://medium.com/@ricardocosteira/encapsulate-and-abstract-for-future-proof-software-dbf1cff7448c"&gt;Medium&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
      <category>architecture</category>
      <category>oop</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Package by feature vs. feature by module</title>
      <dc:creator>Ricardo Costeira</dc:creator>
      <pubDate>Sun, 25 Nov 2018 21:40:58 +0000</pubDate>
      <link>https://dev.to/rcosteira79/package-by-feature-vs-feature-by-module-5387</link>
      <guid>https://dev.to/rcosteira79/package-by-feature-vs-feature-by-module-5387</guid>
      <description>&lt;p&gt;Hi everyone, &lt;/p&gt;

&lt;p&gt;Lately I've been wanting to start a new personal Android project, and after attending a GDG DevFest event I finally found the inspiration to do so.&lt;/p&gt;

&lt;p&gt;I'm used to organize everything following a "package by feature" logic, but I've read some articles recently where people were talking about having each feature in its own independent module. &lt;/p&gt;

&lt;p&gt;Now, architecturally speaking, this seems like a really good option (high cohesion, loose coupling, feature reuse and all that), but probably due to the fact that I'm used to monolithic Android apps (with well defined boundaries between components though), I'm struggling to not see this as an overkill. &lt;/p&gt;

&lt;p&gt;Do any of you have any experience with this? Or can you see any disadvantages? I'm figuring dependency injection and navigation might become a challenge to do in a clean way.&lt;/p&gt;

&lt;p&gt;I'll probably end up doing it just to try it on my own, but I would like to hear your thoughts on this. Thanks! :)&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>android</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
