<?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: Mateusz Gajda</title>
    <description>The latest articles on DEV Community by Mateusz Gajda (@mgce).</description>
    <link>https://dev.to/mgce</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%2F212997%2F574f98ef-2a32-4905-8b92-0c8f2dac1779.jpeg</url>
      <title>DEV Community: Mateusz Gajda</title>
      <link>https://dev.to/mgce</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mgce"/>
    <language>en</language>
    <item>
      <title>Serverless application development in 5 minutes with Node.js</title>
      <dc:creator>Mateusz Gajda</dc:creator>
      <pubDate>Mon, 27 Apr 2020 08:11:28 +0000</pubDate>
      <link>https://dev.to/mgce/serverless-application-development-in-5-minutes-with-node-js-436e</link>
      <guid>https://dev.to/mgce/serverless-application-development-in-5-minutes-with-node-js-436e</guid>
      <description>&lt;h3&gt;
  
  
  Serverless is one of those trends that excite both developers and business people, due to all the potential benefits it can provide. Want to quickly get up to speed with it? Let’s create a serverless application in a couple of minutes with Node.js! It’s a great opportunity to find out more about the strengths and weaknesses of serverless application development.
&lt;/h3&gt;

&lt;p&gt;What if I told you that you can create your REST API, deploy it on a server, and connect it to the database in just 5 minutes? What’s more, it won’t drain your wallet. Sounds good? Let’s talk today about serverless.&lt;/p&gt;

&lt;p&gt;Let’s establish an important fact. Unfortunately, serverless doesn’t mean that our code will magically run without a server 😢. What we get is a solution which allows us to deploy the code without having to manage the whole server infrastructure. Everything runs automatically. All we have to do is bring our code to one of the popular platforms and all of our worries about buying servers, monitoring, or scaling can be forgotten. &lt;/p&gt;

&lt;p&gt;But it’s not all roses. An important aspect to consider is vendor lock-in. When our configuration is big, it can be hard to migrate from one vendor to another. Each one of them has specific services and different ways to configure them. Once we decide on one of them, it can be very difficult to change it.&lt;/p&gt;

&lt;p&gt;With that said, let’s take a closer look on the bright side of serverless 😉&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of serverless app development
&lt;/h2&gt;

&lt;p&gt;I noticed that most developers don’t find it very hard to create custom software or design its architecture. This is our daily bread and we feel comfortable about it. The problem occurs when we need to deploy our application somewhere. I think that people are a little bit afraid of the server layer. Managing and deploying applications correctly might give you a headache. &lt;/p&gt;

&lt;p&gt;What if we didn’t have to do it? That is one of the reasons why we should try this new fancy solution called serverless. It allows us to focus solely on writing an app, without having to care about infrastructure.&lt;/p&gt;

&lt;p&gt;But what about the cost? Unlike with traditional servers, we only pay for the resources used or for the functions called out. But what does it mean? &lt;/p&gt;

&lt;p&gt;Let’s say we have an application, which is used mostly during standard working hours (9-17). For the rest of the day, the application “sleeps”. With the traditional approach, we will pay for the whole day all the same, even if no one has used the application for almost 16 hours. In a case like this, we can save up a lot with serverless. Why? Because when our service is deployed as a function, we pay only for each execution or for the execution time. The cost is tied to actual usage.&lt;/p&gt;

&lt;p&gt;What else can functions give us? For once, they are extremely scalable. Depending on the traffic, the functions scale up to a level that will allow for handling of all the requests. They are also highly available, so we don’t have to worry about service availability.&lt;/p&gt;

&lt;p&gt;That’s the theory – what about practice? Let’s make a quick serverless for beginners project to really get you started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Serverless application development frameworks
&lt;/h2&gt;

&lt;p&gt;We’re going to use the Serverless Framework, one of the most popular tools for building serverless applications. It is an open source solution and you can &lt;a href="https://github.com/serverless/serverless"&gt;find it on GitHub&lt;/a&gt;. This framework supports a lot of providers such as AWS, GCP, Azure, OpenWhisk and many others. It also supports different languages such as Node.js, C#, Java, Go, Python etc. With a large community around it as well as impressive popularity, it’s definitely worth a try.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s create our REST API!
&lt;/h2&gt;

