<?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>Forem: Roland Dumitrascu</title>
    <description>The latest articles on Forem by Roland Dumitrascu (@rolanddumit).</description>
    <link>https://forem.com/rolanddumit</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%2F880335%2F93ae264d-0bba-4f34-8fe3-b03b7b25af59.jpg</url>
      <title>Forem: Roland Dumitrascu</title>
      <link>https://forem.com/rolanddumit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rolanddumit"/>
    <language>en</language>
    <item>
      <title>Guard Clauses Technique</title>
      <dc:creator>Roland Dumitrascu</dc:creator>
      <pubDate>Tue, 21 Jun 2022 15:40:10 +0000</pubDate>
      <link>https://forem.com/rolanddumit/guard-clauses-technique-5bb8</link>
      <guid>https://forem.com/rolanddumit/guard-clauses-technique-5bb8</guid>
      <description>&lt;p&gt;Let’s be honest, how many times you found yourself in situations like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;someMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition3&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;doSomething&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;debugPrint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Problem 3'&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;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;debugPrint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Problem 2'&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;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;debugPrint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Problem 1'&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;Countless times, perhaps even worse than that, especially at the beginning of our developer careers. The more conditions you nest, the more your code will get like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;someMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition3&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition4&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition5&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition6&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition7&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition8&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                                    &lt;span class="n"&gt;doSomething&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can get pretty ugly and make the code reading process very frustrating. There is a better, more intuitive and read-friendly way to do it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Guard Clauses
&lt;/h2&gt;

&lt;p&gt;A guard clause consists of an expression that must evaluate whether code execution should continue. In a nutshell, instead of nesting multiple if conditions one inside another, we create guard clauses that individually checks if there is a problem before executing the main code. Let’s have a look at the transformation from the example above to the guard clauses technique:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;someMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;condition1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;debugPrint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Problem 1'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;condition2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;debugPrint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Problem 2'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;condition3&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;debugPrint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Problem 3'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;doSomething&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&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;Much more readable right?&lt;/p&gt;

&lt;p&gt;Now is much simpler to add new conditions without messing up the code.&lt;/p&gt;

&lt;p&gt;For more articles, follow me here on DEV.TO, on &lt;a href="//flutterinanutshell.com"&gt;Flutter in a Nutshell&lt;/a&gt; and &lt;a href="https://twitter.com/flutternutshell"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>softwaredevelopment</category>
      <category>dart</category>
      <category>mobile</category>
    </item>
    <item>
      <title>BLoC Architectural Pattern for Flutter</title>
      <dc:creator>Roland Dumitrascu</dc:creator>
      <pubDate>Tue, 21 Jun 2022 09:08:09 +0000</pubDate>
      <link>https://forem.com/rolanddumit/bloc-architectural-pattern-for-flutter-jdo</link>
      <guid>https://forem.com/rolanddumit/bloc-architectural-pattern-for-flutter-jdo</guid>
      <description>&lt;p&gt;BLoC is an architectural pattern for Flutter, but what is an architectural pattern? Let me explain everything in a nutshell.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an architectural pattern
&lt;/h2&gt;

&lt;p&gt;Let’s start by asking this question: what is a pattern? In software development, an architectural pattern defines the connection between classes and how the data should interact with the different roles. In a nutshell, creating a pattern is like drawing some dots and joining them with lines to define the data flow from source to user.&lt;/p&gt;

&lt;p&gt;Popular patterns are &lt;strong&gt;MVC&lt;/strong&gt; (&lt;strong&gt;M&lt;/strong&gt;odel &lt;strong&gt;V&lt;/strong&gt;iew &lt;strong&gt;C&lt;/strong&gt;ontroller), &lt;strong&gt;MVP&lt;/strong&gt; (&lt;strong&gt;M&lt;/strong&gt;odel &lt;strong&gt;V&lt;/strong&gt;iew &lt;strong&gt;P&lt;/strong&gt;resenter), &lt;strong&gt;MVVM&lt;/strong&gt; (&lt;strong&gt;M&lt;/strong&gt;odel &lt;strong&gt;V&lt;/strong&gt;iew &lt;strong&gt;V&lt;/strong&gt;iew*&lt;em&gt;M&lt;/em&gt;*odel). All these define the interaction and flow of data from point A to point B.&lt;/p&gt;

&lt;p&gt;In 2018, at the I/O, Google talked for the first time about a new pattern they created for Flutter, the &lt;strong&gt;BLoC&lt;/strong&gt; pattern, and since then it’s the golden standard for the majority of Flutter developers.&lt;/p&gt;

&lt;p&gt;BLoC stands for &lt;strong&gt;Business Logic Component&lt;/strong&gt; and its goal is to facilitate the management of state, decoupling logic from presentation (views).&lt;/p&gt;

&lt;p&gt;NOTE: State is data needed to rebuild the UI at any moment in time. When the state of the app changes, for example the user presses a button, that triggers a redraw of the UI.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why we need a development pattern
&lt;/h2&gt;

&lt;p&gt;Using a pattern is not mandatory, an app can be made without using them but that can bring problems in future stages of the project. If you’re developing a small project, an app with a couple of pages, you may not use them; but if you have a bigger project, or rather a project that will scale up over time, a pattern could eliminate headaches thanks to responsibilities separation and a clear data flow.&lt;/p&gt;

&lt;p&gt;If you find yourself working on a big one, which has no pattern or uses a very chaotic one, you could end up destroying the functioning of the app because touching the smallest piece of code can turn into the explosion of another piece of functionality due to spider web-like data flow.&lt;/p&gt;

&lt;p&gt;To avoid these kind of situations, we introduce responsibilities separation which is at the core of development patterns. Separating responsibilities between classes means that you create compartments that have only one job, for example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ui class – the user interface class&lt;/li&gt;
&lt;li&gt;business logic class – a class that takes inputs (events) from UI and either processes it or forwards the event&lt;/li&gt;
&lt;li&gt;network class – it’s only task is to handle api calls and pass data to who requested it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a nutshell, in the BLoC pattern, instead of having everything cluttered in one place it would look like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k3KVqSce--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q2m4b9xfevqoj2tp0js.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k3KVqSce--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q2m4b9xfevqoj2tp0js.png" alt="Design Pattern Example" width="880" height="898"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;User presses the refresh button, User Interface fires an event&lt;/li&gt;
&lt;li&gt;BLoC captures the event and asks for data from Repository&lt;/li&gt;
&lt;li&gt;Repository downloads the data and returns it to the BLoC&lt;/li&gt;
&lt;li&gt;BLoC elaborates the data (if needed) and adds it to the sink&lt;/li&gt;
&lt;li&gt;User Interface listens to the stream and displays new data when it arrives&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next step is to deep dive into the BLoC pattern. To do so, continue reading on my blog:&lt;br&gt;
&lt;a href="https://flutterinanutshell.com/bloc-architectural-pattern-for-flutter/"&gt;Flutter in a Nutshell&lt;/a&gt;&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
