<?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: Carlos Cuesta</title>
    <description>The latest articles on DEV Community by Carlos Cuesta (@carloscuesta).</description>
    <link>https://dev.to/carloscuesta</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%2F30518%2F9e3c9d4f-6f3e-40fc-9845-c6629523a33a.jpg</url>
      <title>DEV Community: Carlos Cuesta</title>
      <link>https://dev.to/carloscuesta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/carloscuesta"/>
    <language>en</language>
    <item>
      <title>Writing RFCs</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Wed, 06 Mar 2024 14:00:00 +0000</pubDate>
      <link>https://dev.to/carloscuesta/writing-rfcs-2mb5</link>
      <guid>https://dev.to/carloscuesta/writing-rfcs-2mb5</guid>
      <description>&lt;p&gt;A &lt;strong&gt;Request For Comments&lt;/strong&gt; also known as &lt;em&gt;RFC&lt;/em&gt;, is a process that captures ideas and proposals about a specific topic to discuss and &lt;strong&gt;find consensus&lt;/strong&gt; 🤝.&lt;/p&gt;

&lt;p&gt;An RFC involves a &lt;strong&gt;document&lt;/strong&gt; or a series of documents 📑, which are &lt;strong&gt;drafted&lt;/strong&gt;, &lt;strong&gt;reviewed&lt;/strong&gt; and eventually &lt;strong&gt;approved&lt;/strong&gt; or &lt;strong&gt;rejected&lt;/strong&gt; by a group of people.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the benefits?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Planning
&lt;/h3&gt;

&lt;p&gt;Writing down your idea into a document will clarify your thoughts and will help organizing them in a structured manner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Anticipating&lt;/strong&gt; 🔮 the problem you aim to solve and envisioning potential solutions is key to &lt;strong&gt;identifying issues&lt;/strong&gt; and &lt;strong&gt;edge cases&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Alignment
&lt;/h3&gt;

&lt;p&gt;As a team, it's important that &lt;strong&gt;decisions&lt;/strong&gt; 🎙️ are &lt;strong&gt;made&lt;/strong&gt; by &lt;strong&gt;reaching&lt;/strong&gt; a &lt;strong&gt;consensus&lt;/strong&gt; 🤝. It's very common that as humans we have different opinions and points of view.&lt;/p&gt;

&lt;p&gt;RFCs are an effective process to include everyone's opinions 💭 and reach a decision everyone agrees with.&lt;/p&gt;

&lt;h3&gt;
  
  
  Documentation
&lt;/h3&gt;

&lt;p&gt;The document 📑 itself is a &lt;strong&gt;valuable piece of documentation&lt;/strong&gt; as it captures the context, the problem, the solution, and the decision-making process.&lt;/p&gt;

&lt;p&gt;It will serve as a &lt;strong&gt;reference&lt;/strong&gt; for &lt;strong&gt;future&lt;/strong&gt; team members and will help them &lt;strong&gt;understand&lt;/strong&gt; the &lt;strong&gt;context&lt;/strong&gt; and the &lt;strong&gt;reasoning&lt;/strong&gt; behind the &lt;strong&gt;decisions&lt;/strong&gt; made ❣️&lt;/p&gt;

&lt;p&gt;An RFC is usually the initial step in the decision-making process before creating &lt;a href="https://carloscuesta.me/blog/writing-architecture-decision-records" rel="noopener noreferrer"&gt;Architecture Decision Record&lt;/a&gt; which I previously wrote about ✍️&lt;/p&gt;

&lt;h3&gt;
  
  
  Better decision-making
&lt;/h3&gt;

&lt;p&gt;One of the steps of the process is to share the RFC with the team to &lt;strong&gt;collect feedback&lt;/strong&gt; and review the context and the proposed solution.&lt;/p&gt;

&lt;p&gt;Involving multiple people 🧠 in the decision-making process is key to achieve high quality decisions, as it will bring &lt;strong&gt;different perspectives&lt;/strong&gt; and &lt;strong&gt;solutions&lt;/strong&gt; to the table &lt;strong&gt;reducing&lt;/strong&gt; the chances of &lt;strong&gt;overlooking important details&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can think of it as a &lt;strong&gt;code review&lt;/strong&gt;, but for &lt;strong&gt;ideas&lt;/strong&gt; and &lt;strong&gt;proposals&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  When you should write an RFC?
&lt;/h2&gt;

&lt;p&gt;It depends 🙈 but generally I would recommend writing them when &lt;strong&gt;dealing&lt;/strong&gt; with &lt;strong&gt;high-impact decisions&lt;/strong&gt; and &lt;strong&gt;major changes&lt;/strong&gt; that &lt;strong&gt;require consensus&lt;/strong&gt; and alignment such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introducing a new service in your company 🚀&lt;/li&gt;
&lt;li&gt;Major changes in the architecture 🏗️&lt;/li&gt;
&lt;li&gt;Big features that have an overarching impact 🆕&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's up to you to decide when to write them, but in my opinion you should &lt;strong&gt;weight&lt;/strong&gt; ⚖️ the &lt;strong&gt;effort&lt;/strong&gt; 🥵 and the &lt;strong&gt;impact&lt;/strong&gt; 💥 since it's a time-consuming process that involves many people.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to write an RFC
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Define a template
&lt;/h3&gt;

&lt;p&gt;When writing them, a common practice is to have a template to write ✍🏼 all the documents in the same way.&lt;/p&gt;

&lt;p&gt;You'll find a lot of templates online, but &lt;a href="https://docs.google.com/document/d/19WIct9u3e-BEYTVWzrPxcXUJ28GyQFBrfGF_kpznE6c/edit?usp=sharing" rel="noopener noreferrer"&gt;this is the one I use&lt;/a&gt; 👈&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcarloscuesta%2Fimage%2Fupload%2Fv1709328274%2Frfc_jsuwbb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcarloscuesta%2Fimage%2Fupload%2Fv1709328274%2Frfc_jsuwbb.jpg" alt="RFC Template"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;❗️ Use a tool that allows &lt;strong&gt;collaborative features&lt;/strong&gt; (comments, suggestions etc) to make it easier for the team to review and provide feedback such as Google Docs or Notion.&lt;/p&gt;

&lt;h3&gt;
  
  
  Share document for review
&lt;/h3&gt;

&lt;p&gt;Once you have the document ready, share it with the team for them to &lt;strong&gt;review&lt;/strong&gt; 👀 and &lt;strong&gt;provide feedback&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Address feedback
&lt;/h3&gt;

&lt;p&gt;After the reviews come in, you should &lt;strong&gt;address&lt;/strong&gt; the &lt;strong&gt;comments&lt;/strong&gt;, &lt;strong&gt;questions&lt;/strong&gt; and &lt;strong&gt;concerns&lt;/strong&gt; and &lt;strong&gt;incorporate&lt;/strong&gt; any &lt;strong&gt;suggestions&lt;/strong&gt; if appropriate 🖊️.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make a decision
&lt;/h3&gt;

&lt;p&gt;Finally, after the document has been reviewed and the feedback has been addressed and collected, it's time to &lt;strong&gt;make a decision&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The team should either &lt;strong&gt;approve&lt;/strong&gt; ✅ the RFC and move forward with the proposal or &lt;strong&gt;reject&lt;/strong&gt; ❌ it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;In case you're looking for inspiration, there are many organizations and communities that use RFCs to propose and discuss new ideas, here are some examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/tc39/proposals" rel="noopener noreferrer"&gt;TC39&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/reactjs/rfcs" rel="noopener noreferrer"&gt;React&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/rust-lang/rfcs" rel="noopener noreferrer"&gt;Rust&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/npm/rfcs/" rel="noopener noreferrer"&gt;NPM&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;RFCs are a powerful process to &lt;strong&gt;gather feedback&lt;/strong&gt;, &lt;strong&gt;align&lt;/strong&gt; the team and &lt;strong&gt;document&lt;/strong&gt; &lt;strong&gt;decision-making&lt;/strong&gt;. They're a great way to &lt;strong&gt;reach consensus&lt;/strong&gt; as a team representing everyone's opinion.&lt;/p&gt;

&lt;p&gt;I encourage you to give them a try and see how they can help you and your team to make better decisions! 🫰&lt;/p&gt;

</description>
      <category>rfc</category>
    </item>
    <item>
      <title>Effective Refactoring with Codemods</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Mon, 30 Jan 2023 17:58:36 +0000</pubDate>
      <link>https://dev.to/carloscuesta/effective-refactoring-with-codemods-5010</link>
      <guid>https://dev.to/carloscuesta/effective-refactoring-with-codemods-5010</guid>
      <description>&lt;p&gt;&lt;strong&gt;Metaprogramming&lt;/strong&gt; is a technique where we write code to manipulate other code. I know this sounds a bit intimidating and confusing 😱, but actually I'm sure you've already used it in your daily work.&lt;/p&gt;

&lt;p&gt;Have you ever used a transpiler, a linter or a code formatter, such as: &lt;a href="http://babeljs.io" rel="noopener noreferrer"&gt;Babel&lt;/a&gt;, &lt;a href="http://eslint.org" rel="noopener noreferrer"&gt;ESLint&lt;/a&gt; or &lt;a href="http://prettier.io" rel="noopener noreferrer"&gt;Prettier&lt;/a&gt;? If the answer is yes, then you've already used &lt;a href="https://en.wikipedia.org/wiki/Metaprogramming" rel="noopener noreferrer"&gt;metaprogramming&lt;/a&gt;! 👏&lt;/p&gt;

&lt;p&gt;In this article I'm going to explain how you can use &lt;strong&gt;codemods&lt;/strong&gt; (&lt;em&gt;metaprogramming&lt;/em&gt;) to perform large-scale refactors to a codebase. Let's dive into it! 🤿&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a codemod?
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;codemod&lt;/strong&gt; is a tool that helps you with large-scale refactors by &lt;strong&gt;running transformations&lt;/strong&gt; on your &lt;strong&gt;codebase programatically&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;This automated transformations will allow you to change large amount of files without having to go through them manually, &lt;strong&gt;saving&lt;/strong&gt; a lot of &lt;strong&gt;time&lt;/strong&gt; and &lt;strong&gt;increasing&lt;/strong&gt; &lt;strong&gt;confidence&lt;/strong&gt;! 🙌&lt;/p&gt;

&lt;h3&gt;
  
  
  How do they work?
&lt;/h3&gt;

&lt;p&gt;On a general level codemods perform the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read the code input 📖&lt;/li&gt;
&lt;li&gt;Generate an &lt;strong&gt;A&lt;/strong&gt;bstract &lt;strong&gt;S&lt;/strong&gt;yntax &lt;strong&gt;T&lt;/strong&gt;ree (AST) 🌳&lt;/li&gt;
&lt;li&gt;Transform the AST 🔄&lt;/li&gt;
&lt;li&gt;Generate the new source code 📦&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Abstract Syntax Trees is what makes all of this magic 🪄 possible. Let's understand what they are and how they work 👇&lt;/p&gt;

&lt;h3&gt;
  
  
  Abstract Syntax Trees
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;Abstract Syntax Tree&lt;/strong&gt; (AST) is a &lt;a href="https://en.wikipedia.org/wiki/Tree_(data_structure)" rel="noopener noreferrer"&gt;tree data structure&lt;/a&gt; that represents a piece of code.&lt;/p&gt;

&lt;p&gt;Let's understand it with an example 🖼️:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;! 👋`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To inspect and visualize the AST for the snippet of code 👆, we will use the &lt;a href="https://astexplorer.net" rel="noopener noreferrer"&gt;AST Explorer&lt;/a&gt;. This is an online tool that allows you to explore and play with abstract syntax trees.&lt;/p&gt;

&lt;p&gt;The entire snippet is represented by a &lt;strong&gt;Program node&lt;/strong&gt;. Inside of this &lt;strong&gt;node&lt;/strong&gt; we can find different &lt;strong&gt;children&lt;/strong&gt;, each of which represents a different part of the code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk580p1uaa8gvwx1r2tre.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk580p1uaa8gvwx1r2tre.png" alt="Abstract Syntax Tree Example" width="800" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see on the image the code is represented as a tree. There are different types of nodes and we can navigate through them 🚢:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F28yl6uqptggz6ok7vu86.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F28yl6uqptggz6ok7vu86.jpg" alt="AST Explorer - Playing with the Abstract Syntax Tree" width="800" height="597"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The nodes shown in the image, will be the ones we'll traverse when writing codemods! You can &lt;strong&gt;explore the AST&lt;/strong&gt; &lt;a href="https://astexplorer.net/#/gist/b97d1ac96cddeb4084c7191af54f6a8c/974175f63f814cbb465d44453dbe93c3ab93723d" rel="noopener noreferrer"&gt;using this link&lt;/a&gt; 👈&lt;/p&gt;

&lt;p&gt;That's pretty cool right? 🤓. Now that we understand how we can navigate AST, let's see how we can combine them with codemods to perform large-scale refactors 🚀.&lt;/p&gt;

&lt;h3&gt;
  
  
  Refactoring with codemods
&lt;/h3&gt;

&lt;p&gt;Time to put knowledge into 👨‍💻 practice. I'm going to use &lt;strong&gt;JavaScript&lt;/strong&gt; for the sake of example, but the fundamentals and concepts are the same for any other language.&lt;/p&gt;

&lt;p&gt;I will use the following tools to perform the refactor:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://astexplorer.net" rel="noopener noreferrer"&gt;AST Explorer&lt;/a&gt;: The online tool we saw before to explore and visualize AST.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/facebook/jscodeshift" rel="noopener noreferrer"&gt;JSCodeshift&lt;/a&gt;: A toolkit for running and writing codemods.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Understanding the transformation
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Input
&lt;/h5&gt;

&lt;p&gt;Image that in our codebase we are using the &lt;a href="https://lodash.com/docs/4.17.15#get" rel="noopener noreferrer"&gt;&lt;code&gt;lodash/get&lt;/code&gt;&lt;/a&gt; helper to &lt;strong&gt;safely extract values from objects&lt;/strong&gt;, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash/object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// get(object, path, [defaultValue])&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cartTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;attributes.items.total&lt;/span&gt;&lt;span class="dl"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Output
&lt;/h5&gt;

&lt;p&gt;Now, we decide that we want to &lt;strong&gt;stop using the lodash&lt;/strong&gt; helper in &lt;strong&gt;favour&lt;/strong&gt; of the &lt;strong&gt;native JavaScript&lt;/strong&gt; operator to safely extract values from objects: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining" rel="noopener noreferrer"&gt;&lt;code&gt;optional chaining&lt;/code&gt;&lt;/a&gt;, in this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cartTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;items&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="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see on the code 👆 our transformation needs to perform a few changes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Remove the import statement.&lt;/li&gt;
&lt;li&gt;Replace the &lt;code&gt;get&lt;/code&gt; helper with optional chaining.&lt;/li&gt;
&lt;li&gt;Provide a default value if the value is &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Writing the Codemod
&lt;/h4&gt;

&lt;p&gt;With the transformation clear in mind, it's time write the codemod! 👨‍💻. Go ahead and open the &lt;a href="https://astexplorer.net" rel="noopener noreferrer"&gt;AST Explorer&lt;/a&gt; and select:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Language: &lt;em&gt;JavaScript&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Parser: &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/parser&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Transform: &lt;em&gt;jscodeshift&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also use this &lt;a href="https://astexplorer.net/#/gist/f70fc3f955950b232c54263453d19a23/42786c4bb94016a96f9c56fcc76d287956c471eb" rel="noopener noreferrer"&gt;link&lt;/a&gt; to open the explorer 🕵️‍♀️ with the correct settings and code 👈. You should end up with a screen similar to this one:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6hy87o3hwcv371ihvhi.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6hy87o3hwcv371ihvhi.jpg" alt="AST Explorer - Writing the codemod" width="800" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Generating the AST
&lt;/h5&gt;

&lt;p&gt;The first thing we need to do is transforming the source code into an Abstract Syntax Tree, so we can traverse it.&lt;/p&gt;

&lt;p&gt;To transform code into an AST we will invoke the &lt;code&gt;j&lt;/code&gt; function from the &lt;code&gt;jscodeshift&lt;/code&gt; library, passing the code as a parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;babel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;transformer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;api&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="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;jscodeshift&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;j&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;source&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;h5&gt;
  
  
  Traversing the AST
&lt;/h5&gt;

&lt;p&gt;With the AST 🌳 already in place, we can start &lt;strong&gt;traversing it&lt;/strong&gt;. Use the explorer panel to &lt;strong&gt;find&lt;/strong&gt; the &lt;strong&gt;nodes&lt;/strong&gt; that represents the &lt;code&gt;import&lt;/code&gt; statement and the &lt;code&gt;get()&lt;/code&gt; helper calls. Did you find them? 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqa06uq4e0cda0ylreuxl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqa06uq4e0cda0ylreuxl.png" alt="AST Explorer - Traversing the AST" width="800" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are the two nodes we will need to transform to refactor our code: &lt;code&gt;ImportDeclaration&lt;/code&gt; and &lt;code&gt;CallExpression&lt;/code&gt;. &lt;br&gt;
Now, let's transform the nodes ♻️&lt;/p&gt;
&lt;h5&gt;
  
  
  Transforming the nodes
&lt;/h5&gt;