&lt;p&gt;Below you will find a rest of an article! Have fun! :)&lt;br&gt;
&lt;a href="https://tsh.io/blog/serverless-application-development/"&gt;https://tsh.io/blog/serverless-application-development/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Do you know GRASP? Part 3 - Cohesion and Coupling</title>
      <dc:creator>Mateusz Gajda</dc:creator>
      <pubDate>Wed, 08 Jan 2020 09:05:07 +0000</pubDate>
      <link>https://dev.to/mgce/do-you-know-grasp-part-3-cohesion-and-coupling-45ia</link>
      <guid>https://dev.to/mgce/do-you-know-grasp-part-3-cohesion-and-coupling-45ia</guid>
      <description>&lt;p&gt;Cohesion and Coupling are very popular terms in Software Engineering. Also, GRASP tells something about it. Let's take a look at what these patterns tell us about these weirds words.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coupling
&lt;/h2&gt;

&lt;p&gt;What everybody knows about coupling is that good practice is to keep coupling low. It is because coupling means how one element relates to another. As coupling grows, the dependence of different modules on each other increases. So we can say that if the coupling is low, changing something in one class shouldn't affect another. &lt;br&gt;
We can also notice another benefit of low coupling. If classes are independent or have a low number of dependencies, they are easy to reuse. They are also easy to test because we have fewer dependencies to manage :)&lt;/p&gt;

&lt;p&gt;High coupling makes it very difficult to maintain or introduce any features into our codebase. From the client perspective, it is also very irritating, because the client needs to wait much more time for new features. So he need also to pay more for our work. &lt;/p&gt;

&lt;p&gt;As we divided coupling for low and high, let's consider how to distinguish them? High coupling is when one class use or depends on the implementation details of another class. For example, we use some properties from another class directly. So we depend on implementation detail. In case when this property will be changed, we will be forced to change first class. Low coupling is when we depend on abstraction and encapsulation. It's when the second class exposes some public methods, which allows us to access important data. It's because when implementation details have been changed, our public contract will be the same. So we don't care what is going on inside the method, we just use it.&lt;/p&gt;

&lt;p&gt;There are different types of couplings, below is a list ordered from least accepted to the most accepted:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Content Coupling&lt;/strong&gt; - when one module talks directly with another module. It means that module X use implementation details from module Y.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Common Coupling&lt;/strong&gt; - when two separated module depends on some shared state. It could be some variable for example. In this case, they don't talk directly to each other, but by some other value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;External Coupling&lt;/strong&gt; - if two modules want to talk to each other, they use some external interface. When the interface has been changed, it can no longer communicate anymore.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Control Coupling&lt;/strong&gt; - one module tells another module how to do his job. It can be done by passing a flag, which will manipulate another module by turning off/on some kind of business logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stamp Coupling&lt;/strong&gt; - we pass some data structure from one module to another, and this module use only part of the passed data to do their job. We depend on the whole data structure even if we need only some data from it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Coupling&lt;/strong&gt; - we pass from one module to another only data which are necessary by this module to do their job.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So for us, as a developer, important is to keep coupling low. Maybe not as low as possible, because sometimes it is useless, and increase the level of complexity, so we should be pragmatic.&lt;/p&gt;
&lt;h2&gt;
  
  
  Cohesion
&lt;/h2&gt;

&lt;p&gt;When we talk about cohesion, we mean how focused the class is on their task. If the class has high cohesion we see that in the class are only methods related to the intention of the class. Thanks to that, when we have high cohesion, the modularity of our system is increasing. So if we keep things related to each other in one class or module, the bindings between other classes will be smaller. What will cause the changes will concern some small areas and not half of the system. If the class has low cohesion, we can notice inside the class some methods not related to it. &lt;/p&gt;

&lt;p&gt;Like the coupling, cohesion has also some types. Below is a list ordered from least acceptable to the most:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Coincidental&lt;/strong&gt; - two functions are in one module by accident and nothing connects them. Classes like utilities or shared are the best example of the coincidental cohesion.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logical&lt;/strong&gt; - when, at first glance, the two entities are connected, but after deeper consideration, they would turn out to be completely different. For example, two entities with similar names, but differentiated responsibility. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Temporal&lt;/strong&gt; - two functions are in the same module because they must be executed at the same time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Procedural&lt;/strong&gt; - the procedure needs some operation to be executed. So we group all operations into one module&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Communicational&lt;/strong&gt; - there are functions in the module that use the same data &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sequential&lt;/strong&gt; - there is a function, which returns data which the next function needs. So we create a pipeline of executions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Functional&lt;/strong&gt; - functions are in one module because together they create a whole feature.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's take a look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Address&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;salaries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Salary&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="nx"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;
  &lt;span class="nx"&gt;getAddress&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;
  &lt;span class="nx"&gt;getSalaryReport&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we have a class Employee with two methods, &lt;em&gt;get name&lt;/em&gt;, and &lt;em&gt;get a salary report&lt;/em&gt;.  We can say that this is natural that employee has a salary, but are we really need this information in employee class? Should employee be responsible for creating a report? In my opinion not. What we should do? We should create a separated class focused on this functionality, like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Address&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;
  &lt;span class="nx"&gt;getAddress&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;EmployeeSalary&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;employeeId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;salaries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Salary&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;

  &lt;span class="nx"&gt;getSalaryReport&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, we have an independent class. When we want to generate the report for an employee we retrieve EmployeeSalary class from the database and execute method &lt;em&gt;getSalaryReport()&lt;/em&gt;. Thanks to that Employee class don't know anything about the salary, because it is not important information for Employee.&lt;/p&gt;

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

