<?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: Piero Sifuentes</title>
    <description>The latest articles on DEV Community by Piero Sifuentes (@piero9212).</description>
    <link>https://dev.to/piero9212</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%2F170747%2F4e0c2a4d-6fec-4fc8-93aa-c5d08a274ac8.jpeg</url>
      <title>DEV Community: Piero Sifuentes</title>
      <link>https://dev.to/piero9212</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/piero9212"/>
    <language>en</language>
    <item>
      <title>SOLID Programming with Swift examples (1)</title>
      <dc:creator>Piero Sifuentes</dc:creator>
      <pubDate>Thu, 23 May 2019 04:27:30 +0000</pubDate>
      <link>https://dev.to/piero9212/solid-programming-with-swift-examples-1-4pg2</link>
      <guid>https://dev.to/piero9212/solid-programming-with-swift-examples-1-4pg2</guid>
      <description>&lt;p&gt;All of us who are immersed in software development at some point have heard, have an idea or have experience about &lt;em&gt;SOLID&lt;/em&gt; principles. This principles have been described long time ago (2000) and have been used in several programming languages.&lt;br&gt;
This concepts are still been useful for all those who are developing software or have to review piece of codes in terms of quality/functionality (Like developers reviewing code in other programming language). It doesn’t matter how long have you been working with software (experience) these principles work as &lt;em&gt;patterns&lt;/em&gt; to take good decisions at the developing time.&lt;/p&gt;

&lt;p&gt;Let’s see what these patterns are and what they are trying to solve.&lt;/p&gt;
&lt;h2&gt;
  
  
  Bad Architectural Decisions (as I usually said BAD)
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Lets try to determine if you already have BAD code.
&lt;/h3&gt;

&lt;p&gt;Have you ever tried to modify some piece of code and noticed that you will have to spent a lot of time refactoring or changing other functionality/files/classes than the specific piece of code?&lt;br&gt;
This problem is known as Rigidity because it requires a lot of efforts like change several parts of the project to make a change.&lt;/p&gt;

&lt;p&gt;Have you ever changed a piece of code and got errors/crashes/problems in other functionality/files/classes than the specific piece of code?&lt;br&gt;
This problem is known as Fragility because a change may break unexpected parts .&lt;/p&gt;

&lt;p&gt;Have you ever tried to reuse a piece of code in other file/module/project/repository and had to add his dependencies with it?&lt;br&gt;
This problem is known as Immobility because is difficult to reuse a component in another file/module/project/repository because it has too many coupled dependencies.&lt;/p&gt;

&lt;p&gt;I summarize all those problems making one golden rule:&lt;/p&gt;

&lt;p&gt;Write code that is easy to delete or replace&lt;br&gt;
As Uncle Bob (Robert C Martin) said, these concepts are not rules, laws or perfect truths, these are guidelines to help you developing good software and improve the quality of your software architecture so use them properly.&lt;/p&gt;
&lt;h2&gt;
  
  
  SOLID
&lt;/h2&gt;

&lt;p&gt;Is an acronym that stands for five guiding principles. I’ll be explaining each one with examples made in Swift (iOS).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Single Responsibility Principle&lt;/em&gt; means that a function, module, class or struct should have just one goal, reason or job. Every time you create/change a function/module/class/struct, you should ask yourself: How many responsibilities does it has? Sounds easy right?&lt;/p&gt;

&lt;p&gt;We usually violate this principle because apply this pattern could be hard work in terms of the number of files, classes and/or protocols that you need to create for it.&lt;/p&gt;

&lt;p&gt;In my experience as iOS developer, I saw one of the worst elements where we usually break the Single Responsibility Principle, the view controller.&lt;/p&gt;

&lt;p&gt;We can detect this problem easily considering that:&lt;/p&gt;

&lt;p&gt;A view controller can be a data source or delegate of your table view or collection view,&lt;br&gt;
You can make web requests in a view controller&lt;br&gt;
You can use business logic in a view controller&lt;br&gt;
You can be formatting elements in a view controller&lt;br&gt;
You can be handling the navigation in a view controller&lt;/p&gt;