&lt;p&gt;As we saw before, we need to transform two nodes from the AST:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;CallExpression&lt;/code&gt;: Remove lodash &lt;code&gt;get&lt;/code&gt; helper with optional chaining.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ImportDeclaration&lt;/code&gt;: Remove the &lt;code&gt;get&lt;/code&gt; import statement.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the AST is completely &lt;strong&gt;transformed&lt;/strong&gt;, we will &lt;strong&gt;convert it&lt;/strong&gt; back &lt;strong&gt;to source code&lt;/strong&gt;.&lt;br&gt;
Let's break this down into steps ✅: &lt;/p&gt;
&lt;h6&gt;
  
  
  1. Find &lt;code&gt;get&lt;/code&gt; helper usages
&lt;/h6&gt;

&lt;p&gt;To find &lt;code&gt;get&lt;/code&gt; calls we will have to traverse the AST, &lt;strong&gt;finding&lt;/strong&gt; nodes with the type &lt;code&gt;CallExpression&lt;/code&gt; containing a &lt;code&gt;callee.name&lt;/code&gt; of &lt;code&gt;get&lt;/code&gt; 🔎&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CallExpression&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;callee&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  2. Replace &lt;code&gt;get&lt;/code&gt; with optional chaining and default value
&lt;/h6&gt;

&lt;p&gt;For every match, we will &lt;strong&gt;replace&lt;/strong&gt; the AST object to use &lt;strong&gt;optional chaining&lt;/strong&gt; and the default value ♻️&lt;br&gt;
For that, we will use the &lt;code&gt;replaceWith&lt;/code&gt; function, passing the new AST object as a parameter.&lt;/p&gt;

&lt;p&gt;I know the AST transformation might look a bit complex 😨, but it's just a combination of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ExpressionStatement&lt;/code&gt;: To replace the function call with an expression.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;LogicalExpression&lt;/code&gt;: To provide a default value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;OptionalMemberExpression&lt;/code&gt;: To build the optional chaining.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Identifier&lt;/code&gt;: To represent the source object.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pathItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.&lt;/span&gt;&lt;span class="dl"&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;lastPathItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pathItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nf"&gt;j&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;replaceWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;expressionStatement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;logicalExpression&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;??&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;optionalMemberExpression&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="nx"&gt;pathItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;param&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;optionalMemberExpression&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
              &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
              &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="nx"&gt;source&lt;/span&gt;
          &lt;span class="p"&gt;),&lt;/span&gt;
          &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lastPathItem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;),&lt;/span&gt; 
        &lt;span class="nx"&gt;fallback&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="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h6&gt;
  
  
  3. Find lodash/get import statements
&lt;/h6&gt;

&lt;p&gt;Once &lt;code&gt;get&lt;/code&gt; is removed, we can safely delete the import statements. But first, we will need to &lt;strong&gt;find&lt;/strong&gt; nodes with the type &lt;code&gt;ImportDeclaration&lt;/code&gt; containing a &lt;code&gt;source.value&lt;/code&gt; of &lt;code&gt;lodash/object&lt;/code&gt; 🔎&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ImportDeclaration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash/object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  4. Prune import statements
&lt;/h6&gt;

&lt;p&gt;To remove &lt;code&gt;get&lt;/code&gt; from the import statement, we will iterate over the matches &lt;strong&gt;filtering&lt;/strong&gt; the &lt;code&gt;specifiers&lt;/code&gt; array to remove the &lt;code&gt;get&lt;/code&gt; import. In case there are no more imports, we will remove the complete statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;node&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;importedModules&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;specifiers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;imported&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;imported&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;importedModules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;specifiers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;importedModules&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;j&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;remove&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;h6&gt;
  
  
  5. Convert AST back to source code
&lt;/h6&gt;

&lt;p&gt;Once all the transformations are applied, we need to convert the AST 🌳 object back to source code 🪄.&lt;br&gt;
For that we will use the following method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toSource&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Putting it all together
&lt;/h5&gt;

&lt;p&gt;Now it's time to put all the pieces together 🧩, so we can transform our code to the output we described before:&lt;/p&gt;

&lt;p&gt;Click here to see the snippet 👈&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;babel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;transformer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;api&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="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;jscodeshift&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;ast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;j&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nx"&gt;ast&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CallExpression&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;callee&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get&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="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;gt&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;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pathItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.&lt;/span&gt;&lt;span class="dl"&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;lastPathItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pathItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="nf"&gt;j&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;replaceWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;expressionStatement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;logicalExpression&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
              &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;??&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
              &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;optionalMemberExpression&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="nx"&gt;pathItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;param&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;gt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;optionalMemberExpression&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                    &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                  &lt;span class="p"&gt;),&lt;/span&gt;
                  &lt;span class="nx"&gt;source&lt;/span&gt;
                &lt;span class="p"&gt;),&lt;/span&gt;
                &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lastPathItem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
              &lt;span class="p"&gt;),&lt;/span&gt; 
              &lt;span class="nx"&gt;fallback&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="p"&gt;})&lt;/span&gt;

    &lt;span class="nx"&gt;ast&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ImportDeclaration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash/object&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;gt&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="nx"&gt;importedModules&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;specifiers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;imported&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;gt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;imported&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;importedModules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;specifiers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;importedModules&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;j&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;remove&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toSource&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;Finally we can see the codemod in in action 💖&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2sx9ezsmjmptip14m3mz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2sx9ezsmjmptip14m3mz.jpg" alt="AST Explorer Codemod Transformation" width="800" height="551"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Testing codemods
&lt;/h4&gt;

&lt;p&gt;Codemods can be potentially &lt;strong&gt;dangerous&lt;/strong&gt; and they can &lt;strong&gt;break&lt;/strong&gt; your code 🐛. Luckily, you can &lt;strong&gt;write unit tests&lt;/strong&gt; for them to &lt;strong&gt;ensure everything works&lt;/strong&gt; as expected ✅ &lt;strong&gt;before running&lt;/strong&gt; them on the codebase.&lt;/p&gt;

&lt;p&gt;The library I used on this article provides some utilities to &lt;strong&gt;test&lt;/strong&gt; codemods. You can find more information about them &lt;a href="https://github.com/facebook/jscodeshift/blob/main/README.md#unit-testing" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In case you're using a different tool with no testing utilities, you can always &lt;strong&gt;test&lt;/strong&gt; codemods by &lt;strong&gt;running&lt;/strong&gt; them over a mock file &lt;strong&gt;asserting&lt;/strong&gt; the output.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Automating codebase changes can be &lt;strong&gt;difficult&lt;/strong&gt; the first time you do it, but it's totally worth it. &lt;br&gt;
It will &lt;strong&gt;save&lt;/strong&gt; you a &lt;strong&gt;lot of time&lt;/strong&gt; 🚀 and it will prevent you from introducing bugs 🐛.&lt;/p&gt;

&lt;p&gt;Automate all the things! 🙌&lt;/p&gt;

</description>
      <category>codemods</category>
      <category>ast</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>Using generators to improve developer productivity</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Sat, 28 May 2022 15:22:38 +0000</pubDate>
      <link>https://dev.to/carloscuesta/using-generators-to-improve-developer-productivity-2i7i</link>
      <guid>https://dev.to/carloscuesta/using-generators-to-improve-developer-productivity-2i7i</guid>
      <description>&lt;p&gt;A few weeks ago at &lt;a href="http://n26.com" rel="noopener noreferrer"&gt;N26&lt;/a&gt;, we did &lt;em&gt;&lt;a href="https://medium.com/insiden26/getting-stuff-done-days-at-n26-a70105e6b9c9" rel="noopener noreferrer"&gt;get stuff done week&lt;/a&gt;&lt;/em&gt;, during this time the product and engineering teams have the opportunity to try or build anything we like.&lt;/p&gt;

&lt;p&gt;I decided to spend the week adding a &lt;strong&gt;code generation&lt;/strong&gt; tool to the Web project&lt;br&gt;
automating a bit of the coding work while improving the developer experience.&lt;/p&gt;

&lt;p&gt;In this article, I'll walk through the experience and the outcome of implementing a tool to generate code 👨‍💻&lt;/p&gt;
&lt;h3&gt;
  
  
  What is a code generator?
&lt;/h3&gt;

&lt;p&gt;A code generator is a tool that given a set of rules and inputs creates code, files, and folders.&lt;/p&gt;

&lt;p&gt;To name a few popular ones 👇&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://create-react-app.dev" rel="noopener noreferrer"&gt;create-react-app&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nextjs.org/docs/api-reference/create-next-app" rel="noopener noreferrer"&gt;create-next-app&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/expo/create-react-native-app" rel="noopener noreferrer"&gt;create-react-native-app&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of them will &lt;strong&gt;create code&lt;/strong&gt; based on specific &lt;strong&gt;rules&lt;/strong&gt; taking into account the &lt;strong&gt;inputs&lt;/strong&gt; provided by the &lt;strong&gt;user&lt;/strong&gt;. Here's a simple example 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcarloscuesta%2Fimage%2Fupload%2Fv1653235448%2Fcode-generators-teaser_hlr2qm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcarloscuesta%2Fimage%2Fupload%2Fv1653235448%2Fcode-generators-teaser_hlr2qm.png" alt="Code generator teaser"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What's the problem?
&lt;/h3&gt;

&lt;p&gt;Imagine, you start the day working on a new task and you need to create a feature, before writing any code you will need to consider a few things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Folder structure and architecture 📂&lt;/li&gt;
&lt;li&gt;Naming files convention 📑&lt;/li&gt;
&lt;li&gt;Where to put the feature 🗂&lt;/li&gt;
&lt;li&gt;How you should write the tests 🧪&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'm sure the project you're working on has a list of  &lt;strong&gt;conventions and patterns defined&lt;/strong&gt; that explains how  you should work in the codebase.&lt;/p&gt;

&lt;p&gt;However, every time you are &lt;strong&gt;going through this process&lt;/strong&gt; it &lt;strong&gt;requires&lt;/strong&gt; you to &lt;strong&gt;think&lt;/strong&gt; 🤔 about those conventions to &lt;strong&gt;make the decision&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Where is the source of truth? 🧐&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Documentation 📝&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://carloscuesta.me/blog/writing-architecture-decision-records" rel="noopener noreferrer"&gt;Architecture Decision Records&lt;/a&gt; 🏗&lt;/li&gt;
&lt;li&gt;Similar files on the codebase 🕵️&lt;/li&gt;
&lt;li&gt;A teammate 🧑‍💻&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As software engineers we work very hard to not repeat code building abstractions, automating manual workflows...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about writing code&lt;/strong&gt;? In the same way, we advocate for automating processes such as deployments, we should also make an effort to the &lt;em&gt;non-creative part&lt;/em&gt; of coding such as scaffolding.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why you should use them?
&lt;/h3&gt;

&lt;p&gt;Generating code will save you time ⏰ and will increase the productivity of the team 📈&lt;/p&gt;
&lt;h4&gt;
  
  
  Developer experience
&lt;/h4&gt;

&lt;p&gt;In a team that is constantly growing it is important to make sure that everyone is aligned and able to build things efficiently.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://carloscuesta.me/blog/front-end-tooling-guide-to-improve-developer-experience#the-impact-of-having-a-great-dx" rel="noopener noreferrer"&gt;Having a great Developer Experience&lt;/a&gt; will boost the confidence a developer has with the codebase.&lt;/p&gt;

&lt;p&gt;Trust, empowers people to be more productive and agile ⚡️, to name a few of the many benefits you'll get by using generators:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensure everyone is doing things &lt;em&gt;"as expected"&lt;/em&gt; according to the project conventions 💖&lt;/li&gt;
&lt;li&gt;Reduce friction when working with the codebase 🤔&lt;/li&gt;
&lt;li&gt;Ease the &lt;strong&gt;onboarding&lt;/strong&gt; of new joiners 🆕&lt;/li&gt;
&lt;li&gt;Decrease the development time 🚀&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Decision fatigue
&lt;/h4&gt;

&lt;p&gt;The most important thing is that your teammates will not have to spend time on &lt;em&gt;low-value&lt;/em&gt; decisions, such as deciding how a component is structured.&lt;/p&gt;

&lt;p&gt;Turns out our decision-making process gets worse the more decisions we make.&lt;/p&gt;

&lt;p&gt;This is called &lt;a href="https://en.wikipedia.org/wiki/Decision_fatigue" rel="noopener noreferrer"&gt;decision fatigue&lt;/a&gt;. For example, Steve Jobs, &lt;a href="https://www.businessinsider.com/steve-jobs-productivity-hack-reduce-decision-fatigue-psychologists-2021-11" rel="noopener noreferrer"&gt;limited his everyday clothing&lt;/a&gt; down to one outfit to avoid making a decision.&lt;/p&gt;
&lt;h3&gt;
  
  
  Using code generators
&lt;/h3&gt;

&lt;p&gt;Sounds good right 😍? Let's take a look at how we can implement code generators in our project 👀&lt;/p&gt;
&lt;h4&gt;
  
  
  Choosing the right tool
&lt;/h4&gt;

&lt;p&gt;I didn't want to &lt;strong&gt;reinvent the wheel&lt;/strong&gt;, my focus was set on the outcome of generating code, not building a tool that solves this problem.&lt;/p&gt;

&lt;p&gt;There are a lot of Open Source projects that will do an awesome job generating code. Here's the list of the ones I considered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://hygen.io" rel="noopener noreferrer"&gt;Hygen&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://plopjs.com" rel="noopener noreferrer"&gt;Plop&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://yeoman.io" rel="noopener noreferrer"&gt;Yeoman&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I decided to go with &lt;strong&gt;Hygen&lt;/strong&gt; at N26 because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Great monorepo support&lt;/li&gt;
&lt;li&gt;Easy to maintain. You'll only need to care about &lt;code&gt;.EJS&lt;/code&gt; template files and prompts.&lt;/li&gt;
&lt;li&gt;No configuration required&lt;/li&gt;
&lt;li&gt;High value with low effort, writing a generator is very simple.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Plop&lt;/strong&gt; is also a great tool but creating a generator is more complex since you need to spend more time writing code and the tool demands extra time on the configuration part compared to Hygen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yeoman&lt;/strong&gt; is another valid option, but maintaining generators requires you to manage and publish packages to a registry and I would say the use-case of this tool is more suited to scaffolding projects instead of smaller parts of a codebase.&lt;/p&gt;
&lt;h4&gt;
  
  
  Getting started
&lt;/h4&gt;

&lt;p&gt;It's time to create our code generator 🥳. First, install Hygen as a devDependency in your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; hygen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Define a script inside the &lt;code&gt;package.json&lt;/code&gt; to use the script binary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"generators"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"hygen"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can run Hygen through the &lt;code&gt;generators&lt;/code&gt; script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm run generators
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the command, you'll see that Hygen is telling us we don't have any generators.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: please specify a generator.
Hygen v6.2.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, hygen will read the generator files from a folder named &lt;code&gt;_templates&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you want to change this behavior, create a file named &lt;code&gt;.hygen.js&lt;/code&gt; at the root level of your project, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;generators&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Creating a generator
&lt;/h4&gt;

&lt;p&gt;A generator is composed of &lt;strong&gt;one or more actions&lt;/strong&gt;, every action contains two items:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Templates&lt;/strong&gt;: &lt;code&gt;*.ejs&lt;/code&gt; 🧩&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prompts&lt;/strong&gt;: &lt;code&gt;prompt.js&lt;/code&gt; ⁉️&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's build a simple generator that creates a React component 🤓. At the end of the post, you'll find a GitHub repository with a lot of different examples 👏&lt;/p&gt;

&lt;p&gt;The first thing we need to create is the generator folder that will contain the action:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generator: &lt;code&gt;component&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Action: &lt;code&gt;react&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; generators/component/react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Templates
&lt;/h5&gt;

&lt;p&gt;Template files define the code that will be created after running the generator, these files are written using a template language called &lt;a href="https://ejs.co" rel="noopener noreferrer"&gt;Embedded JavaScript templates&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Every template starts with a &lt;strong&gt;&lt;a href="https://www.hygen.io/docs/templates/#frontmatter" rel="noopener noreferrer"&gt;frontmatter&lt;/a&gt; header&lt;/strong&gt;. On this header, you will define the metadata of the template using the &lt;a href="https://www.hygen.io/docs/templates#all-frontmatter-properties" rel="noopener noreferrer"&gt;following properties&lt;/a&gt; 🔍&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Property&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Usage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;to&lt;/td&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;The destination of the file once compiled.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;from&lt;/td&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;Use an external file as template.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;force&lt;/td&gt;
&lt;td&gt;Boolean&lt;/td&gt;
&lt;td&gt;Overwrite existing files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unless_exists&lt;/td&gt;
&lt;td&gt;Boolean&lt;/td&gt;
&lt;td&gt;Execute the generator unless file already exists.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;inject&lt;/td&gt;
&lt;td&gt;Boolean&lt;/td&gt;
&lt;td&gt;Inject the contents into an existing file instead of creating a new one.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;after&lt;/td&gt;
&lt;td&gt;Regex&lt;/td&gt;
&lt;td&gt;Inject the template after the regex&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;before&lt;/td&gt;
&lt;td&gt;Regex&lt;/td&gt;
&lt;td&gt;Inject the template before the regex&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;prepend&lt;/td&gt;
&lt;td&gt;Boolean&lt;/td&gt;
&lt;td&gt;Inject the template at the start of the file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;append&lt;/td&gt;
&lt;td&gt;Boolean&lt;/td&gt;
&lt;td&gt;Inject the template at the end of the file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;at_line&lt;/td&gt;
&lt;td&gt;Regex&lt;/td&gt;
&lt;td&gt;Inject the template at the specified line number&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;skip_if&lt;/td&gt;
&lt;td&gt;Regex&lt;/td&gt;
&lt;td&gt;Skip injection if regex matches.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;eof_last&lt;/td&gt;
&lt;td&gt;Boolean&lt;/td&gt;
&lt;td&gt;Trim the newline from the end of the injection.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sh&lt;/td&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;Trigger a shell command after compiling the template&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now, let's add a template file named &lt;code&gt;index.ejs&lt;/code&gt; inside the &lt;code&gt;react&lt;/code&gt; action folder we created previously:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;generators/component/react/index.ejs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As I mentioned before, we want to create a React component, so we need to specify the location of the file where the component is going to be created.&lt;/p&gt;