&lt;p&gt;In conclusion, &lt;strong&gt;cohesion&lt;/strong&gt; means how related and focused the responsibilities of an software element are. &lt;strong&gt;Coupling&lt;/strong&gt; refers to how strongly a software element is connected to other elements.&lt;br&gt;
As we can see, taking care about low coupling and high cohesion helps us creating class with one specialization, without any additional dependency. It will help us in the future, when we will be forced to change an implementation. When we spend enought time thinking about, how to design our system and apply this patterns, we save time in the future. We will also make our client happy, what is very important, because we create software for the business.&lt;/p&gt;

&lt;p&gt;In the next part, I write about last 3 patterns: Polymorphism, Protected variations and Pure fabrications.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>codequality</category>
      <category>firstyearincode</category>
    </item>
    <item>
      <title>Do you know GRASP? Part 2 - Indirection and Information expert</title>
      <dc:creator>Mateusz Gajda</dc:creator>
      <pubDate>Sun, 05 Jan 2020 18:46:54 +0000</pubDate>
      <link>https://dev.to/mgce/do-you-know-grasp-part-2-indirection-and-information-expert-51i4</link>
      <guid>https://dev.to/mgce/do-you-know-grasp-part-2-indirection-and-information-expert-51i4</guid>
      <description>&lt;p&gt;Welcome in Part 2 in series about GRASP. Today I would like to tell you more about &lt;strong&gt;Indirection&lt;/strong&gt; and &lt;strong&gt;Information expert&lt;/strong&gt; patterns. As a reminder, these patterns allow us to see how to allocate responsibility in our code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Indirection
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Problem: Where to assign a responsibility to avoid direct coupling between two or more things?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For better understanding where we can use this pattern, let's imagine the following situation. We have a system for booking people at an event. The client can choose from many employees due to their specialization and add them to their project. In this case, we need to set each employee as booked for the specified date, we must also inform the project that new people have been added to it, and finally update the fee the client will pay. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YDWGycCk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/pgx00h0i5qm4hlsz1fym.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YDWGycCk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/pgx00h0i5qm4hlsz1fym.png" alt="Indirection Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, in this case, the Employee component needs to call and know about the Project component so that it can update the information about booked employees. In addition, there would be another binding between the project and client charges. We want these three components to be independent, so this solution is not good for us. &lt;/p&gt;

&lt;p&gt;So what we need? A new component that will be kind of orchestrator for others. It will determine the order of operations. We can use some of these design patterns to introduce it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;bridge&lt;/li&gt;
&lt;li&gt;facade&lt;/li&gt;
&lt;li&gt;adapter&lt;/li&gt;
&lt;li&gt;mediator&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why? Because we need an object that will be responsible for communication between all modules. This way the components will know nothing about each other. In addition, there will be no problems to find out which component is looking at which one, because none is looking at any.&lt;/p&gt;

&lt;p&gt;Here is a result of introducing mediator to our code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yBEb_ZlF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/couvc9bpzqkhkozyb4u4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yBEb_ZlF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/couvc9bpzqkhkozyb4u4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Information expert
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Problem: What is a basic principle by which to assign responsibilities to objects?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This pattern tells us that we should add new responsibility to the class that has the information needed to fulfill it. We do it to avoid implicitly connections between class. So this is very important to aggregate similar responsibilities in the same class.&lt;br&gt;
Let take a look at the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;ShoppingCart&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="nx"&gt;addOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;getTotal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So if we want to add a new feature, to count the total cost of our shipping card, we should add this function to the ShoppingCart class. Why? Because this class knows everything about orders, so this is the best place for this. Someone may be wondering if such an addition of methods will not make the class grow too much? It's possible. But is it really a problem for us if all functions are linked to the same data? I don't think so.&lt;/p&gt;

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