&lt;p&gt;Let’s look an example:&lt;/p&gt;


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


&lt;p&gt;At first glance it’s not bad at all, but this class does more than single work and as you can see in the function names, there are different goals that this class is trying to achieve. That is giving us sign of bad design.&lt;/p&gt;

&lt;p&gt;How many responsibilities does this class have?&lt;/p&gt;

&lt;p&gt;MovieListViewController retrieves the data from the API (1), parses the API response (2), filter the movie list showing just the upcoming movies (3), and finally reload the table showing the cells (4), formatting the cell title (5). Also, we can filter the list using IBAction (6) and show the MovieDetailViewController once a cell gets tapped(7).&lt;/p&gt;

&lt;p&gt;Some keys to achieve this principle is employ Delegation with Protocols, Design Patterns and Code Isolation (Moving the responsibilities down to little classes).&lt;/p&gt;

&lt;p&gt;Let’s refactor this:&lt;/p&gt;


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


&lt;p&gt;So first, we’ve isolated the view responsibilities to just display the information in the View using protocols (MovieListView), delegation (MovieListTableViewDataSource) and MVP pattern (MovieListPresenter), then&lt;/p&gt;


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


&lt;p&gt;We’ve encapsulated the table data source job creating a class that handle all the table work needed and handling the table view selection through delegates (MovieListTableViewDataSource). Finally:&lt;/p&gt;


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


&lt;p&gt;We’ve created a MovieListPresenter where we can handle the business logic, fetch data from the API using theMoviesAPIClient class and parsing that response using MovieParseHandler.&lt;/p&gt;

&lt;p&gt;Finally, we’ve made a lot of improvements in this example project but it could take time because you need to uncouple all the class jobs and create a lot of files for it, but you’ll be rewarded having better and fully testable code.&lt;/p&gt;

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

&lt;p&gt;These principles helps you to keep your classes as clean as possible. Don’t forget that you’ll need to use good practices and design/architectural patterns as tools to apply these concepts and improve your code to next level.&lt;/p&gt;

&lt;p&gt;As you have just learned, I’ll be creating a series of articles about each SOLID principle avoiding have one so big article.&lt;/p&gt;

&lt;p&gt;Hope you like this article, feel free to hit the clap button below 👏 to help others find it! Let me know your feedback in the comments below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contact me
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/pierosifuentes/"&gt;https://www.linkedin.com/in/pierosifuentes/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="mailto:piero9212@gmail.com"&gt;piero9212@gmail.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Original post
&lt;/h2&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/@piero9212/solid-programming-with-swift-examples-c540c9b1af72" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MRgMbgVo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/fit/c/100/100/2%2A6l5_CmLLs38SP7D50cj3pA.jpeg" alt="Piero Sifuentes"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/@piero9212/solid-programming-with-swift-examples-c540c9b1af72" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;SOLID Programming with Swift examples&lt;/h2&gt;
      &lt;h3&gt;Piero Sifuentes ・ 4 min read&lt;/h3&gt;
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aYMKNcyE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/medium_icon-fbdac08496f06c5bd53be920c7bc8d56d355b69c0fb7e49cac6357a70140af17.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.scaledrone.com/blog/solid-principles-for-becoming-a-better-ios-swift-developer/"&gt;https://www.scaledrone.com/blog/solid-principles-for-becoming-a-better-ios-swift-developer/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://clean-swift.com/single-responsibility-principle-for-class/"&gt;https://clean-swift.com/single-responsibility-principle-for-class/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://sites.google.com/site/unclebobconsultingllc/getting-a-solid-start"&gt;https://sites.google.com/site/unclebobconsultingllc/getting-a-solid-start&lt;/a&gt;&lt;br&gt;
&lt;a href="https://codeburst.io/solid-design-principle-using-swift-fa67443672b8"&gt;https://codeburst.io/solid-design-principle-using-swift-fa67443672b8&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/ochococo/OOD-Principles-In-Swift#-the-single-responsibility-principle"&gt;https://github.com/ochococo/OOD-Principles-In-Swift#-the-single-responsibility-principle&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>solid</category>
    </item>
  </channel>
</rss>