&lt;p&gt;We can do that using the &lt;code&gt;to&lt;/code&gt; property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
to: src/components/&amp;lt;%= h.changeCase.pascalCase(name) %&amp;gt;/index.js
---
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we're using a variable called &lt;code&gt;name&lt;/code&gt; on the header. This value will be provided by the prompts of the generator ⁉️&lt;/p&gt;

&lt;p&gt;Then, we need to write the body of the template, where we define the code that will be generated once the template is compiled. I'm reusing the &lt;code&gt;name&lt;/code&gt; variable in the body as well to create the name and export of the component ✨&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
to: src/components/&amp;lt;%= h.changeCase.pascalCase(name) %&amp;gt;/index.js
---

const &amp;lt;%= h.changeCase.pascalCase(name) %&amp;gt; = () =&amp;gt; (
  &amp;lt;section&amp;gt;
    &amp;lt;h1&amp;gt;Hey! 👋&amp;lt;/h1&amp;gt;
    &amp;lt;h2&amp;gt;&amp;lt;code&amp;gt;&amp;lt;%= h.changeCase.pascalCase(name) %&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/h2&amp;gt;
  &amp;lt;/section&amp;gt;
)

export default &amp;lt;%= h.changeCase.pascalCase(name) %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Prompts
&lt;/h5&gt;

&lt;p&gt;In case you need to ask for user input, optionally you can use a prompt file. This is very useful to customize the output of the generator. Prompts are defined using a library named &lt;a href="https://github.com/enquirer/enquirer" rel="noopener noreferrer"&gt;Enquirer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Inside of the same &lt;code&gt;react&lt;/code&gt; 📁, we will create a &lt;code&gt;prompt.js&lt;/code&gt; file to ask for the variable defined as &lt;code&gt;name&lt;/code&gt; in the template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&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;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;input&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Write the name of your component&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a ton of different input types available 🔡, take a &lt;a href="https://github.com/enquirer/enquirer#-built-in-prompts" rel="noopener noreferrer"&gt;look at this link to get the complete list&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now it's time to finally run our generator 🥳 using the script along with the &lt;strong&gt;name&lt;/strong&gt; and the &lt;strong&gt;action&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm run generators component react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll be asked for the prompts and finally the magic will happen! 🦄&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcarloscuesta%2Fimage%2Fupload%2Fv1653117649%2Fcode-generators-demo_e0ngqz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcarloscuesta%2Fimage%2Fupload%2Fv1653117649%2Fcode-generators-demo_e0ngqz.png" alt="Code generator demo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Demo
&lt;/h4&gt;

&lt;p&gt;Take a look at &lt;a href="https://github.com/carloscuesta/codegenerators-demo" rel="noopener noreferrer"&gt;carloscuesta/codegenerators-demo&lt;/a&gt; 🕹 if you want to play and see more complex examples!&lt;/p&gt;

&lt;h3&gt;
  
  
  Automate all the things!
&lt;/h3&gt;

&lt;p&gt;It's time for you to &lt;strong&gt;find out repetitive tasks&lt;/strong&gt; and patterns to &lt;strong&gt;extract them into a generator&lt;/strong&gt;! 🚀&lt;/p&gt;

&lt;p&gt;I'm very happy with the &lt;strong&gt;productivity boost&lt;/strong&gt; and the &lt;strong&gt;consistency&lt;/strong&gt; that a tool like this can bring to a team ❤️&lt;/p&gt;

</description>
      <category>generators</category>
      <category>productivity</category>
      <category>dx</category>
    </item>
    <item>
      <title>Writing Architecture Decision Records</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Sat, 12 Mar 2022 11:58:05 +0000</pubDate>
      <link>https://dev.to/carloscuesta/writing-architecture-decision-records-3582</link>
      <guid>https://dev.to/carloscuesta/writing-architecture-decision-records-3582</guid>
      <description>&lt;p&gt;An &lt;strong&gt;architecture decision record&lt;/strong&gt; also known as &lt;em&gt;ADR&lt;/em&gt;, is a document that captures an important architecture decision, including the context of how the decision was made and the consequences of that change.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are the benefits?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Sharing context
&lt;/h4&gt;

&lt;p&gt;ADRs capture a decision at the time it's being made. They're the outcome of all of those asynchronous discussions in Slack that'll be lost over time ⏰&lt;/p&gt;

&lt;p&gt;When you materialize them into docs, you're allowing your future teammates, to get the context for this specific change. So they'll be able to understand the issues, the decision that was taken and the impact it had on the project.&lt;/p&gt;

&lt;p&gt;Writing docs will &lt;strong&gt;help&lt;/strong&gt; you and your &lt;strong&gt;future&lt;/strong&gt; peers in the &lt;strong&gt;long run&lt;/strong&gt;! 🚀&lt;/p&gt;

&lt;h4&gt;
  
  
  Alignment
&lt;/h4&gt;

&lt;p&gt;Writing decisions leads the team to consensus and ensures that all the assumptions of a change are clear 📏.&lt;/p&gt;

&lt;p&gt;This will have a positive impact after the architecture decision has been implemented. Everyone on the team will have a deep understanding which will help on moving forward faster with the new approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  Writing ADRs
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Define a template
&lt;/h4&gt;

&lt;p&gt;When writing them, a common practice is to have a template to write ✍🏼 all the documents in the same way.&lt;/p&gt;

&lt;p&gt;You'll find a lot of templates online, but here's the one I use it's very simple but easy to write.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gh"&gt;# Title&lt;/span&gt;
&lt;span class="p"&gt;
-&lt;/span&gt; Status: &lt;span class="c"&gt;&amp;lt;!-- PENDING/APPROVED/DEPRECATED --&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; Date: DD/MM/YYYY
&lt;span class="p"&gt;-&lt;/span&gt; RFC: &lt;span class="c"&gt;&amp;lt;!-- Link to the RFC that ended up into the ADR --&amp;gt;&lt;/span&gt;

&lt;span class="gu"&gt;## Context&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- What is the issue that is motivating the decision or change? --&amp;gt;&lt;/span&gt;

&lt;span class="gu"&gt;## Decision&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- What is the change we're doing? --&amp;gt;&lt;/span&gt;

&lt;span class="gu"&gt;## Consequences&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- What becomes easier or more difficult because of this change? --&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Store them next to the code
&lt;/h4&gt;

&lt;p&gt;I believe that &lt;strong&gt;documentation&lt;/strong&gt; should be &lt;strong&gt;next&lt;/strong&gt; to the &lt;strong&gt;code&lt;/strong&gt; to make it easy to contribute to it. Whenever you add an external tool you're increasing the complexity to contribute and reducing the visibility of the docs 👀.&lt;/p&gt;

&lt;p&gt;The solution is simple, check out the ADRs into your project repository and keep things simple and clean ✨&lt;/p&gt;

&lt;p&gt;I recommend you to create a folder 📁 where you store all the decision records, for example: &lt;code&gt;docs/adrs/&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use a naming convention
&lt;/h4&gt;

&lt;p&gt;When saving the files use a naming convention &lt;code&gt;title-of-the-adr-DD-MM-YYYY.md&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Write more ADRs&lt;/strong&gt; 😜, if you're not doing it yet, it's never too late to start!&lt;/p&gt;

&lt;p&gt;In my experience as a team grows larger and a codebase becomes more complex, architecture decision records are a &lt;strong&gt;great tool&lt;/strong&gt; to help your current and future team ❤️&lt;/p&gt;

</description>
      <category>adr</category>
      <category>adrs</category>
      <category>documentation</category>
    </item>
    <item>
      <title>Composing UIs with Finite State Machines</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Tue, 04 May 2021 16:13:26 +0000</pubDate>
      <link>https://dev.to/carloscuesta/composing-uis-with-finite-state-machines-39ak</link>
      <guid>https://dev.to/carloscuesta/composing-uis-with-finite-state-machines-39ak</guid>
      <description>&lt;p&gt;&lt;strong&gt;Finite State Machines&lt;/strong&gt;, often abbreviated as &lt;a href="https://en.wikipedia.org/wiki/Finite-state_machine"&gt;&lt;strong&gt;FSM&lt;/strong&gt;&lt;/a&gt; is a mathematical computation model that could be useful for building user interfaces, especially nowadays that &lt;strong&gt;front-end&lt;/strong&gt; apps are becoming &lt;strong&gt;much more complex&lt;/strong&gt; due to the nature of the problems that they solve. Did you know that 🧑‍🚀 &lt;a href="https://twitter.com/jason_mayes/status/1267227834096861184"&gt;SpaceX used JavaScript&lt;/a&gt; for the spaceship flight interface? 🤯.&lt;/p&gt;

&lt;p&gt;In this article, I'm going to explain the benefits of composing user interfaces using &lt;em&gt;finite state machines&lt;/em&gt;. Let's dive in! 🤿&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a finite state machine?
&lt;/h3&gt;