&lt;p&gt;In the next part, I will talk more about cohesion and coupling. These are very interesting topics, so I will try to tell you a little more about them. &lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>codequality</category>
      <category>firstyearincode</category>
    </item>
    <item>
      <title>Do you know GRASP? Part 1 - Controller and Creator</title>
      <dc:creator>Mateusz Gajda</dc:creator>
      <pubDate>Sat, 04 Jan 2020 20:56:40 +0000</pubDate>
      <link>https://dev.to/mgce/do-you-know-grasp-part-1-controller-and-creator-46bg</link>
      <guid>https://dev.to/mgce/do-you-know-grasp-part-1-controller-and-creator-46bg</guid>
      <description>&lt;p&gt;Due to the fact that shorter posts are much better read, I decided to divide this topic into about 4 separate entries. Each concerning two or three patterns. Here is part one.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is it?
&lt;/h1&gt;

&lt;p&gt;GRASP - &lt;em&gt;General Responsibility Assignment Software Patterns&lt;/em&gt; are 9 patterns, which will help you with adding a new responsibility to your current codebase. It could be very helpful because sometimes we don't know if this component is suitable for this functionality. Maybe we should create a new one? These 9 patterns can help us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Controller
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Problem: What the first object beyond the UI layer receives and coordinates “controls” a system operation?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8R1ptVxV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://heavyeditorial.files.wordpress.com/2019/08/13-best-ps4-controllers-your-buyers-guide-2019.jpg%3Fquality%3D65%26strip%3Dall" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8R1ptVxV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://heavyeditorial.files.wordpress.com/2019/08/13-best-ps4-controllers-your-buyers-guide-2019.jpg%3Fquality%3D65%26strip%3Dall" alt="Controllers image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the software world, everything should have its responsibility. Models contain data, views present the data, repositories talk with databases, etc. Our systems are consistent with a lot of different objects and components. Very often they have to talk with each other, but we should take care who's talking to whom. &lt;br&gt;
We do not want to let our objects talk directly to the database or other external services. We need something which takes this responsibility.&lt;/p&gt;

&lt;p&gt;By simplifying our systems, we can say that they consist of many different use cases. Each use case needs to talk with a lot of different objects. We need to talk with the database, execute some operations on the domain object or retrieve some data from external API. We need also a layer that will be responsible for handling user input.&lt;/p&gt;

&lt;p&gt;That is why we a controller. An object which will be an entry point for our use case or system. It will be the first object beyond the UI layer. Very often it will be an application service or command handler if we are talking in CQRS case. But, be careful! It isn't a controller such as in MVC. In MVC the controller is still part of a UI. In our case, it is the first object in the system. Let's take a look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;OrderService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;orderRepository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;OrderRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;productRepository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ProductRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;orderRepository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;OrderRepository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="nx"&gt;productRepository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ProductRepository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;orderRepository&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;orderRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productRepository&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;productRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderDetails&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;OrderDetailsDto&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;productId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;orderDetails&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;orderRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have our application service called OrderService. Here we talk to our database through repositories. We retrieve a product which next is passed to our static factory method, which is part of our domain model Order. Next, we save a new order to the database. Thanks to that, the domain model doesn't know anything about the database. Additionally, the separation of the application code from the UI allows us to test our service more easily. In the case of the unit tests, we need only to mock our repositories.&lt;br&gt;
Here is how it looks like in our router:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;orderRouting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;OrderService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/order&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NextFunction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;orderService&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Creator
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Problem: Who creates object Y?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LffJ4fB0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.al.to/i/setup/images/prod/big/product-new-big%2C%2C2019/5/pr_2019_5_15_10_2_14_113_00.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LffJ4fB0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.al.to/i/setup/images/prod/big/product-new-big%2C%2C2019/5/pr_2019_5_15_10_2_14_113_00.jpg" alt="Lego creator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A common problem is definitely which object should be responsible for creating a class Y instance. Creating an object is an important process, so it is good to have defined rules that will make it easier to decide who should create a Y-instance. So let's take a look at these rules. Object X can create Y if the following conditions are true:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;X aggregate/contains Y.&lt;/li&gt;
&lt;li&gt;X has all the data required to create Y&lt;/li&gt;
&lt;li&gt;X closely use Y&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks to these rules, it may turn out that we don't have to create a special factory to create the Y object. We can simply create it in object X. Why? Because X object needs Y object, it knows everything about how to create Y object so a new factory, in this case, will be useless.&lt;/p&gt;

&lt;p&gt;Assume that we have a diary class that contains our notes. We can easily create our note inside the &lt;em&gt;Diary&lt;/em&gt;, because it contains the list of &lt;em&gt;Notes&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Diary&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Notes&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="nx"&gt;addNote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;That's only 2 out of 9 patterns. In the next few parts, I'll introduce you to the next ones. I hope you'll find them useful and use them. If you have some question, feel free to write a comment :)&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>codequality</category>
      <category>firstyearincode</category>
    </item>
  </channel>
</rss>