&lt;p&gt;A finite state machine is an &lt;strong&gt;abstract machine&lt;/strong&gt; that can be in exactly &lt;strong&gt;one&lt;/strong&gt; of a &lt;strong&gt;finite number of states&lt;/strong&gt; at any given time. The FSM can &lt;strong&gt;change&lt;/strong&gt; from one &lt;strong&gt;state&lt;/strong&gt; to another, this change is called a &lt;strong&gt;transition&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A FSM is defined by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Σ&lt;/em&gt;: The input alphabet.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;S&lt;/em&gt; : A finite, non-empty set of states.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;δ&lt;/em&gt; : The state-transition function (&lt;em&gt;δ&lt;/em&gt;: &lt;em&gt;S&lt;/em&gt; x &lt;em&gt;Σ&lt;/em&gt; -&amp;gt; &lt;em&gt;S&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;s0&lt;/em&gt; : The initial state, an element of &lt;em&gt;S&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;F&lt;/em&gt; : The set of accepting states.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you're probably like &lt;em&gt;wtf&lt;/em&gt; 🤯, this sounds scary 😱 and academic, right? Let's try to illustrate this definition with a real world example to understand it better.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding a FSM
&lt;/h3&gt;

&lt;p&gt;The world is full of finite state machines, in fact, you are using them every day, but probably you didn't think of them as such. I'm sure that after reading the post you'll start pointing them in the real world, trust me I'm doing it right now 😂&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;traffic light&lt;/strong&gt; 🚦 is a simple example to understand FSM. For the sake of this consider that our traffic light has 3 colors.&lt;/p&gt;

&lt;p&gt;At any point in time, the traffic light will be on one of the following scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🟢 Green&lt;/li&gt;
&lt;li&gt;🟡 Yellow&lt;/li&gt;
&lt;li&gt;🔴 Red&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those scenarios are called &lt;strong&gt;states&lt;/strong&gt; and because the set is limited to &lt;em&gt;3 states&lt;/em&gt; we can say that is &lt;strong&gt;finite&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;initial state&lt;/strong&gt; of the machine is 🟢 &lt;em&gt;green&lt;/em&gt; and whenever one of the lights is &lt;strong&gt;on&lt;/strong&gt; the &lt;strong&gt;output&lt;/strong&gt; of the other ones is &lt;strong&gt;off&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The state will change in response to an &lt;strong&gt;input&lt;/strong&gt;, that in our case is a timer, through a &lt;strong&gt;transition&lt;/strong&gt;. The transition is a mapping that defines the path of our state.&lt;/p&gt;

&lt;p&gt;Let's represent the traffic light FSM on a graphic, so we can visualize the state transitions and understand how the machine works. Usually, you'll see the FSM represented like this 📸:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7XhtQNeH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1619731418/traffic-light-fms_bu2xcy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7XhtQNeH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1619731418/traffic-light-fms_bu2xcy.png" alt="Traffic Light FSM Model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try to link this example with the mathematical definition we introduced before! Seems easier right? ☺️&lt;/p&gt;

&lt;p&gt;Ok 🆒! I explained how a traffic light works, but now what? How can we use this model to compose better UIs? 🤔. Now that we understand how FSM works, we're going to code a JavaScript application to see the advantages and benefits! 👀&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing a FSM with JavaScript
&lt;/h3&gt;

&lt;p&gt;The traffic light is a simple example to understand the concept of FSM. However, to showcase all the benefits and the potential of this concept, we're going to build something a little bit more complex. Such as a UI that could potentially fail due to external circumstances.&lt;/p&gt;

&lt;p&gt;The application that we're going to build is a UI with a button, whenever the button is pressed we're going to call an external API and we're going to render the response of the API in our app.&lt;/p&gt;

&lt;h4&gt;
  
  
  Defining the state machine
&lt;/h4&gt;

&lt;p&gt;Before starting to code, as we've seen in our previous example, the &lt;strong&gt;first thing&lt;/strong&gt; we need to do is &lt;strong&gt;defining&lt;/strong&gt; our &lt;strong&gt;state machine&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is actually the first benefit. Why? Because from the first moment you have to define the FSM and this process helps you to &lt;strong&gt;plan and contemplate all the possible states&lt;/strong&gt; of your UI. So basically you won't miss any edge case.&lt;/p&gt;

&lt;p&gt;This way of approaching a problem is called 🔝 ⬇️ &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design"&gt;top-down&lt;/a&gt;&lt;/strong&gt; approach. Instead of trying to solve a specific part of the problem without understanding it fully ⬇️ 🆙 &lt;strong&gt;bottom-up&lt;/strong&gt;, first, you define the whole model of your application.&lt;/p&gt;

&lt;p&gt;This would be the &lt;strong&gt;statechart&lt;/strong&gt; of the application we're going to build:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G1_9zF0T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1619864341/fsm-ui-app_qzrzeu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G1_9zF0T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1619864341/fsm-ui-app_qzrzeu.png" alt="FSM Application UI State Chart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we defined &lt;strong&gt;all&lt;/strong&gt; the &lt;strong&gt;possible states&lt;/strong&gt; of the user interface and also the transitions between them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Idle&lt;/code&gt;: The initial state.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Fetching&lt;/code&gt;: The state where the UI is fetching the API.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Fulfilled&lt;/code&gt;: The state when the API fetch succeeds.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rejected&lt;/code&gt;: The state when the API fetch fails.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, we can define for each state, the &lt;strong&gt;output&lt;/strong&gt; and &lt;strong&gt;behaviour&lt;/strong&gt; of our application. This makes our UI &lt;strong&gt;deterministic&lt;/strong&gt; and what this means is that given the current state and an input you'll know what the next state is going to be all the time. When you control every state, you are free of bugs 🐛.&lt;/p&gt;

&lt;p&gt;Let's build the wireframes 🎨 to define the &lt;em&gt;output&lt;/em&gt; of the application:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KgiZ5GBV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1619889747/fsm-ui-wireframe_hcpgkm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KgiZ5GBV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1619889747/fsm-ui-wireframe_hcpgkm.png" alt="FSM Application Wireframe"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our wireframes, implement all the states that are defined by our FSM. We're ready to move on with coding! 👏.&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementing the FSM
&lt;/h4&gt;

&lt;p&gt;I'm going to build the FSM using &lt;strong&gt;plain JavaScript&lt;/strong&gt; only. Why? I'll answer this question after the implementation 👀.&lt;/p&gt;

&lt;p&gt;The first thing we're going to define is our &lt;strong&gt;input alphabet&lt;/strong&gt; &lt;em&gt;Σ&lt;/em&gt;. Based on the &lt;strong&gt;statechart&lt;/strong&gt; we designed before. Inputs are &lt;strong&gt;events&lt;/strong&gt; that will cause a state transition in our FSM. Our &lt;em&gt;Σ&lt;/em&gt; will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;EVENTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;FETCH&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;FETCH&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;RESOLVE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;RESOLVE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;REJECT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;REJECT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we are going to define our &lt;strong&gt;set of states&lt;/strong&gt; &lt;em&gt;S&lt;/em&gt;. Also, as we defined, we should set the &lt;strong&gt;initial state&lt;/strong&gt; to Idle as &lt;em&gt;S0&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;STATE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;IDLE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;IDLE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;FETCHING&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;FETCHING&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;FULFILLED&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;FULFILLED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;REJECTED&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;REJECTED&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;STATE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;IDLE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally we're going to combine all those pieces into the FSM. Defining the &lt;strong&gt;transitions&lt;/strong&gt; &lt;em&gt;δ&lt;/em&gt; between every &lt;strong&gt;state&lt;/strong&gt; in response to the &lt;strong&gt;inputs&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stateMachine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;initial&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;states&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;STATE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;IDLE&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;on&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;EVENTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FETCH&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;STATE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FETCHING&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;STATE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FETCHING&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;on&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;EVENTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;RESOLVE&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;STATE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FULFILLED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;EVENTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;REJECT&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;STATE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;REJECTED&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;STATE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FULFILLED&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;on&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;EVENTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FETCH&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;STATE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FETCHING&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;STATE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;REJECTED&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;on&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;EVENTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FETCH&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;STATE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FETCHING&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;FSM&lt;/strong&gt; is ready to be used! 🥳.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Why did I implement the FSM using plain JavaScript?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Because I want to show how simple it is to create one. As well as to show that FSM is totally decoupled from any library or framework 💯. They don't know anything about rendering, instead, they define the &lt;strong&gt;application state flow&lt;/strong&gt;. This is one of the best things about composing UIs with FSM 😍.&lt;/p&gt;

&lt;p&gt;You can &lt;strong&gt;abstract&lt;/strong&gt; and &lt;strong&gt;decouple&lt;/strong&gt; the whole flow from any framework 🙌. You can use this FSM with any library, such as &lt;em&gt;React&lt;/em&gt;, &lt;em&gt;React-Native&lt;/em&gt;, &lt;em&gt;Vue&lt;/em&gt;, &lt;em&gt;Svelte&lt;/em&gt;, &lt;em&gt;Preact&lt;/em&gt;, &lt;em&gt;Angular&lt;/em&gt;...&lt;/p&gt;

&lt;h4&gt;
  
  
  Demo time 🕹
&lt;/h4&gt;

&lt;p&gt;To see our FSM in action I built a demo app with &lt;code&gt;React&lt;/code&gt; so I can show you how our application works! The example is a dummy app that calls a &lt;em&gt;Pokemon API&lt;/em&gt; and renders the result, a common task in front-end development nowadays.&lt;/p&gt;

&lt;p&gt;Take a look at the CodeSandbox below 👇 and try to break the UI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Disable your Internet connection and try to click the button 🔌&lt;/li&gt;
&lt;li&gt;Try to click the button multiple times 👈&lt;/li&gt;
&lt;li&gt;Simulate a slow network connection 🐌&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first thing I did was to define all the UI for every state in our machine. Using a simple &lt;code&gt;switch&lt;/code&gt; statement to do the conditional rendering:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&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;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;STATES&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;IDLE&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="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;STATES&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;FETCHING&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="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;STATES&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;FULFILLED&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="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;STATES&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;REJECTED&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="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&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;Once our app knows how to render every state of the machine, we need to define the &lt;em&gt;transitions&lt;/em&gt; between the states in response to &lt;em&gt;events&lt;/em&gt; (inputs). Remember that our FSM has the following inputs: &lt;code&gt;Fetch&lt;/code&gt;, &lt;code&gt;Resolve&lt;/code&gt; and &lt;code&gt;Reject&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this case, I'm using a &lt;a href="https://github.com/cassiozen/useStateMachine"&gt;&lt;code&gt;useStateMachine&lt;/code&gt;&lt;/a&gt; hook from a library, just to avoid having to implement the not-so-relevant React part of the state handling. This hook receives the state machine we defined before as a configuration argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPokemon&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useStateMachine&lt;/span&gt;&lt;span class="p"&gt;()(&lt;/span&gt;&lt;span class="nx"&gt;stateMachine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The hook exposes the &lt;code&gt;state&lt;/code&gt; that is an object which contains the current state of the machine we defined and the &lt;code&gt;send&lt;/code&gt; function which is the state transition function (&lt;em&gt;δ&lt;/em&gt;: &lt;em&gt;S&lt;/em&gt; x &lt;em&gt;Σ&lt;/em&gt; -&amp;gt; &lt;em&gt;S&lt;/em&gt;). Also, we have a &lt;code&gt;pokemon&lt;/code&gt; state variable to save the API response.&lt;/p&gt;

&lt;p&gt;So, to transition from a state to another, we will call the &lt;code&gt;send&lt;/code&gt; Function passing an &lt;code&gt;Input&lt;/code&gt; as an argument.&lt;/p&gt;

&lt;p&gt;As you can see we have a &lt;code&gt;onFetchPokemon&lt;/code&gt; function to make the API request. As soon as you click the Button, we will send a &lt;code&gt;FETCH&lt;/code&gt; input and as a result of this, we will transition the state to &lt;code&gt;Fetching&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If there's an error, we're going to catch it and send a &lt;code&gt;REJECT&lt;/code&gt; input to transition the state to &lt;code&gt;Rejected&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If everything works well we will save the response into the &lt;code&gt;pokemon&lt;/code&gt; state variable and then send a &lt;code&gt;RESOLVE&lt;/code&gt; input to transition the state to &lt;code&gt;Resolved&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&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;onFetchPokemon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&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;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;EVENTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FETCH&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;pokedexRandomNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;151&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;pokemon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetchPokemon&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokedexRandomNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="nx"&gt;setPokemon&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;EVENTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;RESOLVE&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;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;EVENTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;REJECT&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The UI already knows what they need to render on every state, so basically, we've got all the possible cases covered 🎉. We're 100% free of bugs! 💯&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits
&lt;/h3&gt;

&lt;p&gt;Let's do a quick summary of the benefits of composing user interfaces with FSM:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Contemplate and plan all the possible states of the application 🧠&lt;/li&gt;
&lt;li&gt;Document the application flow, to make it more accessible to non-dev people 📝&lt;/li&gt;
&lt;li&gt;Predictable and declarative UIs 📖&lt;/li&gt;
&lt;li&gt;Makes code bases easier to maintain 💖&lt;/li&gt;
&lt;li&gt;No unexpected bugs 🐛&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Libraries
&lt;/h3&gt;

&lt;p&gt;I didn't use any library on purpose to build the FSM, but I would really recommend looking at the following tools If you plan to incorporate them into production:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://xstate.js.org"&gt;Xstate&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://thisrobot.life"&gt;Robot&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>fsm</category>
    </item>
    <item>
      <title>Functional programming in JavaScript</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Wed, 24 Mar 2021 16:06:16 +0000</pubDate>
      <link>https://dev.to/carloscuesta/functional-programming-in-javascript-fga</link>
      <guid>https://dev.to/carloscuesta/functional-programming-in-javascript-fga</guid>
      <description>&lt;p&gt;Functional programming, often abbreviated as &lt;strong&gt;FP&lt;/strong&gt; has become a 🔥 popular trend over the last years in the JavaScript community, but it was created on the &lt;em&gt;1950s&lt;/em&gt;, long way before JavaScript 😅&lt;/p&gt;

&lt;p&gt;As soon as you start searching &lt;em&gt;"functional programming"&lt;/em&gt; on the internet, you're going to hit a brick wall of academic terms that are really intimidating 😱 for someone who is learning, don't be scared by those concepts, it's not as hard as it seems.&lt;/p&gt;

&lt;p&gt;In this article I'm going to explain in a friendly and simple way, the basics of this paradigm applied to JavaScript. Let's get started 🤓&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Functional programming?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Functional programming&lt;/strong&gt; is a programming &lt;strong&gt;paradigm&lt;/strong&gt;, a way of thinking about code, that consists of building software by &lt;strong&gt;composing functions&lt;/strong&gt;. Functions are the center of &lt;strong&gt;FP&lt;/strong&gt;, hence its name.&lt;/p&gt;

&lt;p&gt;Does that mean &lt;em&gt;functional&lt;/em&gt; is about just programming with the &lt;code&gt;function&lt;/code&gt; keyword? Absolutely no! It's &lt;strong&gt;how we use&lt;/strong&gt; those functions what makes our code &lt;em&gt;functional&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;functional&lt;/em&gt; paradigm is based on the following principles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Immutability&lt;/strong&gt; 🧊: The state of not changing, or being unable to be changed. Immutability makes our code &lt;em&gt;predictable&lt;/em&gt; and free of &lt;a href="https://en.wikipedia.org/wiki/Side_effect_(computer_science)"&gt;&lt;em&gt;side effects&lt;/em&gt;&lt;/a&gt; 🐛&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Declarative&lt;/strong&gt; 📖: Declarative programming is code that is focused on describing &lt;em&gt;what&lt;/em&gt; the code does, &lt;em&gt;outcome&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pure functions&lt;/strong&gt; 🦄: Functions that given the &lt;em&gt;same input&lt;/em&gt; always return the &lt;em&gt;same output&lt;/em&gt; and have no side effects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function composition&lt;/strong&gt; 🍱: The process of combining two or more functions to produce new functions or perform some operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why Functional programming?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Confidence
&lt;/h4&gt;

&lt;p&gt;One of the best things about the &lt;em&gt;functional&lt;/em&gt; paradigm is that it helps you to create &lt;strong&gt;predictable&lt;/strong&gt; code.&lt;/p&gt;

&lt;p&gt;How? With &lt;strong&gt;immutable&lt;/strong&gt; values and &lt;strong&gt;pure functions&lt;/strong&gt;. Those principles create software free of side effects, so it's easy to understand and predict what a piece of code &lt;strong&gt;will do&lt;/strong&gt; just &lt;strong&gt;by reading it&lt;/strong&gt; 🍰, without executing the code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Code that you don’t understand is code you can’t trust”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This mindset helps you to boost your confidence 🔝 because you won't have to worry about anything else than the piece of code you're focusing on 🎯&lt;/p&gt;

&lt;p&gt;Predictable code is easier to maintain and debug. In the long term, this reduces the number of bugs you may ship into production 🐛&lt;/p&gt;

&lt;h4&gt;
  
  
  Communication
&lt;/h4&gt;

&lt;p&gt;I believe that &lt;strong&gt;coding is about communication&lt;/strong&gt;, the role of code itself is communicating with other humans before instructing the computer. This sounds a little bit &lt;em&gt;philosophical&lt;/em&gt; right 🔮?&lt;/p&gt;

&lt;p&gt;But think about your own experience as a developer; you probably spent much &lt;strong&gt;more time reading&lt;/strong&gt; and understanding code than actually &lt;strong&gt;programming&lt;/strong&gt; it 🧑‍💻.&lt;/p&gt;

&lt;p&gt;Writing &lt;strong&gt;declarative&lt;/strong&gt; code, which is another principle of the &lt;em&gt;functional&lt;/em&gt; paradigm will help you to focus on describing &lt;strong&gt;what&lt;/strong&gt; the code does, instead of &lt;em&gt;how&lt;/em&gt; it works, so code will be much more understandable for humans.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functional JavaScript
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Immutable
&lt;/h4&gt;

&lt;p&gt;Immutability is what keeps us safe 😊 from side effects and unexpected bugs 🐛. I &lt;a href="https://carloscuesta.me/blog/immutability-in-javascript"&gt;wrote a post reasoning about Immutability in JavaScript&lt;/a&gt; 📝. Take a look at it if you want to understand the whole concept.&lt;/p&gt;

&lt;p&gt;Imagine that we have a &lt;code&gt;shoppingCart&lt;/code&gt; that has two properties, &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;total&lt;/code&gt;. Let's say we want to update the &lt;code&gt;total&lt;/code&gt; property of our shopping cart, how can we achieve this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shoppingCart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;69zd841&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;total&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Immutable ✅
&lt;/h5&gt;

&lt;p&gt;Clone the &lt;code&gt;shoppingCart&lt;/code&gt; object and update the &lt;code&gt;total&lt;/code&gt; property value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;shoppingCart&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="mi"&gt;15&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Mutable ❌
&lt;/h5&gt;

&lt;p&gt;Perform a modification through the object property accessor to our original object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;cart&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="mi"&gt;15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Declarative
&lt;/h4&gt;

&lt;p&gt;Declarative programming consists on describing &lt;strong&gt;what&lt;/strong&gt; the code does and making the &lt;strong&gt;outcome predictable&lt;/strong&gt; without explicitly describing &lt;strong&gt;how&lt;/strong&gt; the control flow works.&lt;/p&gt;

&lt;p&gt;This definition may sound a bit abstract 🤨. But let's understand this with an example by comparing &lt;em&gt;declarative&lt;/em&gt; to &lt;em&gt;imperative&lt;/em&gt; code.&lt;/p&gt;

&lt;p&gt;We're going to build, a code that given an array of numbers returns the even ones 🔢:&lt;/p&gt;

&lt;h5&gt;
  
  
  Declarative ✅
&lt;/h5&gt;

&lt;p&gt;Focused on describing &lt;em&gt;what&lt;/em&gt; the code does and the outcome of it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To solve the example using a declarative approach, we create a &lt;code&gt;Function&lt;/code&gt; that receives an array of numbers.&lt;/p&gt;

&lt;p&gt;As we want to get all the even numbers, we are going to make use of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter"&gt;&lt;code&gt;Array.filter&lt;/code&gt;&lt;/a&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getEvenNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;getEvenNumbers&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; [2, 8, 10]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Imperative ❌
&lt;/h5&gt;

&lt;p&gt;Focused on &lt;em&gt;how&lt;/em&gt; it works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To solve the example using an imperative approach, we create a function that receives an array of numbers. Inside the function, we initialize an empty array variable called &lt;code&gt;evenNumbers&lt;/code&gt;, to store the numbers that met the condition.&lt;/p&gt;

&lt;p&gt;After that, we iterate our array using a &lt;code&gt;for&lt;/code&gt; loop with an &lt;code&gt;if&lt;/code&gt; statement to push the even numbers to the empty array in order to return them at the end of the iteration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getEvenNumbers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;evenNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;evenNumbers&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;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;evenNumbers&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;getEvenNumbers&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; [2, 8, 10]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Declarative 🆚 Imperative
&lt;/h5&gt;

&lt;p&gt;As you can see in &lt;strong&gt;both examples&lt;/strong&gt; on &lt;strong&gt;declarative approach&lt;/strong&gt; there are no explicit conditionals, loops, side effects, reassignments, or mutations. Instead, it employs &lt;strong&gt;well-known trustable patterns&lt;/strong&gt; like filtering and composition.&lt;/p&gt;

&lt;p&gt;With the imperative approach, we are forced to store through assignment each intermediate result of the iteration. Certainly, this increases the complexity of the code, as a result of this, understanding and finding bugs becomes a harder task.&lt;/p&gt;

&lt;p&gt;The focus shifts from &lt;strong&gt;low-level&lt;/strong&gt; &lt;em&gt;how&lt;/em&gt; to &lt;strong&gt;higher level&lt;/strong&gt; &lt;em&gt;what&lt;/em&gt; outcomes.&lt;/p&gt;

&lt;p&gt;On both examples, you can certainly trace and predict the outcome of the code just by reading it. However the &lt;strong&gt;declarative&lt;/strong&gt; approach is much more &lt;strong&gt;clear&lt;/strong&gt; and &lt;strong&gt;straightforward&lt;/strong&gt; ✨. Simply because you're abstracting all the iteration and filter logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Familiarity&lt;/strong&gt; has a big &lt;strong&gt;influence&lt;/strong&gt; on your judgments of &lt;em&gt;clearness&lt;/em&gt; and &lt;em&gt;readability&lt;/em&gt;. Meaning that if you're more used to work with an &lt;em&gt;imperative&lt;/em&gt; approach you'll probably find those snippets easier to understand.&lt;/p&gt;

&lt;p&gt;But as soon as you get on the same level of familiarity with the &lt;em&gt;declarative&lt;/em&gt; approach your mindset will click and you'll start to see the benefits 🥳&lt;/p&gt;

&lt;h4&gt;
  
  
  Functions over procedures
&lt;/h4&gt;

&lt;p&gt;If you plan to do &lt;strong&gt;FP&lt;/strong&gt; you &lt;strong&gt;should be using functions as much as possible&lt;/strong&gt;, trying to avoid procedures wherever possible. All your functions should &lt;strong&gt;take input(s)&lt;/strong&gt; and &lt;strong&gt;return output(s)&lt;/strong&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  Function ✅
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; 👋`&lt;/span&gt;

&lt;span class="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Carlos&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; Hello Carlos 👋&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Procedure ❌
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Carlos&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; 👋`&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; Hello Carlos 👋&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pure functions
&lt;/h4&gt;

&lt;p&gt;A &lt;strong&gt;pure function&lt;/strong&gt; is a &lt;code&gt;function&lt;/code&gt; which:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Given the &lt;strong&gt;same input&lt;/strong&gt;, will always return the &lt;strong&gt;same output&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Has &lt;strong&gt;no side effects&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Pure functions ✅
&lt;/h5&gt;

&lt;p&gt;This function given the same &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; inputs will always return the same output. Has no side effects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;

&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Impure functions ❌
&lt;/h5&gt;

&lt;p&gt;This &lt;code&gt;incrementCount&lt;/code&gt; function has a side effect, because it's depending on state that it's outside of his own scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;incrementCount&lt;/span&gt; &lt;span class="o"&gt;=&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;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another example of built in JavaScript impure functions would be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random"&gt;&lt;code&gt;Math.random&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Console/log"&gt;&lt;code&gt;console.log&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Who knows what will return! 😜&lt;/span&gt;
&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; ?&lt;/span&gt;

&lt;span class="c1"&gt;// Always returns the same output, but writing to the console is a side effect&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello! 👋&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Composing functions
&lt;/h4&gt;

&lt;p&gt;Another important concept on the &lt;em&gt;functional paradigm&lt;/em&gt; is &lt;strong&gt;composing&lt;/strong&gt; functions and using &lt;strong&gt;higher-order functions&lt;/strong&gt;. In JavaScript, &lt;code&gt;functions&lt;/code&gt; are values, this means that we can assign them into variables and pass them to other functions.&lt;/p&gt;

&lt;p&gt;Think about the even numbers example we used before 🔢;&lt;/p&gt;

&lt;p&gt;As you can see, we can assign a function to the &lt;code&gt;isEven&lt;/code&gt; variable, because functions are values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isEven&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="nx"&gt;isEven&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; true&lt;/span&gt;
&lt;span class="nx"&gt;isEven&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows us to compose and build our code by combining smaller functions and pass the &lt;code&gt;isEven&lt;/code&gt; function to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter"&gt;&lt;code&gt;Array.filter&lt;/code&gt;&lt;/a&gt; higher-order function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getEvenNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isEven&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;getEvenNumbers&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; [10, 20]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See how we broke up the problem of determining if a number is even with the &lt;code&gt;isEven&lt;/code&gt; function and then how we found the even numbers from the array composing the &lt;code&gt;filter&lt;/code&gt; function with the utility one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Composing&lt;/strong&gt; functions is a great technique to &lt;strong&gt;broke up&lt;/strong&gt; a &lt;strong&gt;problem&lt;/strong&gt; into smaller isolated pieces. In a way that we can &lt;strong&gt;reason&lt;/strong&gt; about those problems &lt;strong&gt;separately&lt;/strong&gt;, which makes our functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reusable&lt;/li&gt;
&lt;li&gt;Easier to maintain&lt;/li&gt;
&lt;li&gt;Easier to debug&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  List operations
&lt;/h4&gt;

&lt;p&gt;Working with lists, also known as Arrays, is something that we do daily, let's examine the most common operations and how we can do them with a &lt;em&gt;functional&lt;/em&gt; approach.&lt;/p&gt;

&lt;h5&gt;
  
  
  Map
&lt;/h5&gt;

&lt;p&gt;A &lt;strong&gt;mapping&lt;/strong&gt; 🗺 is a &lt;strong&gt;transformation&lt;/strong&gt; from &lt;strong&gt;one value&lt;/strong&gt; to &lt;strong&gt;another&lt;/strong&gt;. We can apply a transformation to a list using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;&lt;code&gt;Array.map&lt;/code&gt;&lt;/a&gt; function.&lt;/p&gt;

&lt;p&gt;For example, imagine that as a result of calling to an API we've got the following JSON:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"An emoji guide for your commit messages."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"language"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"javascript"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="s2"&gt;"gitmoji"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"stargazers_count"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;9500&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's say that want to render the data on our application, but we don't want to couple to the data schema the API uses, as it could potentially change and we don't need all the data.&lt;/p&gt;

&lt;p&gt;How we can solve that? Easy by making a mapping with the &lt;code&gt;Array.map&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;repository&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="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;stars&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stargazers_count&lt;/span&gt;
&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This mapping would transform the JSON to something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;An emoji guide for your commit messages&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gitmoji&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;stars&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9500&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;h5&gt;
  
  
  Filter
&lt;/h5&gt;

&lt;p&gt;The filtering 🔎 process consists on &lt;strong&gt;including&lt;/strong&gt; or &lt;strong&gt;excluding&lt;/strong&gt; certain &lt;strong&gt;elements&lt;/strong&gt; from a list based on a certain &lt;strong&gt;condition&lt;/strong&gt; using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;&lt;code&gt;Array.filter&lt;/code&gt;&lt;/a&gt; function.&lt;/p&gt;

&lt;p&gt;The process could be &lt;em&gt;inclusive&lt;/em&gt; or &lt;em&gt;exclusive&lt;/em&gt;. This sometimes causes confusion, because you have to twist your brain when you're applying an excluding filter.&lt;/p&gt;

&lt;p&gt;Let's illustrate this operation with our &lt;code&gt;shoppingBasket&lt;/code&gt; object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shoppingBasket&lt;/span&gt; &lt;span class="o"&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍏&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fruit&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍌&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fruit&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥕&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vegetable&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍔&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;meat&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥔&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vegetable&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want to perform two operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inclusive: Get every product that is a &lt;code&gt;fruit&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Exclusive: Get every product that is not a &lt;code&gt;fruit&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's understand the difference with a quick example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inclusive filter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's filter our &lt;code&gt;shoppingBasket&lt;/code&gt; to get the fruits&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;shoppingBasket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fruit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This returns a new list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍏&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fruit&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍌&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fruit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Excluding filter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we're going to do the opposite, get everything that is not a fruit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;shoppingBasket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fruit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This returns a new list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥕&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vegetable&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍔&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;meat&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥔&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vegetable&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Reduce
&lt;/h5&gt;

&lt;p&gt;A reduction ♻️ is a &lt;strong&gt;transformation&lt;/strong&gt; from &lt;strong&gt;two or more values&lt;/strong&gt; into a &lt;strong&gt;single one&lt;/strong&gt;. Hence the name of the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce"&gt;&lt;code&gt;Array.reduce&lt;/code&gt;&lt;/a&gt; function 🌝&lt;/p&gt;

&lt;p&gt;Suppose that we want to transform our &lt;code&gt;shoppingBasket&lt;/code&gt; list to an object, that groups the products by the &lt;code&gt;type&lt;/code&gt; property, something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"fruit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"vegetable"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"meat"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy! Let's apply a reduction to the original list that contains N items to transform it to an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shoppingBasket&lt;/span&gt; &lt;span class="o"&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍏&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fruit&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍌&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fruit&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥕&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vegetable&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍔&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;meat&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥔&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vegetable&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="nx"&gt;shoppingBasket&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;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[]].&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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;The outcome of this reduction is an Object that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;fruit&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍏&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fruit&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍌&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fruit&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="nx"&gt;vegetable&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥕&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vegetable&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥔&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vegetable&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="nx"&gt;meat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍔&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;meat&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Recursion
&lt;/h4&gt;

&lt;p&gt;Recursion is a technique that happens when a &lt;strong&gt;function calls itself&lt;/strong&gt; until it satisfies a base condition. Is a way to avoid imperative looping and reassignments. When used properly, it helps you to solve complex problems in a declarative way.&lt;/p&gt;

&lt;p&gt;☢️ Recursion should be used carefully because it relies a lot on memory, so the performance of your application could be affected.&lt;/p&gt;

&lt;p&gt;Let's see a silly example to understand what &lt;em&gt;recursion&lt;/em&gt; looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;countDownFrom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&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;number&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;countDownFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&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;Note that since the function calls itself, you'll need to specify a &lt;strong&gt;stop condition&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Learning resources
&lt;/h3&gt;

&lt;p&gt;Do you want to learn more about functional programming in JavaScript? Here's an awesome list 🤩:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📘 &lt;a href="https://github.com/getify/Functional-Light-JS"&gt;Functional-Light JavaScript by Kyle Simpson&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎬 &lt;a href="https://www.youtube.com/watch?v=BMUiFMZr7vk&amp;amp;list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84"&gt;Functional Programming in JavaScript by Fun Fun Function&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  FP libraries
&lt;/h3&gt;

&lt;p&gt;Here's a list of &lt;em&gt;functional programming&lt;/em&gt; libraries you can check:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://ramdajs.com"&gt;Ramda&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://lodash.com"&gt;Lodash&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
    </item>
    <item>
      <title>The frontend tooling guide to improve Developer Experience</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Mon, 22 Feb 2021 16:03:20 +0000</pubDate>
      <link>https://dev.to/carloscuesta/the-frontend-tooling-guide-to-improve-developer-experience-3o5g</link>
      <guid>https://dev.to/carloscuesta/the-frontend-tooling-guide-to-improve-developer-experience-3o5g</guid>
      <description>&lt;p&gt;Let's talk about how we can use frontend tooling to improve the &lt;strong&gt;developer experience&lt;/strong&gt; in our team and how these tools impact the reliability of shipping value into production &lt;strong&gt;safer&lt;/strong&gt; and &lt;strong&gt;faster&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Have you ever been on a situation where your team had to &lt;strong&gt;quickly fix a bug&lt;/strong&gt; on production or &lt;strong&gt;struggled&lt;/strong&gt; to &lt;strong&gt;deploy&lt;/strong&gt; a new &lt;strong&gt;feature reliably&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;Keep reading then! All those problems usually indicate that you don't have the proper tools. In this article I'll give you the tools your team needs to create a high-velocity development environment for frontend applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Developer Experience ?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;D&lt;/strong&gt;eveloper &lt;strong&gt;E&lt;/strong&gt;xperience &lt;em&gt;in this context&lt;/em&gt;, is everything that happens in between programming and the outcome of the code.&lt;/p&gt;

&lt;p&gt;Starting from the first line of code that is written on the editor and ending when the code is shipped and available to our users. Is the literal feeling a developer has when coding.&lt;/p&gt;

&lt;p&gt;Think about all those little things that help you to code better without having to put any attention on them.  &lt;/p&gt;

&lt;p&gt;A few examples and common use cases of &lt;em&gt;DX&lt;/em&gt; would be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An engineer writes code that it's not typed and a tool warns about it.&lt;/li&gt;
&lt;li&gt;An application that is &lt;em&gt;&lt;a href="https://reactnative.dev/blog/2016/03/24/introducing-hot-reloading"&gt;hot reloaded&lt;/a&gt;&lt;/em&gt; every time the code changes.&lt;/li&gt;
&lt;li&gt;An automation that deploys your changes into production.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The impact of having a great DX
&lt;/h3&gt;

&lt;p&gt;Having a great DX has a significant influence in your development speed. How? It's simple, as soon as you rely on automated proceses and tools, the people on your team will start to feel more confident.&lt;/p&gt;

&lt;p&gt;Trust, empowers developers to be more productive and agile 🧠, forgetting about the fear of change. So you will be able to bring value much more faster to your users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Automation&lt;/strong&gt; is the &lt;strong&gt;key&lt;/strong&gt; to &lt;strong&gt;build&lt;/strong&gt; a &lt;strong&gt;high-velocity&lt;/strong&gt; development &lt;strong&gt;team&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of it as giving &lt;strong&gt;superpowers&lt;/strong&gt; 🦸‍♂️ to people. With the proper tools your team will be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deliver value to production fast ⚡️&lt;/li&gt;
&lt;li&gt;Anticipate and fix bugs 🐛&lt;/li&gt;
&lt;li&gt;Optimise the development time of engineers ⌚️&lt;/li&gt;
&lt;li&gt;Build consistent and high quality codebases 🥇&lt;/li&gt;
&lt;li&gt;Leave fear of change behind 😍&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sounds awesome right? Let's dive into it! 🛠&lt;/p&gt;

&lt;h3&gt;
  
  
  A tool for every problem
&lt;/h3&gt;

&lt;p&gt;Before starting to analyze what we are going to use, is important to understand that the election of a tool depends totally on the problem that needs to be addressed.&lt;/p&gt;

&lt;p&gt;As &lt;strong&gt;Abraham Maslow&lt;/strong&gt; said, on the &lt;em&gt;&lt;a href="https://en.wikipedia.org/wiki/Law_of_the_instrument"&gt;law of the instrument&lt;/a&gt;&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"I suppose it is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The following quote 👆 is &lt;strong&gt;highly relevant&lt;/strong&gt; on the &lt;strong&gt;programming context&lt;/strong&gt;. Many times as software engineers we tend to use the same known tools for any new project, no matter what the constraints and requirements are.&lt;/p&gt;

&lt;p&gt;This is a comfort zone bias. We decide to not change or even consider a new tool to avoid risk.&lt;/p&gt;

&lt;p&gt;The problem is that we're limiting our knowledge and the ability to discover more efficient instruments, because we can't compare them against anything.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tools
&lt;/h3&gt;

&lt;p&gt;Let's start exploring the tools that will help us to build an awesome DX on our team. We will start from the ones that require less effort and provide more impact.&lt;/p&gt;

&lt;p&gt;I've made up a little graphic to represent the impact every tool is going to have on the DX of your team against the effort it requires to be used.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZJfBTtqP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1613417482/dx-chart_dtwtsc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZJfBTtqP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1613417482/dx-chart_dtwtsc.png" alt="Front End Tools DX Impact-Effort Chart" width="880" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Linters and formatters
&lt;/h4&gt;

&lt;p&gt;Linters and formatters are &lt;strong&gt;essential&lt;/strong&gt; to &lt;strong&gt;maintain&lt;/strong&gt; a &lt;strong&gt;consistent&lt;/strong&gt; codebase. When it comes to writing code every team member should follow the same style, rules and conventions. &lt;strong&gt;Consistency&lt;/strong&gt; is key in a project.&lt;/p&gt;

&lt;p&gt;You don't want to have different code styles depending on who's writing the code. It's better to agree on a style to avoid confusion. Think about onboarding a new team member, it's will be much easier if the coding convention is well defined with clear rules.&lt;/p&gt;

&lt;p&gt;What's the &lt;strong&gt;difference&lt;/strong&gt; then between a &lt;strong&gt;linter&lt;/strong&gt; and a &lt;strong&gt;formatter&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;Most of the time, you can use both of them for the same purpose, which is to find code errors, bugs and automatically format the code based on the given style guide. However there's a subtle difference:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Linters&lt;/strong&gt;: Find and catch unexpected coding errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Formatters&lt;/strong&gt;: Format and structure the code with the given style.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Recommended tools&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the list of linters and formatters I use that I would recommend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://eslint.org"&gt;ESLint&lt;/a&gt;: Pluggable JavaScript linter.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://prettier.io"&gt;Prettier&lt;/a&gt;: An opinionated code formatter. Works with &lt;code&gt;.css&lt;/code&gt; files too! 🎉&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://standardjs.com"&gt;StandardJS&lt;/a&gt;: JavaScript style guide, linter, and formatter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 &lt;strong&gt;Protip&lt;/strong&gt;: Run linters and formatters on a &lt;a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks"&gt;&lt;code&gt;pre-commit&lt;/code&gt; git hook&lt;/a&gt; using &lt;a href="https://github.com/typicode/husky"&gt;Husky&lt;/a&gt; + &lt;a href="https://github.com/okonet/lint-staged"&gt;lint-staged&lt;/a&gt; to make sure that the code is readable and formated &lt;strong&gt;before&lt;/strong&gt; the code review process 😍&lt;/p&gt;

&lt;h4&gt;
  
  
  Static typing
&lt;/h4&gt;

&lt;p&gt;JavaScript is a &lt;a href="https://en.wikipedia.org/wiki/JavaScript"&gt;&lt;em&gt;weakly typed&lt;/em&gt;&lt;/a&gt; programming language. Due to it's nature this can cause unexpected bugs and errors. A &lt;strong&gt;static type checker&lt;/strong&gt; will help your team to &lt;strong&gt;write code faster&lt;/strong&gt; and more &lt;strong&gt;confidently&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Since we started to use static type checkers on the team, we began to code and refactor a lot faster while feeling much more safer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended tools&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the list of static type checkers I use that I would recommend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://flow.org/"&gt;Flow&lt;/a&gt;: A static type checker for JavaScript.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 &lt;strong&gt;Protip&lt;/strong&gt;: You can also run &lt;code&gt;flow&lt;/code&gt; type checks on a &lt;code&gt;pre-commit&lt;/code&gt; hook basis, as suggested on linters and formatters.&lt;/p&gt;

&lt;h4&gt;
  
  
  Performance and accessibility audits
&lt;/h4&gt;

&lt;p&gt;Performance and accessibility are two important values we should be &lt;strong&gt;accountable&lt;/strong&gt; for as Software Engineers. Although this does not have a big impact on &lt;em&gt;DX&lt;/em&gt;, the outcome is part of our programming process, so we should measure and improve our &lt;a href="https://www.a11yproject.com/"&gt;a11y&lt;/a&gt; and performance constantly.&lt;/p&gt;

&lt;p&gt;These are the most &lt;strong&gt;relevant values&lt;/strong&gt; for your users. Keep in mind, that whenever a user enters your site or application, the first thing they will feel is the overall performance of the site, how fast it loads, how fast is to navigate through, the way it works and integrates with screen readers etc.&lt;/p&gt;

&lt;p&gt;So for me, makes a lot of sense using a tool to audit and monitor performance and a11y to ensure they met the standard requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended tools&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the list of tools I use for auditing performance and accessibility I would recommend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/GoogleChrome/lighthouse-ci"&gt;Lighthouse-CI&lt;/a&gt;: Automate running Lighthouse for every commit, viewing the changes, and preventing regressions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 &lt;strong&gt;Protip&lt;/strong&gt;: It's a great idea to include this audit as a step of your Pull-Request checks. We will introduce this concept on the next section.&lt;/p&gt;

&lt;h4&gt;
  
  
  Unit testing
&lt;/h4&gt;

&lt;p&gt;The aim for unit tests is to ensure that individual and isolated pieces of your codebase work properly. Also they act as a layer of protection against the passage of time. As long as the test exists the behaviour of those units will be guaranteed.&lt;/p&gt;

&lt;p&gt;So then it's impossible break those individual pieces by accident on a refactor or when introducing some changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unit tests&lt;/strong&gt; should be &lt;strong&gt;fast to run&lt;/strong&gt;. Some examples and use cases where I use unit tests:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Components&lt;/strong&gt;: Ensure that isolated components are working as expected on their own.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Utility functions&lt;/strong&gt;: We usually build a lot of helpers and wrappers to create abstractions in our projects. To ensure they work as expected with all the uses cases, you should be unit testing them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sagas&lt;/strong&gt;: Audit the steps and behaviour of your asynchronous flows in your application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Recommended tools&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the list of tools I use for unit testing I would recommend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://jestjs.io"&gt;Jest&lt;/a&gt;: A delightful JavaScript Testing Framework with a focus on simplicity.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/jest-community/jest-extended"&gt;Jest-Extended&lt;/a&gt;: Additional Jest matchers&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Integration testing
&lt;/h4&gt;

&lt;p&gt;Integration testing is about checking the behaviour of unit pieces grouped together. In my opinion, this is where &lt;strong&gt;most of your focus should be when writing tests&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Why? Considering the &lt;strong&gt;time and effort&lt;/strong&gt; it takes to &lt;strong&gt;write tests&lt;/strong&gt; compared to the &lt;strong&gt;level of confidence&lt;/strong&gt; they give back to you, &lt;strong&gt;integration&lt;/strong&gt; tests are merely the best.&lt;/p&gt;

&lt;p&gt;Unit tests are great but they give you low confidence results, think about the &lt;a href="https://twitter.com/erinfranmc/status/1148986961207730176"&gt;umbrella example&lt;/a&gt; 🌂&lt;/p&gt;

&lt;p&gt;Some examples and use cases where I use integration tests:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;User interactions&lt;/strong&gt;: Simulate user interactions through events as if you were a user. For example, on an e-commerce site add a product to the cart and assert that an API request has been made and the product items count has been increased.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Forms&lt;/strong&gt;: Fill forms in different ways, simulate a form submit and validate the data, make the form fail and assert the errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Navigation&lt;/strong&gt;: When the user clicks on something, are we navigating to the expected screen?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Recommended tools&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the list of tools I use for integration testing I would recommend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://jestjs.io"&gt;Jest&lt;/a&gt;: A delightful JavaScript Testing Framework with a focus on simplicity.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://testing-library.com"&gt;Testing-Library&lt;/a&gt;: Simple and complete testing utilities that encourage good testing practices.&lt;/li&gt;
&lt;li&gt;&lt;a href="//reactjs.org/docs/test-renderer.html"&gt;React-Test-Renderer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  End to end testing
&lt;/h4&gt;

&lt;p&gt;When it comes the time to guarantee the behaviour of &lt;strong&gt;key user flows&lt;/strong&gt; on a product, for example on an E-commerce would be; an add to cart or a checkout process. E2E testing is crucial. Because you're testing on a &lt;strong&gt;real browser&lt;/strong&gt; and a &lt;strong&gt;real device&lt;/strong&gt; against &lt;strong&gt;external APIs&lt;/strong&gt;, as if you were an &lt;strong&gt;genuine user&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Finally! With E2E your team would be able to sleep properly 😴, with the safety that nothing is going to brake suddenly due to a weird bug or something that hasn't been tested properly 🎉.&lt;/p&gt;

&lt;p&gt;Be aware that since those type of tests run on real devices, you should only test &lt;strong&gt;the most important things&lt;/strong&gt; for what you're building, specifically user flows and paths. Building an E2E could be a little bit time consuming at first, but the effort pays off. Luckily at the moment there are a lot of tools that makes this process a lot easier!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended tools&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the list of tools I use for E2E I would recommend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.cypress.io"&gt;Cypress&lt;/a&gt;: Fast, easy and reliable testing for anything that runs in a &lt;strong&gt;browser&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/wix/Detox"&gt;Detox&lt;/a&gt;: Gray box end-to-end testing and automation framework for &lt;strong&gt;mobile apps&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Integrating tools on the development workflow
&lt;/h3&gt;

&lt;p&gt;Now that we understand what is each tool for, I'm sure you asked yourself; &lt;em&gt;How I'm going to integrate those tools on my team development workflow?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Automate all the things&lt;/strong&gt; with a &lt;em&gt;&lt;a href="https://en.wikipedia.org/wiki/Continuous_integration"&gt;continuous integration&lt;/a&gt;&lt;/em&gt; and a &lt;em&gt;&lt;a href="https://en.wikipedia.org/wiki/Continuous_delivery"&gt;continuous delivery&lt;/a&gt;&lt;/em&gt; &lt;strong&gt;pipeline&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As we've seen previously, we can simply run some tools automatically using git hooks and that's totally fine, but this approach has a few problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Certain tools can't run on a git hook, because they could be really slow.&lt;/li&gt;
&lt;li&gt;Something that runs on a local machine could be skipped.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Pipelines
&lt;/h4&gt;

&lt;p&gt;A &lt;strong&gt;pipeline&lt;/strong&gt; is a way to unify all your tools into a &lt;strong&gt;process&lt;/strong&gt; that does not relies on any local machine and not depends on human interaction.&lt;/p&gt;

&lt;p&gt;This process runs on every Pull-Request and your main branch, in order to ensure any change that wants to be deployed into production, meets the quality standards and works as expected.&lt;/p&gt;

&lt;p&gt;Based on the tools we've seen here's a representation of how our pipeline would be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IGJEi3kc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1613848223/frontend-pipeline_qkjwbb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IGJEi3kc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1613848223/frontend-pipeline_qkjwbb.png" alt="Front End Tools CI Pipeline" width="880" height="819"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that tools that run and fail faster are the first ones that run on our pipeline.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Proptip&lt;/strong&gt;: When building a web project, I use something called &lt;strong&gt;deployment previews&lt;/strong&gt;. This is probably one of my favorite features of the whole pipeline. You can get a &lt;strong&gt;standalone deployment&lt;/strong&gt; with a &lt;strong&gt;unique URL&lt;/strong&gt; for each PR.&lt;/p&gt;

&lt;p&gt;This allows you to test your changes on a safe and isolated environment. Previews speed up code reviews and help you to validate implementations and features. There are a couple of platforms that provide this feature such as &lt;a href="https://www.netlify.com/blog/2016/07/20/introducing-deploy-previews-in-netlify/"&gt;Netlify&lt;/a&gt; and &lt;a href="https://vercel.com/docs/platform/deployments#preview"&gt;Vercel&lt;/a&gt;. If you're not using them you can try to build something on your own!&lt;/p&gt;

&lt;p&gt;Here's a list of platforms you can use to build your pipelines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/features/actions"&gt;GitHub Actions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://travis-ci.com/"&gt;Travis-CI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.jenkins.io/"&gt;Jenkins&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://circleci.com/"&gt;Circle-CI&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Demo time
&lt;/h4&gt;

&lt;p&gt;Right now I'm using &lt;strong&gt;GitHub Actions&lt;/strong&gt; to build my pipelines 😍. If you want to see an implementation like the one we described, you can take a look at &lt;a href="https://github.com/carloscuesta/carloscuesta.me/actions/workflows/ci.yml"&gt;my website pipeline&lt;/a&gt;. Find the implementation on &lt;a href="https://github.com/carloscuesta/carloscuesta.me/blob/master/.github/workflows/ci.yml"&gt;the following link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v8OnrPZg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1613849250/pipeline-demo_krkshf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v8OnrPZg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1613849250/pipeline-demo_krkshf.png" alt="Front End Tools CI Pipeline" width="880" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see that whenever I open a Pull-Request, there's a list of checks that need to be ✅ before being able to merge the changes to master:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IxKaW7YX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1613988260/pr-checks_jpckkt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IxKaW7YX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/v1613988260/pr-checks_jpckkt.png" alt="PR Checks" width="880" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The checks ensure that the quality standards are passed and everything works perfect 🚀.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping up
&lt;/h3&gt;

&lt;p&gt;This article is a brain dump of the concepts and knowledge I use daily on personal and professional projects, to ship software.&lt;/p&gt;

&lt;p&gt;I know it's a lot of information to understand but I can ensure you that if you implement a similar workflow and you put attention into &lt;em&gt;Developer Experience&lt;/em&gt; &lt;strong&gt;it will make a huge difference&lt;/strong&gt; on your team.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>tooling</category>
      <category>dx</category>
      <category>devrel</category>
    </item>
    <item>
      <title>Immutability in JavaScript</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Tue, 26 Jan 2021 16:28:52 +0000</pubDate>
      <link>https://dev.to/carloscuesta/immutability-in-javascript-1bo5</link>
      <guid>https://dev.to/carloscuesta/immutability-in-javascript-1bo5</guid>
      <description>&lt;p&gt;Have you ever heard something about &lt;em&gt;immutability&lt;/em&gt;? I'm mostly sure the answer is &lt;em&gt;yes!&lt;/em&gt;, particularly on the programming ecosystem. Despite being a popular term, there are some misconceptions about the importance and principles of it. Let's dive in into it!&lt;/p&gt;

&lt;h3&gt;
  
  
  What does it mean?
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Immutability&lt;/em&gt; is the &lt;strong&gt;state of not changing&lt;/strong&gt;, or &lt;strong&gt;being unable to be changed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;On a programming context, means that when we need to &lt;strong&gt;change the state in our program&lt;/strong&gt;, we &lt;strong&gt;must create and track a new value&lt;/strong&gt; rather than mutating the existing one.&lt;/p&gt;

&lt;p&gt;🚨 This does not mean we can't have values that change over the lifecycle of our program. That's a common misconception about immutability 🖐&lt;/p&gt;

&lt;h4&gt;
  
  
  Immutable 🆚 Mutable
&lt;/h4&gt;

&lt;p&gt;To understand the difference between both, take a look at the following example. Imagine that we have a shopping cart 🛒 &lt;code&gt;object&lt;/code&gt;, that contains two properties, &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;total&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shoppingCart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;69zd841&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;total&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's say that we want to update the &lt;code&gt;total&lt;/code&gt; property of our &lt;code&gt;shoppingCart&lt;/code&gt;, how can we achieve this?&lt;/p&gt;

&lt;h5&gt;
  
  
  Clone and update
&lt;/h5&gt;

&lt;p&gt;Using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax"&gt;spread operator&lt;/a&gt; we can create a new object cloning the previous cart and updating the &lt;code&gt;total&lt;/code&gt; property, while preserving our original object in a pristine condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;shoppingCart&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="mi"&gt;15&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Mutate
&lt;/h5&gt;

&lt;p&gt;Through the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors"&gt;object property accessor&lt;/a&gt; we can perform a modification to our original object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;cart&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="mi"&gt;15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;The &lt;strong&gt;difference&lt;/strong&gt; between those two examples is that on the first one we preserved our original shopping cart and we created a new one and on the second one we &lt;strong&gt;overwrited&lt;/strong&gt; the value of the &lt;code&gt;total&lt;/code&gt; property.&lt;/p&gt;

&lt;h3&gt;
  
  
  The benefits
&lt;/h3&gt;

&lt;p&gt;Now that we understood the concept of immutability and the difference between mutable 🆚 immutable data, let's see the benefits of applying it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Immutability is like a seatbelt, it won't prevent an accident but it saves your life when it does&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Predictability
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Predictable&lt;/strong&gt; code is code where you can &lt;strong&gt;anticipate the outcome&lt;/strong&gt; with a single read. Mutation hides change, as we've seen before, the source is lost. Hidden change creates unexpected side effects that can cause complex bugs 🐛.&lt;/p&gt;

&lt;p&gt;When we can predict the outcome, we start to feel more confident with our code simplifying our thought process. This helps us reason easier about our program.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tracking mutations
&lt;/h4&gt;

&lt;p&gt;When you mutate data, you're overwriting the same reference every time that you update something as we've pointed on the first example. Then it's impossible to have a track of the changes you've done to the program state.&lt;/p&gt;

&lt;p&gt;Immutable data gives you a clear history of state to the program, because you're creating a new reference based on the source.&lt;/p&gt;

&lt;h3&gt;
  
  
  The drawbacks
&lt;/h3&gt;

&lt;p&gt;Whenever we start creating new values such as &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Object&lt;/code&gt; etc, instead of mutating existing ones the next question always is is: &lt;em&gt;What kind of impact has this for performance?&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Performance
&lt;/h4&gt;

&lt;p&gt;Avoiding mutations has a &lt;strong&gt;cost&lt;/strong&gt; that's correct, every time we have to reallocate a new value, that's consuming &lt;strong&gt;CPU time&lt;/strong&gt; and &lt;strong&gt;memory&lt;/strong&gt;, including the &lt;strong&gt;garbage collection&lt;/strong&gt; process for values that are no longer referenced.&lt;/p&gt;

&lt;p&gt;Is that an &lt;strong&gt;acceptable trade-off&lt;/strong&gt;? &lt;strong&gt;It depends&lt;/strong&gt;. We need some &lt;strong&gt;context&lt;/strong&gt; to answer that question, for example:&lt;/p&gt;

&lt;p&gt;If you have a single state change, that happens a few times on the lifecycle of your program, creating a new object, is certainly not a problem. It won't have no practical impact on the performance of the app. Compared to the time you will save not having to track and fix bugs caused by side effects, the winner clearly is immutability 🎉&lt;/p&gt;

&lt;p&gt;Again, if that state change is going to occur frequently, or it happens in a critical path of your app, then performance is a totally valid concern 👍&lt;/p&gt;

&lt;p&gt;Thankfully, there are some libraries out there that will provide performance optimizations such as &lt;a href="https://immutable-js.github.io/immutable-js"&gt;Immutable.js&lt;/a&gt; and &lt;a href="https://github.com/rtfeldman/seamless-immutable"&gt;seamless-immutable&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Immutable data in JS
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Constants
&lt;/h4&gt;

&lt;p&gt;A constant is a &lt;strong&gt;variable&lt;/strong&gt; that &lt;strong&gt;cannot be reassigned&lt;/strong&gt; and &lt;strong&gt;redeclared&lt;/strong&gt;. This is an important concept to understand, because it does not mean the value it holds is immutable.&lt;/p&gt;

&lt;p&gt;The use of &lt;code&gt;const&lt;/code&gt; tells the human being who's reading your code that the following variable won't be reassigned or redeclared. It's a great improvement in &lt;strong&gt;code readability&lt;/strong&gt; because we're intentionally communicating that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍌&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍓&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥥&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// We can't reassign the constant&lt;/span&gt;
&lt;span class="c1"&gt;// ❌ TypeError: Assignment to constant variable.&lt;/span&gt;
&lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍏&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// We can't redeclare the constant&lt;/span&gt;
&lt;span class="c1"&gt;// ❌ SyntaxError: Identifier 'fruits' has already been declared&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥝&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// ✅ We can mutate the value as we want&lt;/span&gt;
&lt;span class="c1"&gt;// fruits -&amp;gt; ['🍌','🍓', '🥥', '🍍']&lt;/span&gt;
&lt;span class="nx"&gt;fruits&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍍&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Spread syntax
&lt;/h4&gt;

&lt;p&gt;Also known as &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax"&gt;&lt;code&gt;...&lt;/code&gt;&lt;/a&gt;, it's a useful operator for cloning &lt;em&gt;iterables&lt;/em&gt; such as &lt;code&gt;Array&lt;/code&gt; or &lt;code&gt;Object&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍌&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍓&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥥&lt;/span&gt;&lt;span class="dl"&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;shoppingCart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;69zd841&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;total&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="c1"&gt;// Add a some fruits to the end of the array&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruitsCollection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍍&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥝&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// Update the shoppingCart total and clone all the other properties&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shoppingCartWithTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;shoppingCart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Object.freeze
&lt;/h4&gt;

&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze"&gt;&lt;code&gt;Object.freeze&lt;/code&gt;&lt;/a&gt; function is a simple way to turn a mutable &lt;code&gt;Object&lt;/code&gt; or &lt;code&gt;Array&lt;/code&gt; into an "immutable value". This function freezes the object 🥶 you pass as argument.&lt;/p&gt;

&lt;p&gt;But what does &lt;em&gt;frozen object&lt;/em&gt; means? A &lt;strong&gt;frozen object&lt;/strong&gt; is an Object whose properties/indices has been marked as read-only and non-reconfigurable, so they can't be reassigned and the Object itself is marked as non-extensible, so no new properties can be added.&lt;/p&gt;

&lt;p&gt;The freezing process ❄️ is only made at the &lt;strong&gt;top level&lt;/strong&gt; of the object. If you want to make your whole object immutable, make sure you deep &lt;strong&gt;freeze&lt;/strong&gt; each sub &lt;code&gt;Object&lt;/code&gt; or &lt;code&gt;Array&lt;/code&gt; 🤓.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;freeze&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍌&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍓&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🥥&lt;/span&gt;&lt;span class="dl"&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;shoppingCart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;freeze&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;69zd841&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;total&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="na"&gt;products&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="c1"&gt;// We can't extend the fruits Array&lt;/span&gt;
&lt;span class="c1"&gt;// ❌ TypeError: Cannot add property N, object is not extensible&lt;/span&gt;
&lt;span class="nx"&gt;fruits&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🍏&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// ❌ We can't mutate shoppingCart top-level properties&lt;/span&gt;
&lt;span class="c1"&gt;// shoppingCart -&amp;gt; { id: '69zd841', total: 0, products: [] }&lt;/span&gt;
&lt;span class="nx"&gt;shoppingCart&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="mi"&gt;123&lt;/span&gt;

&lt;span class="c1"&gt;// 🚨 We can mutate shoppingCart objects&lt;/span&gt;
&lt;span class="c1"&gt;// shoppingCart -&amp;gt; { id: '69zd841', total: 0, products: [{ name: 'Beer' }] }&lt;/span&gt;
&lt;span class="nx"&gt;shoppingCart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;products&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Beer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>immutability</category>
    </item>
    <item>
      <title>Working remotely as a Software Engineer</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Fri, 21 Aug 2020 13:45:42 +0000</pubDate>
      <link>https://dev.to/carloscuesta/working-remotely-as-a-software-engineer-44dp</link>
      <guid>https://dev.to/carloscuesta/working-remotely-as-a-software-engineer-44dp</guid>
      <description>&lt;p&gt;Since March due to the &lt;em&gt;COVID-19&lt;/em&gt; 🦠, the company I work for, decided to &lt;strong&gt;ditch the office&lt;/strong&gt; and go &lt;strong&gt;full remote&lt;/strong&gt; 🌍.&lt;br&gt;
Within a day everyone moved his workspace from the office to elsewhere 🏠. Actually we do not own a physical space and there's no intention to have it anymore.&lt;/p&gt;

&lt;p&gt;I'm going to give you some &lt;strong&gt;tips&lt;/strong&gt; based on my &lt;strong&gt;remote working experience&lt;/strong&gt; as a &lt;strong&gt;Software Engineer&lt;/strong&gt; on a team that is on the &lt;strong&gt;same timezone&lt;/strong&gt; ⏰.&lt;/p&gt;

&lt;h3&gt;
  
  
  Communication
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;communication environment&lt;/strong&gt; at the office is &lt;em&gt;usually&lt;/em&gt; &lt;strong&gt;synchronous&lt;/strong&gt; and &lt;strong&gt;face to face&lt;/strong&gt; 🗣. It's really easy to reach anyone, you just need to approach to his table, and that's all right ?&lt;/p&gt;

&lt;p&gt;However working &lt;strong&gt;remote&lt;/strong&gt; means you can &lt;strong&gt;work anywhere&lt;/strong&gt; you want in the &lt;strong&gt;world&lt;/strong&gt;. So you don't have the possibility to reach your colleague table and talk with them.&lt;/p&gt;

&lt;p&gt;This leads to the most essential part of remote work the &lt;strong&gt;asynchronous isolated communication&lt;/strong&gt; 💬. From this point you should understand that you'll be communicating through a screen. This &lt;strong&gt;introduces&lt;/strong&gt; a &lt;strong&gt;layer of complexity&lt;/strong&gt; to the &lt;strong&gt;communication process&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Why? You might ask, we can use video conference software! Yes but, it's not the same for the reason that we are &lt;strong&gt;not used&lt;/strong&gt; to it, because &lt;a href="https://www.goodreads.com/quotes/183896-man-is-by-nature-a-social-animal-an-individual-who"&gt;&lt;strong&gt;humans&lt;/strong&gt; by nature are &lt;strong&gt;social animals&lt;/strong&gt;&lt;/a&gt; and we are made to interact physically.&lt;/p&gt;

&lt;p&gt;Keep in mind that a video conference is a &lt;em&gt;synchronous&lt;/em&gt; process, so you won't be able to use it anytime, it will depend on the momentum of the coworker you want to contact. Luckily &lt;strong&gt;text communication&lt;/strong&gt; comes to the rescue! ⛑️&lt;/p&gt;

&lt;h4&gt;
  
  
  Assume positive intent and be kind
&lt;/h4&gt;

&lt;p&gt;Always begin with a &lt;strong&gt;position&lt;/strong&gt; of &lt;strong&gt;positivity&lt;/strong&gt; 😊, the communication will flow a lot easier as a result of how our brain 🧠 processes positive intent ❤️&lt;/p&gt;

&lt;p&gt;Before sending a message read it a couple of times and think about this: &lt;em&gt;Would I say this exactly word by word to the person's face ?&lt;/em&gt; — If the &lt;strong&gt;answer is no&lt;/strong&gt;, &lt;strong&gt;don't send the message&lt;/strong&gt; ✋&lt;/p&gt;

&lt;p&gt;When you are on a situation where you are &lt;strong&gt;not understanding a colleague&lt;/strong&gt;, &lt;strong&gt;jump&lt;/strong&gt; on a &lt;strong&gt;video call&lt;/strong&gt;. You will find that a long discussion could be closed on a &lt;strong&gt;10 minutes talk&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use public channels
&lt;/h4&gt;

&lt;p&gt;When using your company's internal communication tool always &lt;strong&gt;try to communicate on public channels&lt;/strong&gt; avoid &lt;strong&gt;group chats&lt;/strong&gt; or &lt;strong&gt;direct messages&lt;/strong&gt; as possible. For the simple reason that &lt;a href="http://blog.flowdock.com/2014/04/30/beware-of-private-conversations/"&gt;private communication discourages collaboration&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'm pretty sure that anyone on your team could benefit from reading certain discussions to stay informed on whatever you are working on. This will help to embrace team work and collaboration 💪&lt;/p&gt;

&lt;p&gt;If you feel that the topic/resolution is important, &lt;strong&gt;you should document it&lt;/strong&gt; somewhere else &lt;strong&gt;outside&lt;/strong&gt; the &lt;strong&gt;communication tool&lt;/strong&gt;. Otherwise you'll be losing &lt;strong&gt;essential information&lt;/strong&gt; that could be shared and will be lost on the history of the chat.&lt;/p&gt;

&lt;h4&gt;
  
  
  Connect with the team frequently
&lt;/h4&gt;

&lt;p&gt;Do &lt;strong&gt;video calls&lt;/strong&gt; with your team mates &lt;strong&gt;frequently&lt;/strong&gt; to make &lt;strong&gt;them feel connected&lt;/strong&gt;. Working with nobody next to you could be tough and depressing sometimes. I found that proposing a &lt;strong&gt;10 minutes daily meeting&lt;/strong&gt; from the beginning of our remote situation helped people to feel better.&lt;/p&gt;

&lt;p&gt;Add a &lt;strong&gt;social aspect&lt;/strong&gt; to those meetings, talk about trivial things, sports, news, personal stuff... You'll maintain and improve the relationship with your colleagues 🙏&lt;/p&gt;

&lt;h3&gt;
  
  
  Organization
&lt;/h3&gt;

&lt;p&gt;As a distributed team it's essential that &lt;strong&gt;everyone&lt;/strong&gt; on the squad &lt;strong&gt;knows&lt;/strong&gt; the &lt;strong&gt;company roadmap&lt;/strong&gt;. As a Software Engineer being aware of the objectives will provide us a &lt;strong&gt;comfortable habitat&lt;/strong&gt; to work on. Because we won't like to feel as machines 🤖 who write code without any clear purpose 👎&lt;/p&gt;

&lt;p&gt;Personally, I feel much more &lt;strong&gt;motivated&lt;/strong&gt; with the product and problems If I understand the &lt;strong&gt;background&lt;/strong&gt; of what I'm working on.&lt;/p&gt;

&lt;p&gt;Knowing &lt;strong&gt;what&lt;/strong&gt; person &lt;strong&gt;is working on&lt;/strong&gt; is really valuable for the team. It will help to avoid blocks between engineers and provide them a &lt;strong&gt;clear picture&lt;/strong&gt; of the &lt;strong&gt;status&lt;/strong&gt; of any &lt;strong&gt;task&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use a software development methodology
&lt;/h4&gt;

&lt;p&gt;Choose a &lt;strong&gt;system to distribute&lt;/strong&gt; the &lt;strong&gt;workload&lt;/strong&gt; on the team. Try the different frameworks that exist out there and pick the one who works best for your team. We use &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/Kanban_(development)"&gt;Kanban&lt;/a&gt;&lt;/strong&gt; flow and we're pretty happy with it.&lt;/p&gt;

&lt;p&gt;With a methodology like Kanban you'll &lt;strong&gt;reduce&lt;/strong&gt; the &lt;strong&gt;overhead&lt;/strong&gt; of &lt;strong&gt;organizing tasks remotely&lt;/strong&gt; and you'll make visible the work that every member is doing on a daily basis to the rest of the team 👀&lt;/p&gt;

&lt;h4&gt;
  
  
  Manage your working hours properly
&lt;/h4&gt;

&lt;p&gt;The first two months of going remote I made a &lt;strong&gt;ton&lt;/strong&gt; of &lt;strong&gt;overtime hours&lt;/strong&gt;. We had a critical situation at the company and we had to make an effort. I spent a big time on my home office / bedroom coding. Since our &lt;strong&gt;whole country&lt;/strong&gt; was on &lt;strong&gt;lockdown&lt;/strong&gt; 🔒 due to pandemic, I had nothing else to do 👨‍💻&lt;/p&gt;

&lt;p&gt;You should &lt;strong&gt;be very careful&lt;/strong&gt; with your &lt;strong&gt;working hours&lt;/strong&gt; on remote, because our &lt;strong&gt;body&lt;/strong&gt; needs &lt;strong&gt;rest&lt;/strong&gt; time 😴 &lt;strong&gt;sport&lt;/strong&gt; 🏊‍♂️ and &lt;strong&gt;entertainment&lt;/strong&gt; 🎮. A situation like this could end up with a &lt;strong&gt;burn-out&lt;/strong&gt; 🔥, make sure you &lt;strong&gt;protect your time&lt;/strong&gt; 💖&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adjust&lt;/strong&gt; your &lt;strong&gt;schedule flexibly&lt;/strong&gt; to your &lt;strong&gt;daily routine&lt;/strong&gt;, that's one of the &lt;strong&gt;best perks&lt;/strong&gt; of working remote!. You can code whenever you feel most focused, &lt;strong&gt;need a break&lt;/strong&gt;? Go for a walk and grab a cup of tea 🍵&lt;/p&gt;

&lt;h3&gt;
  
  
  Documentation
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Writing documentation&lt;/strong&gt; is an &lt;strong&gt;essential process&lt;/strong&gt; on a &lt;strong&gt;remote based company&lt;/strong&gt;. Have you ever hired someone remotely? &lt;em&gt;What an onboarding should be to a new engineer?&lt;/em&gt;. &lt;em&gt;How our business model works?&lt;/em&gt;. All those questions and &lt;em&gt;more&lt;/em&gt; &lt;strong&gt;should be documented&lt;/strong&gt; on a &lt;strong&gt;knowledge base&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is something really important because you won't be at the office anymore so it's not that easier to communicate with people, specially with someone you don't know.&lt;/p&gt;

&lt;p&gt;You will &lt;strong&gt;repeat and explain&lt;/strong&gt; the &lt;strong&gt;same information&lt;/strong&gt; to every new employee, so you have a great opportunity to write it down ✍️ saving you some time on the future.&lt;/p&gt;

&lt;p&gt;There are multiples documentation tools you can use, I'll list some of them as an example: &lt;em&gt;Confluence&lt;/em&gt;, &lt;em&gt;Notion&lt;/em&gt;, &lt;em&gt;Dropbox Paper&lt;/em&gt;, &lt;em&gt;Google Docs&lt;/em&gt; etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Workspace
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Setup&lt;/strong&gt; a &lt;strong&gt;comfortable&lt;/strong&gt; and &lt;strong&gt;ergonomic&lt;/strong&gt; workspace. Design your space as the home office you would be proud to have. You'll spend a lot of hours everyday inside them. Create &lt;strong&gt;your space&lt;/strong&gt;, here's a list of things to consider when designing your setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lighting&lt;/strong&gt; ☀️: Pick up a place with good lighting, a space that have a great amount of &lt;strong&gt;sunlight&lt;/strong&gt; if possible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Office furniture&lt;/strong&gt; 🪑: Get a &lt;strong&gt;good &amp;amp; ergonomic chair&lt;/strong&gt; and &lt;strong&gt;desk&lt;/strong&gt; your body will thank you later.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ventilation&lt;/strong&gt; 💨: Ensure that you have a decent airflow.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  More resources and tips
&lt;/h3&gt;

&lt;p&gt;Here's a list of useful links about &lt;strong&gt;remote work companies&lt;/strong&gt;! Hope you find them interesting 👇&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://about.gitlab.com/company/culture/all-remote/guide"&gt;Gitlab's Guide to All Remote&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://slack.com/intl/en-es/blog/collaboration/remote-work-tips-from-slack-engineering"&gt;Slack – Remote work tips&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://buffer.com/resources/remote-work-lessons/"&gt;Buffer – 40 Lessons From 4 Years of Remote Work&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>remote</category>
    </item>
    <item>
      <title>JavaScript monorepos with Lerna and Yarn Workspaces</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Tue, 01 Oct 2019 20:16:20 +0000</pubDate>
      <link>https://dev.to/carloscuesta/javascript-monorepos-with-lerna-and-yarn-workspaces-4c05</link>
      <guid>https://dev.to/carloscuesta/javascript-monorepos-with-lerna-and-yarn-workspaces-4c05</guid>
      <description>&lt;h4&gt;
  
  
  &lt;em&gt;Originally posted at &lt;a href="https://carloscuesta.me/blog/javascript-monorepos-lerna-yarn-workspaces"&gt;carloscuesta's blog&lt;/a&gt;&lt;/em&gt;
&lt;/h4&gt;

&lt;h3&gt;
  
  
  What is a monorepo ?
&lt;/h3&gt;

&lt;p&gt;The monorepo term is a compound word between &lt;em&gt;"mono"&lt;/em&gt;, from Ancient Greek &lt;a href="https://en.wiktionary.org/wiki/mono-#Etymology"&gt;&lt;em&gt;"mónos"&lt;/em&gt;&lt;/a&gt;, that means &lt;em&gt;"single"&lt;/em&gt; and &lt;em&gt;"repo"&lt;/em&gt; as a shorthand of &lt;em&gt;"repository"&lt;/em&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A monorepo is a strategy for storing code and projects in a single repository.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What are they useful for ?
&lt;/h3&gt;

&lt;h5&gt;
  
  
  ♻️ Reusing isolated pieces of code
&lt;/h5&gt;

&lt;p&gt;Monorepos allow you to reuse packages and code from another modules while keeping them independent and isolated. This is particularly useful when you have a ton of code that you're constantly repeating on different projects.&lt;/p&gt;

&lt;h5&gt;
  
  
  🧰 Simplifying dependencies management
&lt;/h5&gt;

&lt;p&gt;Dependencies are hoisted to the root level of the project, that means you can share dependencies across all the packages that you have in your monorepo. This reduces the overhead from updating and managing multiple versions of the same dependency.&lt;/p&gt;

&lt;h5&gt;
  
  
  🛠 Refactoring cross-project changes
&lt;/h5&gt;

&lt;p&gt;Making cross-repo changes within different repositories is painful. Typically involves manual coordination between teams and repos. For example let's say you have an API that is used by many clients and you want to make a breaking change into the contract. It's not trivial to apply the update to all the clients and then coordinate the deploy of the projects and so on. With a monorepo it's easier since everything is contained in a single unit.&lt;/p&gt;

&lt;p&gt;Before considering to implement a monorepo architecture, make sure you have the problems that this concept solves ⚠️. There's &lt;strong&gt;no need to overengineer&lt;/strong&gt; a project. Remember &lt;strong&gt;keep it simple&lt;/strong&gt; ✨&lt;/p&gt;

&lt;h3&gt;
  
  
  The tools
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;🐉 &lt;strong&gt;&lt;a href="https://lerna.js.org"&gt;Lerna&lt;/a&gt;&lt;/strong&gt;: The tool for managing the monorepo packages.&lt;/li&gt;
&lt;li&gt;📦 &lt;strong&gt;&lt;a href="https://yarnpkg.com/lang/en/docs/workspaces/"&gt;Yarn Workspaces&lt;/a&gt;&lt;/strong&gt;: Multiple packages architecture.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we know what is a monorepo, the tools that we're going to use and what are they useful for, let's create a real example to see how it works.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the monorepo
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Setup
&lt;/h5&gt;

&lt;p&gt;Let's begin creating our monorepo 👏. The first thing we need to do is define the structure of the project. In this example I created two directories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📁 &lt;code&gt;packages/&lt;/code&gt;: This directory will contain the isolated modules that we are going to reuse on all the applications.&lt;/li&gt;
&lt;li&gt;📁 &lt;code&gt;applications/&lt;/code&gt;: This directory will contain all the applications of our monorepo.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
└── src
    ├── applications
    └── packages
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After that, we're going to create &lt;code&gt;package.json&lt;/code&gt; to define the &lt;code&gt;workspaces&lt;/code&gt; and dependencies of our monorepo.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;workspaces&lt;/code&gt; field is what &lt;a href="https://yarnpkg.com/lang/en/docs/workspaces/#toc-how-to-use-it"&gt;Yarn&lt;/a&gt; uses to symlink our code to the &lt;code&gt;node_modules&lt;/code&gt; in order to reuse and import the code, we'll see this later on.&lt;/p&gt;

&lt;p&gt;Finally we install &lt;code&gt;lerna&lt;/code&gt; as a &lt;code&gt;devDependency&lt;/code&gt; to manage the monorepo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"private"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"engines"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"yarn"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&amp;gt;=1.17.3"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"monorepo-example"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"workspaces"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"src/applications/*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"src/packages/*"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"devDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"lerna"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"latest"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, let's define how Lerna is going to manage our monorepo in a &lt;code&gt;lerna.json&lt;/code&gt; configuration file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;packages&lt;/code&gt;: The directories that we defined as &lt;code&gt;workspaces&lt;/code&gt; in the &lt;code&gt;package.json&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/lerna/lerna/tree/master/commands/bootstrap#--npm-client-client"&gt;&lt;code&gt;npmClient&lt;/code&gt;&lt;/a&gt;: The client used to run the commands.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/lerna/lerna/tree/master/commands/bootstrap#--use-workspaces"&gt;&lt;code&gt;useWorkspaces&lt;/code&gt;&lt;/a&gt;: This flag tells lerna that we're going to use &lt;strong&gt;yarn workspaces&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"lerna"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"latest"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"packages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"src/applications/*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"src/packages/*"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"npmClient"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"yarn"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"useWorkspaces"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We finished our setup 🙌! Let's add some simple code to see how we can manage and reuse packages on our monorepo.&lt;/p&gt;

&lt;h5&gt;
  
  
  Creating packages
&lt;/h5&gt;

&lt;p&gt;A package inside our monorepo context, is an isolated and reusable piece of code. That means, every time we want to create a new package, we're going to create a new independent directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
└── packages
    └── sayHello
        ├── index.js
        └── package.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Each package needs to have a &lt;code&gt;package.json&lt;/code&gt; with the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;version&lt;/code&gt; &lt;strong&gt;fields defined&lt;/strong&gt;. This is important because this describes how we're going to import and use this package on the code base. You can also have dependencies in your package if you need them. On this example I'm writing a simple package called &lt;code&gt;sayHello&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"@packages/sayHello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Think of every directory inside the &lt;code&gt;packages/&lt;/code&gt; folder as an &lt;strong&gt;isolated module&lt;/strong&gt;, with his own &lt;strong&gt;tests&lt;/strong&gt;, &lt;strong&gt;dependencies&lt;/strong&gt; and &lt;strong&gt;code&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;name&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Using packages
&lt;/h5&gt;

&lt;p&gt;This was pretty simple right? Now let's say that we have an application that it's called &lt;code&gt;cli&lt;/code&gt;. In order to use &lt;code&gt;sayHello&lt;/code&gt; package we should &lt;strong&gt;add it&lt;/strong&gt; as a &lt;code&gt;dependency&lt;/code&gt; on the &lt;code&gt;package.json&lt;/code&gt; file. To do that we have a fancy &lt;code&gt;yarn&lt;/code&gt; command 🎉&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;yarn workspace @applications/cli add @packages/sayHello@1.0.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now from our &lt;code&gt;cli&lt;/code&gt; application we can &lt;strong&gt;import&lt;/strong&gt; and &lt;strong&gt;use&lt;/strong&gt; the package! 💯&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@packages/sayHello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Carlos&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, we run our &lt;code&gt;cli&lt;/code&gt; application from the command line using Lerna 🚀&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BAPX-9RM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/monorepo-workspaces.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BAPX-9RM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/monorepo-workspaces.png" alt="monorepo-workspaces"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can find the example explained on the post on &lt;a href="https://github.com/carloscuesta/monorepo-example"&gt;this GitHub repository&lt;/a&gt; 👀. I know this was &lt;strong&gt;pretty simple&lt;/strong&gt;, but there are a &lt;strong&gt;ton of things you can do with monorepos&lt;/strong&gt;! For example you can share react components in different applications while keeping them isolated. But take a look below 👇 to see monorepos on big open source projects!&lt;/p&gt;

&lt;h3&gt;
  
  
  Opensource monorepo projects
&lt;/h3&gt;

&lt;p&gt;Here's a list of well known open source projects that are using the monorepo architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/facebook/react"&gt;React&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/babel/babel"&gt;Babel&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/facebook/jest"&gt;Jest&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/storybookjs/storybook"&gt;Storybook&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>monorepo</category>
      <category>lerna</category>
      <category>yarnworkspaces</category>
    </item>
    <item>
      <title>Managing React-Native crashes with Error Boundaries</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Thu, 12 Sep 2019 15:04:53 +0000</pubDate>
      <link>https://dev.to/carloscuesta/managing-react-native-crashes-with-error-boundaries-13k</link>
      <guid>https://dev.to/carloscuesta/managing-react-native-crashes-with-error-boundaries-13k</guid>
      <description>&lt;h4&gt;
  
  
  &lt;em&gt;Originally posted at &lt;a href="https://carloscuesta.me/blog/managing-react-native-crashes-with-error-boundaries" rel="noopener noreferrer"&gt;carloscuesta's blog&lt;/a&gt;&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://github.com/facebook/react/blob/master/CHANGELOG.md#1600-september-26-2017" rel="noopener noreferrer"&gt;React 16&lt;/a&gt; released a new concept called &lt;a href="https://reactjs.org/docs/error-boundaries.html#introducing-error-boundaries" rel="noopener noreferrer"&gt;&lt;strong&gt;Error Boundary&lt;/strong&gt;&lt;/a&gt;. This concept introduces a new way to catch &lt;strong&gt;JavaScript errors&lt;/strong&gt; 🐛 in a React project.&lt;/p&gt;

&lt;p&gt;In this post I'm going to explain why it's important and how you can use error boundaries in a React-Native application to improve error resiliency, so let's get into it! 👨‍💻&lt;/p&gt;

&lt;h3&gt;
  
  
  Why you should use them ?
&lt;/h3&gt;

&lt;p&gt;According to the &lt;a href="https://reactjs.org/docs/error-boundaries.html#new-behavior-for-uncaught-errors" rel="noopener noreferrer"&gt;official React docs&lt;/a&gt; 📘:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As of &lt;strong&gt;React 16&lt;/strong&gt;, &lt;strong&gt;errors&lt;/strong&gt; that were &lt;strong&gt;not caught&lt;/strong&gt; by any &lt;strong&gt;error boundary&lt;/strong&gt; will &lt;strong&gt;result&lt;/strong&gt; in &lt;strong&gt;unmounting&lt;/strong&gt; of the &lt;strong&gt;whole&lt;/strong&gt; React &lt;strong&gt;component tree&lt;/strong&gt; 😱.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Unmounting&lt;/strong&gt; the &lt;strong&gt;whole&lt;/strong&gt; React &lt;strong&gt;component&lt;/strong&gt; tree, &lt;strong&gt;means&lt;/strong&gt; that if you don't catch errors at all the &lt;strong&gt;user&lt;/strong&gt; will &lt;strong&gt;see&lt;/strong&gt; an &lt;strong&gt;empty white screen&lt;/strong&gt; 💥. Most of the time without having any feedback. This is not a great UX ❌, fortunately you can &lt;strong&gt;fix&lt;/strong&gt; this by &lt;strong&gt;using&lt;/strong&gt; Error Boundaries ✅. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcarloscuesta%2Fimage%2Fupload%2Freact-native-error-unmounted-tree.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcarloscuesta%2Fimage%2Fupload%2Freact-native-error-unmounted-tree.png" alt="React-Native unmounted component tree error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use Error Boundaries
&lt;/h3&gt;

&lt;p&gt;To benefit from Error Boundaries, we'll have to &lt;strong&gt;create&lt;/strong&gt; a &lt;strong&gt;stateful component&lt;/strong&gt; that will use the following lifecycle methods ♻️:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://reactjs.org/docs/react-component.html#static-getderivedstatefromerror" rel="noopener noreferrer"&gt;&lt;code&gt;getDerivedStateFromError&lt;/code&gt;&lt;/a&gt;: This method is going to update the component state to display a fallback UI.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://reactjs.org/docs/react-component.html#componentdidcatch" rel="noopener noreferrer"&gt;&lt;code&gt;componentDidCatch&lt;/code&gt;&lt;/a&gt;: This method should be used to log the error to an external service.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let's create the component that will catch errors in our application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;hasError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;getDerivedStateFromError &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;hasError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;componentDidCatch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;logErrorToService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;componentStack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render &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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasError&lt;/span&gt;
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;FallbackComponent&lt;/span&gt; &lt;span class="p"&gt;/&amp;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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&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;Pretty &lt;strong&gt;simple&lt;/strong&gt; right? With a few lines of code, you can catch errors on your React-Native app 🎉&lt;/p&gt;

&lt;p&gt;To use it, all you need to do now is to &lt;strong&gt;wrap it around any component that could throw an error&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Children&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This component will catch &lt;strong&gt;all the errors&lt;/strong&gt; that are thrown by &lt;strong&gt;any&lt;/strong&gt; of his &lt;strong&gt;children&lt;/strong&gt;. A common thing is to use it at the &lt;strong&gt;top level of your application&lt;/strong&gt; 🔝 to catch anything without having to use it on every screen or route 👏&lt;/p&gt;

&lt;p&gt;That's how our &lt;code&gt;FallbackComponent&lt;/code&gt; looks whenever an error is thrown by our application 😍&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcarloscuesta%2Fimage%2Fupload%2Freact-native-error-boundary.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcarloscuesta%2Fimage%2Fupload%2Freact-native-error-boundary.png" alt="react-native-error-boundary"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;⚠️ &lt;strong&gt;Error Boundaries only catch JavaScript errors&lt;/strong&gt;, all the native crashes that your application might have are not handled. &lt;/p&gt;

&lt;h3&gt;
  
  
  Introducing &lt;a href="https://github.com/carloscuesta/react-native-error-boundary" rel="noopener noreferrer"&gt;&lt;code&gt;react-native-error-boundary&lt;/code&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Few months ago, I created a &lt;strong&gt;simple&lt;/strong&gt;, &lt;strong&gt;flexible&lt;/strong&gt; and &lt;strong&gt;reusable&lt;/strong&gt; React-Native &lt;strong&gt;error boundary component&lt;/strong&gt;. &lt;a href="https://github.com/carloscuesta/react-native-error-boundary" rel="noopener noreferrer"&gt;Take a look into it&lt;/a&gt; 👀 if you're thinking about adding error boundaries to your app! &lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>errorboundaries</category>
      <category>crashes</category>
    </item>
    <item>
      <title>Scalable React Components architecture</title>
      <dc:creator>Carlos Cuesta</dc:creator>
      <pubDate>Tue, 16 Oct 2018 15:30:21 +0000</pubDate>
      <link>https://dev.to/carloscuesta/scalable-react-components-architecture-3k2e</link>
      <guid>https://dev.to/carloscuesta/scalable-react-components-architecture-3k2e</guid>
      <description>&lt;h4&gt;
  
  
  &lt;em&gt;Originally posted at &lt;a href="https://carloscuesta.me/blog/scalable-react-components-architecture"&gt;carloscuesta's blog&lt;/a&gt;&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;It's been a while since I've started working with &lt;a href="http://reactjs.org"&gt;&lt;strong&gt;React&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://facebook.github.io/react-native/"&gt;&lt;strong&gt;React-Native&lt;/strong&gt;&lt;/a&gt; in production. One of the &lt;strong&gt;greatest things&lt;/strong&gt; about &lt;strong&gt;React&lt;/strong&gt; is the &lt;strong&gt;flexibility&lt;/strong&gt; the library gives to you. Meaning that you are free to decide how do you want to implement almost every detail of your project for example the &lt;em&gt;architecture and structure&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;However this freedom on the long term, could lead to a complex and messy codebase, specially if you don't follow a pattern. In this post I'll explain &lt;strong&gt;a simple way&lt;/strong&gt; to &lt;strong&gt;organize&lt;/strong&gt; and &lt;strong&gt;structure&lt;/strong&gt; React &lt;strong&gt;Components&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A Component is a &lt;strong&gt;JavaScript&lt;/strong&gt; &lt;strong&gt;function&lt;/strong&gt; or class that &lt;strong&gt;returns&lt;/strong&gt; a &lt;strong&gt;piece of UI&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We're going to create an &lt;code&gt;EmojiList&lt;/code&gt; component and then we are going to refactor it breaking it up into smaller isolated pieces applying the &lt;strong&gt;folder pattern&lt;/strong&gt;. Here's how our component looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JRJC53zF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/emojilist-compressed.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JRJC53zF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/emojilist-compressed.png" alt="emojilist" width="880" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  EmojiList
&lt;/h3&gt;

&lt;p&gt;As I mentioned before, we can start really simple and small, without following any pattern. This is our &lt;code&gt;EmojiList&lt;/code&gt; component contained in a single function. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/3vjxn0ykyq"&gt;Open Demo on CodeSandbox&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you open the &lt;em&gt;CodeSandbox sidebar&lt;/em&gt; you'll see that our file tree looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── components
│   ├── EmojiList.js
│   └── styles.js
└── index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's &lt;strong&gt;nothing wrong with this approach&lt;/strong&gt;. But on &lt;strong&gt;larger codebases&lt;/strong&gt; that kind of component &lt;strong&gt;becomes hard to maintain&lt;/strong&gt;, because there a lot of things in it: &lt;em&gt;state&lt;/em&gt;, &lt;em&gt;ui&lt;/em&gt;, &lt;em&gt;data&lt;/em&gt;... Take a look at our component code below 👇&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EmojiList.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;EmojiList&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;searchInput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;emojis&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;render&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="nx"&gt;emojis&lt;/span&gt; &lt;span class="o"&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emojis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&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="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
          &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchInput&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Search by name"&lt;/span&gt;
          &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
          &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchInput&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;searchInput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;emojis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;icon&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;code&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;code&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;EmojiList&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A step to improve this code, would be to create separate components into the same file and then using them at the main component. However, you'll be sharing styles among other things and that could be confusing.&lt;/p&gt;

&lt;h4&gt;
  
  
  Refactor
&lt;/h4&gt;

&lt;p&gt;Let's start refactoring the single component into multiple ones by &lt;a href="https://reactjs.org/docs/thinking-in-react.html#step-1-break-the-ui-into-a-component-hierarchy"&gt;breaking up the UI into a component hierarchy&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T2t_wBVk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/emojilist-breakdown.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T2t_wBVk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/carloscuesta/image/upload/emojilist-breakdown.png" alt="emojilist-breakdown" width="880" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we take a look at the image, it's easy to identify that we can &lt;strong&gt;break&lt;/strong&gt; up our &lt;strong&gt;UI&lt;/strong&gt; in &lt;strong&gt;three&lt;/strong&gt; different &lt;strong&gt;components&lt;/strong&gt;: 🛠&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;span&gt;&lt;strong&gt;&lt;code&gt;EmojiList&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;: Combines the smaller components and shares the state down.

&lt;ul&gt;
&lt;li&gt;
&lt;span&gt;&lt;strong&gt;&lt;code&gt;SearchInput&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;: Receives user input and displays the search bar.&lt;/li&gt;
&lt;li&gt;
&lt;span&gt;&lt;strong&gt;&lt;code&gt;EmojiListItem&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;: Displays the List Item for each emoji, with the &lt;em&gt;icon&lt;/em&gt;, &lt;em&gt;name&lt;/em&gt; and &lt;em&gt;description&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We're going to &lt;strong&gt;create&lt;/strong&gt; a &lt;strong&gt;folder&lt;/strong&gt; for &lt;strong&gt;each&lt;/strong&gt; &lt;strong&gt;component&lt;/strong&gt;, with two files, an &lt;code&gt;index.js&lt;/code&gt; that is going to hold all the code for the component and the &lt;code&gt;styles.js&lt;/code&gt;. That's one of the good things about this pattern. Every component defines his own UI and styles, &lt;strong&gt;isolating&lt;/strong&gt; this piece of &lt;strong&gt;code from&lt;/strong&gt; another &lt;strong&gt;components&lt;/strong&gt; that &lt;strong&gt;doesn't need to know anything about them&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/lx8ykrljl9"&gt;Open Demo on CodeSandbox&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that inside the &lt;code&gt;EmojiList&lt;/code&gt; folder, (&lt;em&gt;that is a component&lt;/em&gt;), we add two nested components that only will be used within the &lt;code&gt;EmojiList&lt;/code&gt; component. Again, that's because these two components aren't going to be used out of that context. This helps reducing the visual clutter a lot.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── EmojiList
│   ├── EmojiListItem
│   │   ├── index.js
│   │   └── styles.js
│   ├── SearchInput
│   │   ├── index.js
│   │   └── styles.js
│   ├── index.js
│   └── styles.js
└── index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's isolate and separate the code into the three components from the smallest to the biggest one:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EmojiListItem/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This component renders every emoji item that will appear on the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;EmojiListItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;icon&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;code&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;code&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;EmojiListItem&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;SearchInput/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This component receives the user input and updates the state of the parent component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SearchInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
    &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchInput&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Search by name"&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
    &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;SearchInput&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;EmojiList/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is the top level component, holds the state and data of our example and imports the other components to recreate the whole UI of our tiny application. Isolating components makes the render method more readable and easier to understand ✨.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;SearchInput&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./SearchInput&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;EmojiListItem&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./EmojiListItem&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;EmojiList&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;searchInput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;emojis&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;render&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="nx"&gt;emojis&lt;/span&gt; &lt;span class="o"&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emojis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&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="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;SearchInput&lt;/span&gt;
          &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;searchInput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchInput&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;emojis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EmojiListItem&lt;/span&gt;
            &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
            &lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
            &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
            &lt;span class="na"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;EmojiList&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's basically the architecture that I use at the company I'm working on. I'm &lt;strong&gt;pretty satisfied&lt;/strong&gt; with the &lt;strong&gt;experience&lt;/strong&gt; of using this pattern. Our components turned out a lot easier to maintain and use. Anyway there are &lt;strong&gt;no silver bullets&lt;/strong&gt; on Software Engineering, so &lt;strong&gt;figure what works best for you&lt;/strong&gt; or your team!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
