<?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: DigitalCrafting</title>
    <description>The latest articles on DEV Community by DigitalCrafting (@digitalcrafting).</description>
    <link>https://dev.to/digitalcrafting</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%2F440293%2Fbc7b37bb-325f-463b-8087-23b1dec5aba2.png</url>
      <title>DEV Community: DigitalCrafting</title>
      <link>https://dev.to/digitalcrafting</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/digitalcrafting"/>
    <language>en</language>
    <item>
      <title>Angular vs React</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Thu, 25 Dec 2025 10:09:53 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/angular-vs-react-2hdm</link>
      <guid>https://dev.to/digitalcrafting/angular-vs-react-2hdm</guid>
      <description>&lt;p&gt;I'm not going to compare features of each, because it doesn't matter. You can write good applications in both. What I will compare is: how easy it is to &lt;em&gt;maintain&lt;/em&gt; the project after the initial burst of code finds its way to master/main branch.&lt;/p&gt;

&lt;p&gt;Let's cut to the chase: Angular is easier to maintain in the long run when you have people come and go to the project that lasts for a few years. It gives you everything you need (it's a &lt;em&gt;framework&lt;/em&gt; after all) and it's well structured, even if you have to write a bit more code. &lt;/p&gt;

&lt;p&gt;That being said, React is nicer to write in, and you &lt;em&gt;could&lt;/em&gt; successfully write a large project in it, however, it is more demanding and requires great discipline to do so. It's just a &lt;em&gt;library&lt;/em&gt;, you need to add to it to create something resembling a &lt;em&gt;framework&lt;/em&gt;, and you need the strong will not to chase the new shiny solution, but to keep being consistent for the whole duration of the project. &lt;/p&gt;

&lt;p&gt;Now, how did I get to that conclusion?&lt;/p&gt;

&lt;h2&gt;
  
  
  Projects
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Angular
&lt;/h3&gt;

&lt;p&gt;I've worked with Angular since version 2.0-RC in Banking industry. I wrote code for the Customer Service (in Bank branches, franchises, call center and some stand alone mini-apps). It was all in a single monorepo, it was basically one application consisting of many modules (Customer File Management, Transfers etc), all lazy-loaded, all configurable via admin panel. The modules were very Form heavy, but isolated from each other.&lt;/p&gt;

&lt;h3&gt;
  
  
  React
&lt;/h3&gt;

&lt;p&gt;In React I've worked with deceptively small applications that had a lot of intersections between components and non-trivial business logic. I've used version 16-18 and Next.js. The most demanding was application for Airlines Call Center, which didn't have that much individual pages, but was very feature rich and the individual business components (ex: Tables and Forms for Flights and Services etc.) were reusable across the application. &lt;/p&gt;

&lt;h3&gt;
  
  
  Teams
&lt;/h3&gt;

&lt;p&gt;In both environments I've worked in projects with teams consisting of 4 individual sub-teams, where the level of individual developer varied from junior to senior. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why Angular wins on maintainability?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;It's because people confuse React with a framework.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's get one thing straight - React is a &lt;strong&gt;&lt;em&gt;library&lt;/em&gt;&lt;/strong&gt;. When you visit &lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;React.dev&lt;/a&gt;, you see:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The library for web and native user interfaces&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Whereas if you visit &lt;a href="https://angular.dev/" rel="noopener noreferrer"&gt;Angular.dev&lt;/a&gt;, you see:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The framework for building scalable web apps with confidence&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And this makes ALL the difference.&lt;/p&gt;

&lt;p&gt;Developers, even some of the senior FE developers, seem to think that React is all you need to write good application, and &lt;code&gt;hooks&lt;/code&gt; (looking at you &lt;code&gt;useEffect&lt;/code&gt;) are a god-sent. The React courses or tutorials are not helping by suggesting you should be calling API in the &lt;code&gt;useEffect&lt;/code&gt; on state changes.&lt;/p&gt;

&lt;p&gt;Tracking the cause-and-effect of React hook dependencies is a &lt;strong&gt;&lt;em&gt;nightmare&lt;/em&gt;&lt;/strong&gt;. It might work in small, short lived project like a portfolio website, but when we get more and more requirements and features in our application, it &lt;strong&gt;very&lt;/strong&gt; quickly spirals out of control. &lt;/p&gt;

&lt;p&gt;In order for React to work well for your project, you need to compose a set of libraries that work well together. You need to understand which patterns work in certain scenarios and which don't. You cannot just slap &lt;code&gt;useEffect&lt;/code&gt; everywhere and hope for the best.&lt;/p&gt;

&lt;p&gt;Little to no one cares about how many times the component re-evaluation is triggered, least of all the creators of libraries for React. One of the &lt;code&gt;form&lt;/code&gt; libraries triggers the re-evaluation all the way from the root component at least 2 times, simply because the internal form state is defined as &lt;code&gt;useState&lt;/code&gt; and is updated &lt;strong&gt;every&lt;/strong&gt; time the input value changes. That's not acceptable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where React shines
&lt;/h2&gt;

&lt;p&gt;React can be very nice for a team of seasoned developers, who understand that not everything needs to be written "the React way" - meaning hooks. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;JSX&lt;/code&gt; is amazing for writing components. &lt;code&gt;children&lt;/code&gt; prop is my favorite feature of the whole library, it was so refreshing to easily write wrapper components without having to worry about lifecycle.&lt;/p&gt;

&lt;p&gt;With React you can easily shed all the dead weight slowing your application down but it comes at a cost of having to manually control pretty much your whole components life cycle, and most developers are not used to that. &lt;/p&gt;

&lt;h2&gt;
  
  
  Where Angular fall short
&lt;/h2&gt;

&lt;p&gt;As pretty much every single article comparing the two states: Angular can be a bit harder to start with, mainly because of all the files you have to create, and all the life cycle methods you need to understand, but they are there for a reason. They work really well in a large, corporate projects.&lt;/p&gt;

&lt;p&gt;Don't mistake &lt;code&gt;standalone&lt;/code&gt; components for a full replacement of &lt;code&gt;modules&lt;/code&gt;. &lt;code&gt;Modules&lt;/code&gt; still are the go-to solution for structuring your code, especially in larger projects. Let's take a Money Transfer as an example, where you would have at least a few components, services and configurations, each for every type of Transfer (Own, Internal, External, Cross-Border etc.). You could take that module and lazy-load it as a coherent whole. Not only that, but you can assign modules to a specific teams, which would be responsible for their modules without conflicting with other teams. &lt;/p&gt;

&lt;h2&gt;
  
  
  Final Recommendation
&lt;/h2&gt;

&lt;p&gt;For small applications, like a Portfolio, it doesn't matter, because the impact will be minimal, and you will likely be the only developer there. &lt;/p&gt;

&lt;p&gt;For anything bigger, you should consider all the points above - will you choose Angular and have easier time maintaining it regardless of developers seniority? Or will you opt for React and deal with the future complexity yourself?&lt;/p&gt;

&lt;p&gt;If you still opt for React, I have following suggestions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Limit &lt;code&gt;useEffect&lt;/code&gt; to a minimum.&lt;/strong&gt; Prefer explicit calls on an event (such as click) to implicit call on state change,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separate business logic from React components.&lt;/strong&gt; Keep complex logic in services, so that React components are only responsible for rendering,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the project consistent.&lt;/strong&gt; - avoid new patterns or libraries in the middle of it, unless the business requirement demands it. Consistency matters.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>react</category>
      <category>angular</category>
    </item>
    <item>
      <title>Programming from Top to Bottom - Parsing</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Mon, 18 Mar 2024 15:28:30 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/programming-from-top-to-bottom-parsing-5e67</link>
      <guid>https://dev.to/digitalcrafting/programming-from-top-to-bottom-parsing-5e67</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/digitalcrafting/programming-from-top-to-bottom-the-basics-ba"&gt;previous article&lt;/a&gt; we saw that there is quite a gap between what we write and what CPU does and we also saw a simplified flow diagram of interpreted and compiled languages.&lt;/p&gt;

&lt;p&gt;We will start with interpreted languages, and then move to VM and natively compiled ones, as they are progressively more complicated.&lt;/p&gt;

&lt;p&gt;But before all that, let's ask ourselves more general question: &lt;/p&gt;

&lt;h3&gt;
  
  
  What is &lt;em&gt;programming language&lt;/em&gt; built of?
&lt;/h3&gt;

&lt;p&gt;Well, let's start with spoken language. It is built of &lt;em&gt;words&lt;/em&gt; and &lt;em&gt;grammar&lt;/em&gt;. Words are used to &lt;em&gt;name/describe things/actions&lt;/em&gt;, and grammar defines &lt;em&gt;how&lt;/em&gt; it can be done. For example, expression &lt;code&gt;high mountain&lt;/code&gt;, consists of two words &lt;code&gt;high&lt;/code&gt; and &lt;code&gt;mountain&lt;/code&gt;. Grammar tell us, that: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;high&lt;/strong&gt; is an &lt;em&gt;adjective&lt;/em&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;adjectives&lt;/em&gt; describe things,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mountain&lt;/strong&gt; is a &lt;em&gt;noun&lt;/em&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;nouns&lt;/em&gt; name things,&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;adjective&lt;/em&gt; must come before &lt;em&gt;noun&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks to those simple rules, we know how to decipher the expression &lt;code&gt;high mountain&lt;/code&gt;, and many more like that.&lt;/p&gt;

&lt;p&gt;Getting back to programming: programming language is pretty much &lt;strong&gt;the same&lt;/strong&gt;, only the grammar is simpler. Also, when talking about programming languages &lt;strong&gt;words&lt;/strong&gt; are called &lt;strong&gt;tokens&lt;/strong&gt;, and the types of tokens are named differently (although, you could name them &lt;em&gt;adjectives&lt;/em&gt; and &lt;em&gt;nouns&lt;/em&gt; if you wished).   &lt;/p&gt;

&lt;p&gt;Much like for spoken language, programming language grammar, aside from naming types of tokens and what they look like, tells us what we can do with them. For example, for this expression: &lt;code&gt;int x;&lt;/code&gt;, it tells us that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;int&lt;/strong&gt; is a &lt;em&gt;type&lt;/em&gt;,&lt;/li&gt;
&lt;li&gt;when the expression starts with &lt;em&gt;type&lt;/em&gt;, it's also a &lt;em&gt;declaration&lt;/em&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;x&lt;/strong&gt; is an &lt;em&gt;identifier&lt;/em&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;type&lt;/em&gt; must always come before &lt;em&gt;identifier&lt;/em&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;;&lt;/strong&gt; marks end of expression,&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you compare the &lt;code&gt;high mountain&lt;/code&gt; with &lt;code&gt;int x;&lt;/code&gt; side by side, they are not that different in how they are described.&lt;/p&gt;

&lt;p&gt;With more words comes the complexity, so in order to group them together, we form &lt;code&gt;sentences&lt;/code&gt;, and it's no different in programming - it is called an &lt;code&gt;expression&lt;/code&gt; or a &lt;code&gt;statement&lt;/code&gt;. As in natural languages, an &lt;code&gt;expression&lt;/code&gt; can be simple or complex consisting of 1 or more other &lt;code&gt;expressions&lt;/code&gt; or &lt;code&gt;statements&lt;/code&gt;, but we will get to that. &lt;/p&gt;

&lt;p&gt;To summarize:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;word&lt;/code&gt; is called a &lt;code&gt;token&lt;/code&gt; in programming language,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sentence&lt;/code&gt; is called an &lt;code&gt;expression&lt;/code&gt; or a &lt;code&gt;statement&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;other elements differ from language to language.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's all well and good, but in order for CPU to actually do something, we have to parse programming language and transform it into machine code. So, the next natural questions is:&lt;/p&gt;

&lt;h3&gt;
  
  
  What is parsing?
&lt;/h3&gt;

&lt;p&gt;Simply put: &lt;em&gt;it's translating text into a data structure, based on some specification&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;That's it. In case of programming languages, the specification is called &lt;em&gt;grammar&lt;/em&gt;, which we explained in previous section. If you are interested in more detailed explanation, you can check &lt;a href="https://tomassetti.me/guide-parsing-algorithms-terminology/" rel="noopener noreferrer"&gt;this article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Parser itself is made out of 2 components: &lt;strong&gt;tokenizer&lt;/strong&gt; (also called &lt;strong&gt;lexer&lt;/strong&gt;) and &lt;strong&gt;AST generator&lt;/strong&gt; (actually it's called &lt;strong&gt;parser&lt;/strong&gt; in other sources, don't ask me why parser contains parser, we probably run out of names).&lt;/p&gt;

&lt;h3&gt;
  
  
  Tokenizer
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Tokenizer&lt;/strong&gt; is responsible for taking in file containing some text, and dividing it into tokens (words).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Token&lt;/strong&gt; is much simpler than a spoken word. Unlike in the spoken language, there can be no confusion about the meaning of a token. Let's look at a bit more complicated example, a code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can be divided into following &lt;strong&gt;tokens&lt;/strong&gt;:&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%2Fqlfr7vnqs4l46yp07ede.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%2Fqlfr7vnqs4l46yp07ede.png" alt="Tokens example" width="471" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can never mistake &lt;em&gt;type_declaration&lt;/em&gt; with an &lt;em&gt;identifier&lt;/em&gt;, otherwise the program will not work. Aside from that constraint, you are free to name them whatever you like, there is no one standard, and each parser has it own naming conventions, unless you are planning to use something like &lt;a href="https://llvm.org/" rel="noopener noreferrer"&gt;LLVM&lt;/a&gt;. If you are interested, you can see examples of naming in different language parsers &lt;a href="https://astexplorer.net/" rel="noopener noreferrer"&gt;in the AST Explorer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Actually, there is one more token type that's not presented on the diagram: &lt;em&gt;white_spaces&lt;/em&gt;. Unless it's Python or YAML, we will simply ignore the whitespace characters, their only purpose is to divide other tokens that would otherwise be indistinguishable, like &lt;code&gt;int value&lt;/code&gt; from &lt;code&gt;intvalue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, a &lt;strong&gt;tokenizer&lt;/strong&gt; goes through a file, and reads all the tokens one by one, returning them to &lt;strong&gt;parser&lt;/strong&gt;. How does it know which token it read? By using regex. Each token definition has regex expression associated with it, and we check if the beginning of our string matches one of the token types, otherwise we return &lt;strong&gt;null&lt;/strong&gt; or throw an error. You could write it simply checking each and every character in order to avoid nested looping, but for this article we are not be doing that.&lt;/p&gt;

&lt;p&gt;Notice that we didn't yet touched on the grammar defining the order of tokens, that's the AST generator's task.&lt;/p&gt;

&lt;h3&gt;
  
  
  AST generator - actual parser
&lt;/h3&gt;

&lt;p&gt;But wait, what is an AST? It's an acronym for &lt;strong&gt;Abstract Syntax Tree&lt;/strong&gt;, and it's a intermediate representation of our code in a form of... well... a tree (finally those data structure courses can be of some use).&lt;/p&gt;

&lt;p&gt;Why do we even need that? For type and grammar checking, optimizations, and it's easier to traverse than an array of tokens.&lt;/p&gt;

&lt;p&gt;In our example, the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will be represented as:&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%2Fyzlp7wd19zr1nssub880.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%2Fyzlp7wd19zr1nssub880.png" alt="AST Example Simple Expression" width="491" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may notice the missing &lt;strong&gt;;&lt;/strong&gt; - that's because it's only a delimiter - it marks the end of a single &lt;code&gt;expression&lt;/code&gt; and is not relevant for further processing. Also, in this diagram I don't show the metadata for each node, such as type of the token, but it's there.&lt;/p&gt;

&lt;p&gt;Going from the top, the expression can be easily interpreted as an assignment &lt;code&gt;expression&lt;/code&gt;, where we declare a new variable called &lt;strong&gt;x&lt;/strong&gt; of type &lt;strong&gt;int&lt;/strong&gt;, and &lt;em&gt;assign&lt;/em&gt; to it the result of &lt;em&gt;adding&lt;/em&gt; value from variable &lt;strong&gt;a&lt;/strong&gt; to value from variable &lt;strong&gt;b&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's see more complicated example with a &lt;code&gt;function&lt;/code&gt; declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The AST for that piece of code would look something like this:&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%2F0xecx811rr7d8e3ddd4c.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%2F0xecx811rr7d8e3ddd4c.png" alt="AST Example Function" width="551" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, that became quite big really quick, didn't it? If we added a class on top of that, the diagram would be twice as big, so from now on, we will switch to &lt;a href="https://www.json.org/json-en.html" rel="noopener noreferrer"&gt;JSON&lt;/a&gt; representation of a tree, and only for parts that we actually need.&lt;/p&gt;

&lt;p&gt;Thing to note in this example is that a &lt;code&gt;function&lt;/code&gt; has 3 children nodes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;return type&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;parameters&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;body&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;code&gt;body&lt;/code&gt; node is just a list of expressions in order of appearance, ending with optional &lt;code&gt;return&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;Given that information, you can probably guess what a &lt;code&gt;class&lt;/code&gt; declaration would look like, it would have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;identifier&lt;/code&gt; as a name,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;access modifier&lt;/code&gt; - &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;private&lt;/code&gt; or &lt;code&gt;protected&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;body&lt;/code&gt; which would a list of &lt;code&gt;expressions&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can see where this is going. Each structure and expression in a programming language is well defined and has a set of parameters that it consists of. It also has a defined &lt;em&gt;order&lt;/em&gt; in which they can appear, for example: a class name identifier must appear &lt;em&gt;after&lt;/em&gt; a &lt;code&gt;class&lt;/code&gt; &lt;em&gt;keyword&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;And that is called &lt;strong&gt;programming language grammar&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Grammar
&lt;/h3&gt;

&lt;p&gt;Grammar can be defined using a &lt;a href="https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form" rel="noopener noreferrer"&gt;Backus-Naur Form&lt;/a&gt;, which is a meta language that for me became readable only &lt;em&gt;after&lt;/em&gt; I actually implemented parser, but nevertheless, let's go through a brief introduction now.&lt;/p&gt;

&lt;p&gt;Let's use our original piece of code as an example, which was variable declaration: &lt;code&gt;int x;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is called a &lt;code&gt;declaration expression&lt;/code&gt;, and as name suggests, it's used to declare a variable.&lt;/p&gt;

&lt;p&gt;The grammar for this type of expression, can be described as:&lt;br&gt;
"A type identifier, which is a keyword, followed by a name".&lt;/p&gt;

&lt;p&gt;In Backus-Naur Form (BNF) it can be written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;declaration-expression&amp;gt; ::= &amp;lt;type-identifier&amp;gt; &amp;lt;identifier&amp;gt; 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's look at another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In BNF, this type of expression, can be defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;assignment-expression&amp;gt; ::= &amp;lt;declaration-expression&amp;gt; '=' &amp;lt;expression&amp;gt; | &amp;lt;identifier&amp;gt; '=' &amp;lt;expression&amp;gt; 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Meaning: "An assignment expression is a declaration followed by a '=', followed by an expression, or, an identifier, followed by '=', followed by an expression".&lt;/p&gt;

&lt;p&gt;This can go on and on, and as the expressions become more complex, so does the BNF notation, however, when written right, the BNF can reflect the code quite well, which we will see in the next article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Finally, the theory part is over, at least for the parsing that is 😉&lt;/p&gt;

&lt;p&gt;In the next article, we will dive into the tokenizer and parser code. &lt;/p&gt;

&lt;p&gt;Also note, that what I wrote about is applicable to &lt;em&gt;&lt;strong&gt;any&lt;/strong&gt;&lt;/em&gt; resource, not just programming language. You can use this knowledge to parse JSON, XML, HTML, CSV and whatever else you need, it only comes down to defined grammar.&lt;/p&gt;

&lt;p&gt;See you in the next one!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Programming from Top to Bottom - The Basics</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Sun, 18 Feb 2024 10:40:03 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/programming-from-top-to-bottom-the-basics-ba</link>
      <guid>https://dev.to/digitalcrafting/programming-from-top-to-bottom-the-basics-ba</guid>
      <description>&lt;p&gt;As promised in &lt;a href="https://dev.to/digitalcrafting/a-journey-to-prevent-the-collapse-of-the-web-52o2"&gt;this article&lt;/a&gt;, we are going to dive into how programming actually works from top to bottom. Or in other words: how a CPU can understand what you are writing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; this series of articles is supposed to be a high-level overview, that gives you an idea of how programming works, and a starting point for future learning. As such, it will contain &lt;strong&gt;many&lt;/strong&gt; simplifications.&lt;/em&gt;&lt;/p&gt;

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

&lt;p&gt;Let's start &lt;em&gt;really&lt;/em&gt; high: what is programming?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It's a way of telling computer what to do.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Kind of obvious, right? Let's go a bit deeper: how do you do that?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;By using a programming language like Java.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Correct!&lt;/strong&gt; Well, mostly. That's because a computer, or rather a CPU, doesn't understand language as you understand it. It doesn't matter if it's Python, Java, C or Assembly, a CPU doesn't understand what you are writing. What does the CPU understand then? 1s and 0s. And it's not even a word &lt;code&gt;function&lt;/code&gt; represented as 1s and 0s, the CPU does not have a concept of &lt;code&gt;function&lt;/code&gt; or &lt;code&gt;class&lt;/code&gt;, or any keyword for that matter. What we, the programmers, understand as a "programming language", is soooooooo far from what actually happens, that it's really hard to imagine it. &lt;/p&gt;

&lt;p&gt;And that's &lt;em&gt;mostly&lt;/em&gt; ok. You don't need to understand how circuitry works to be a good developer, however, you should be aware of how much extra work a CPU needs to do in order to interpret high-level programming language, and how you can write code that's easier to process by CPU. &lt;/p&gt;

&lt;p&gt;You might think that compilers should take or of that, but compilers are simply translators. Much like CPU, they do not understand what you want to achieve, they just process text. Of course, there are some optimizations that they can do, but they will not rewrite your code to 100% efficiency, not even close.&lt;/p&gt;

&lt;p&gt;In order to understand why though, we first need to understand:&lt;/p&gt;

&lt;h3&gt;
  
  
  What does the CPU do, actually?
&lt;/h3&gt;

&lt;p&gt;At the core of it, the CPU only does few things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;binary operations,&lt;/li&gt;
&lt;li&gt;math operations,&lt;/li&gt;
&lt;li&gt;reading from memory,&lt;/li&gt;
&lt;li&gt;writing to memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's &lt;em&gt;mostly&lt;/em&gt; it. If you are interested, you can check the &lt;a href="https://edge.edx.org/c4x/BITSPilani/EEE231/asset/8086_family_Users_Manual_1_.pdf" rel="noopener noreferrer"&gt;Intel 8086 Assembly Manual&lt;/a&gt;. It's old, yes, but:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the CPU didn't change that much in terms of what it basically does, the instructions you find in there are still present in the modern CPUs,&lt;/li&gt;
&lt;li&gt;thanks to being limited to 8086, it's easier to wrap your head around it, since compared to modern ones it's quite simple.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order to perform those operations, the CPU has to somehow store values in needs to operate on. For that, it has internal storage, called &lt;a href="https://www.eecg.utoronto.ca/~amza/www.mindsec.com/files/x86regs.html" rel="noopener noreferrer"&gt;Registers&lt;/a&gt;, you can see it in the Manual at page 24. Each register in 8086 has &lt;strong&gt;16 bits&lt;/strong&gt; (modern CPUs have 64 bit registers). Which means that an 8086 CPU can operate on chunks of data that are &lt;strong&gt;at most&lt;/strong&gt; 16 bits at a time. That's &lt;strong&gt;2 bytes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think about that for a second. &lt;/p&gt;

&lt;p&gt;In a world where we operate on whole &lt;strong&gt;objects&lt;/strong&gt; containing various data, how does a CPU handle that? &lt;/p&gt;

&lt;p&gt;That's quite a jump in perception, isn't it? &lt;/p&gt;

&lt;h3&gt;
  
  
  Memory
&lt;/h3&gt;

&lt;p&gt;We now know, that CPU can only operate on very small data chunks, which are stored in memory. So, what does a memory look like?&lt;/p&gt;

&lt;p&gt;You can think of memory as a huge array of bytes, like this:&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%2Fsqh837wh8tb8df4085x2.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%2Fsqh837wh8tb8df4085x2.png" alt="Memory layout" width="721" height="61"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each byte has unique address (12345 - 12351 in the picture), and holds, well, single byte. What happens when data occupies more than 1? It just spills over to the next one and it can occupy as many as it needs, provided there is enough space, of course. For example, string "Hello" would be stored like this:&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%2F51yk79dabr4v7ky84fzq.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%2F51yk79dabr4v7ky84fzq.png" alt="Memory example" width="721" height="61"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;\0&lt;/code&gt; character denotes end of string, otherwise we would never know where to stop when reading it.&lt;/p&gt;




&lt;p&gt;That's it. At the lowest level, that's pretty much how it looks like, just an array of bytes and a piece of hardware that moves it around.&lt;/p&gt;

&lt;p&gt;Even given, that it's a simplified explanation, that's not a lot to work with, isn't it? And yet, we somehow create very complicated programs that do amazing things.&lt;/p&gt;

&lt;p&gt;I will only mention, that the bulk of the job is done by compilers (we will learn about them) and hardware. In the future we will take a look at how hardware communicated - how a monitor knows which pixels to light up, how a network card knows when to send data, etc, but for now let's stay in software realm. &lt;/p&gt;

&lt;p&gt;Let's look at the other side of programs now - programming languages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of Languages
&lt;/h3&gt;

&lt;p&gt;There are few ways to categorize programming languages. For the purpose of this series, I will categorize them like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interpreted - like JavaScript or Python. Meaning, you just feed what you wrote directly to interpreter,&lt;/li&gt;
&lt;li&gt;Virtual machine languages - like Java. Meaning you still need to compile it, but you feed the compiled code to VM.&lt;/li&gt;
&lt;li&gt;Compiled - C, C++, Odin. Languages compiled to machine code for a specific system and CPU architecture.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the 3 things I will cover: language interpretation, VMs and machine code compilation. Again, this will be simplified version, which is supposed to give you an &lt;em&gt;idea&lt;/em&gt;, not a complete solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic flow
&lt;/h3&gt;

&lt;p&gt;For interpreted languages, the flow looks like this:&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%2F32j7ou8egvwbbfyg0v79.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%2F32j7ou8egvwbbfyg0v79.png" alt="Simple interpreted language flow" width="481" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For VMs and Compiled languages it's actually not that different:&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%2Fz6zibd0gwtgvdbehf495.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%2Fz6zibd0gwtgvdbehf495.png" alt="Simple compiled language flow" width="611" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each step can be further divided, but we will talk about that in separate articles. For now, let's just say that whether it's &lt;em&gt;interpretation&lt;/em&gt; or &lt;em&gt;compilation&lt;/em&gt;, the first step is &lt;strong&gt;parsing&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;This looks relatively simple, and it mostly is. It's not that hard to interpret:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tricky part is how do we represent something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;multiply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="n"&gt;tester&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tester&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;multiply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the memory and CPU instructions? &lt;/p&gt;

&lt;p&gt;Spoiler alert: &lt;em&gt;the class does not exist&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;That was a very brief and (hopefully) simple explanation of what the CPU actually does, and it showed the gap between the CPU and what we write. In this series I'm hoping to close that gap just enough for every developer to have a general understanding of the whole process, as I believe it will benefit us in the long run.  &lt;/p&gt;

&lt;p&gt;From the next article, I'm going to explain how it's done from the top in much more detail, starting with &lt;em&gt;parsing&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I hope this article piqued your interest and you learned something interesting 😃 See you in the &lt;a href="https://dev.to/digitalcrafting/programming-from-top-to-bottom-parsing-5e67"&gt;next one&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Java Benchmark Adventures - ArrayList vs LinkedList</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Sun, 04 Feb 2024 16:01:33 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/java-benchmark-adventures-arraylist-vs-linkedlist-4oho</link>
      <guid>https://dev.to/digitalcrafting/java-benchmark-adventures-arraylist-vs-linkedlist-4oho</guid>
      <description>&lt;p&gt;Classic interview question:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"What is the complexity of ArrayList vs LinkedList?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Usually answered with something along the lines of:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Removing items from LinkedList is O(1) while ..."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And that's probably it, however, this doesn't reflect the actual performance of them, and it may lead to a substantial slow down in your code.&lt;/p&gt;

&lt;p&gt;But before we get to that, let's discuss some important implementation details for ArrayList and LinkedList in Java. Keep those in mind when looking at the benchmark results.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Implementation Details
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ArrayList
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It uses primitive array underneath to store values,&lt;/li&gt;
&lt;li&gt;Primitive array is stored as a &lt;strong&gt;continuous&lt;/strong&gt; chunk of memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  LinkedList
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Each element holds value and reference to next &lt;strong&gt;and&lt;/strong&gt; previous element,&lt;/li&gt;
&lt;li&gt;It holds references to both first and last element of the list,&lt;/li&gt;
&lt;li&gt;It optimizes get by index: if the index is less than half the size, start from the beginning, else start from the end,&lt;/li&gt;
&lt;li&gt;Elements are &lt;strong&gt;scattered&lt;/strong&gt; around the memory. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Benchmark
&lt;/h2&gt;

&lt;p&gt;You can see the code for the benchmark &lt;a href="https://github.com/DigitalCrafting/benchmark-adventures/blob/master/src/main/java/org/digitalcrafting/benchmarkadventures/arrayvslinkedlist/ArrayVSLinkedListBenchmark.java" rel="noopener noreferrer"&gt;in this repo&lt;/a&gt;. I used objects instead of usual integer just to have something looking a bit more like a real world example, and I used &lt;a href="https://www.baeldung.com/java-microbenchmark-harness" rel="noopener noreferrer"&gt;JMH&lt;/a&gt; for benchmark.&lt;/p&gt;

&lt;p&gt;Test cases are what you would expect in a normal project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;findByReference&lt;/li&gt;
&lt;li&gt;getByKnownIndex&lt;/li&gt;
&lt;li&gt;iterateOver&lt;/li&gt;
&lt;li&gt;removeByKnownIndex&lt;/li&gt;
&lt;li&gt;removeByReference&lt;/li&gt;
&lt;li&gt;addFirst&lt;/li&gt;
&lt;li&gt;addInTheMiddle&lt;/li&gt;
&lt;li&gt;addLast&lt;/li&gt;
&lt;li&gt;removeFirst&lt;/li&gt;
&lt;li&gt;removeInTheMiddle&lt;/li&gt;
&lt;li&gt;removeLast&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The benchmark creates 2 Lists with 1 million objects each, LinkedList and ArrayList, we also store reference to one of the objects in each List, from around the middle.&lt;/p&gt;

&lt;p&gt;My laptop is IdeaPad Gaming 3 15IMH05 with Intel i7-10750H (12) @ 5.000GHz and 32GB of RAM. I run &lt;a href="https://www.azul.com/downloads/?package=jdk#zulu" rel="noopener noreferrer"&gt;Azul Zulu 17.0.6 Java&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So, what are the results? On my laptop they came to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Benchmark                             Score          Error  Units
arrayList_findByReference       2696499.728 ±    32202.502  ns/op
linkedList_findByReference      7028258.295 ±   411854.774  ns/op
arrayList_getByKnownIndex             1.355 ±        0.020  ns/op
linkedList_getByKnownIndex     14109357.750 ±  4523485.134  ns/op
arrayList_iterateOver           4241855.707 ±    51949.429  ns/op
linkedList_iterateOver         18701734.963 ±   153427.714  ns/op
arrayList_removeByKnownIndex      23807.499 ±    22832.530  ns/op
linkedList_removeByKnownIndex  13611894.721 ±   415849.451  ns/op
arrayList_removeByReference     5446367.115 ±   116991.612  ns/op
linkedList_removeByReference   13715672.076 ±  1153278.238  ns/op
arrayList_addFirst              3220312.800 ±   428670.631  ns/op
linkedList_addFirst                8515.000 ±    24870.360  ns/op
arrayList_addInTheMiddle         815357.800 ±   282617.391  ns/op
linkedList_addInTheMiddle     142979327.200 ± 18151974.063  ns/op
arrayList_addLast                  4606.600 ±    23419.256  ns/op
linkedList_addLast                 1620.400 ±      680.293  ns/op
arrayList_removeFirst           1933858.000 ±   127364.646  ns/op
linkedList_removeFirst             5216.600 ±      713.334  ns/op
arrayList_removeInTheMiddle      824331.600 ±    53493.129  ns/op
linkedList_removeInTheMiddle  143313481.400 ± 34759711.042  ns/op
arrayList_removeLast               3943.600 ±      834.749  ns/op
linkedList_removeLast              5973.800 ±      972.878  ns/op
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, in only 2 of them the ArrayList was clearly slower than LinkedList, and it was only cases at the start of the List. It was similar at the end, and in all others it was at least few times faster. &lt;/p&gt;

&lt;p&gt;You might say: "wait, but that's not what the Big O Notation says, LinkedList should be faster in deletions and inserts", and you would be right, that's exactly what it says.&lt;/p&gt;

&lt;p&gt;So, where does the performance hit come from? &lt;/p&gt;

&lt;h2&gt;
  
  
  The Reason
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Big O
&lt;/h3&gt;

&lt;p&gt;The O(1) for deletion doesn't count &lt;em&gt;finding&lt;/em&gt; the element you need to delete. Yes, the operation itself is just moving around few references, but you need to &lt;em&gt;find&lt;/em&gt; the object first. Some articles, &lt;a href="https://lukabaramishvili.medium.com/data-structure-linked-list-big-o-notation-36333f39ddb6" rel="noopener noreferrer"&gt;like this one&lt;/a&gt;, actually take this into consideration, however, some don't. &lt;/p&gt;

&lt;p&gt;Overall, you need to be aware that removing references is just &lt;em&gt;part&lt;/em&gt; of the algorithm for deletion, if you include searching, then you will come to correct O(n) value for elements in the middle of the List.&lt;/p&gt;

&lt;p&gt;As for the last element: the performance for ArrayList and LinkedList is pretty much the same, because Java implementation of LinkedList holds references to both beginning and end by default. If, for some reason, you would use LinkedList without the back reference, you would need to iterate over the entire list to get to the last element.&lt;/p&gt;

&lt;p&gt;However, that's still not everything.&lt;/p&gt;

&lt;h3&gt;
  
  
  The CPU and Caches
&lt;/h3&gt;

&lt;p&gt;You've probably heard that CPU has Caches - L1, L2, L3 and sometimes even L4, then there is RAM and finally disk. It looks (in great simplification) like this:&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%2Fmj2xejnljq8d1fq8fw00.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%2Fmj2xejnljq8d1fq8fw00.png" alt="CPU Cache" width="372" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you want to access some data in your program, like iterate over an array of elements, it is loaded into the CPU Cache for quick access. The arrows on the diagram show order in which the caches are checked for the data, if it's found in L1, great, if not go check L2 and so on, until finally it checks the disk. &lt;/p&gt;

&lt;p&gt;What helps with loading data into caches is &lt;a href="https://en.wikipedia.org/wiki/Cache_prefetching" rel="noopener noreferrer"&gt;Cache Prefetching&lt;/a&gt;, but that in itself is not important for us.&lt;/p&gt;

&lt;p&gt;The important bit of information is this: &lt;strong&gt;the prefetching is optimized for continuous memory access, so that it can load entire array at once, if it fits into to the cache&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Focus on the word: &lt;strong&gt;continuous&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Array is stored as continuous block of memory, and CPU is optimized for continuous memory access.&lt;/p&gt;

&lt;p&gt;Here is another important piece of knowledge: accessing RAM can be &lt;a href="https://www.intel.com/content/www/us/en/developer/articles/technical/memory-performance-in-a-nutshell.html" rel="noopener noreferrer"&gt;&lt;strong&gt;10 to 100 times slower&lt;/strong&gt;&lt;/a&gt; than accessing cache.&lt;/p&gt;

&lt;p&gt;That's the whole trick.&lt;/p&gt;

&lt;p&gt;Since LinkedList can be randomly scattered around memory, there is no way to load it into the cache at once. You need to first get an element and check the reference of next one before you can get it. Each element has to be accessed separately, 10 to 100 times slower than the elements in ArrayList. &lt;/p&gt;

&lt;p&gt;That is the reason for performance difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is LinkedList useless then?
&lt;/h2&gt;

&lt;p&gt;No. There are cases where LinkedList would be better. From the top of my head: double-ended linked list can be used as a queue with unknown number of elements, in fact a Deque is exactly that. Thanks to having references to both beginning and end, you add elements at the end, and remove at the front with the performance promised by Big O notation.&lt;/p&gt;

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

&lt;p&gt;The biggest lesson here, at least for me, is to remember that the programs we write are executed by the CPU, and we should not forget that. Understanding what happens in the CPU, even generally, can be very valuable. I might not know how the prefetching &lt;em&gt;works&lt;/em&gt;, but it's enough to know how it &lt;em&gt;affects&lt;/em&gt; my program. &lt;/p&gt;

&lt;p&gt;The other take from this is that ArrayList should be used pretty much everywhere by default, unless you have specific requirements that would benefit from LinkedList.&lt;/p&gt;

&lt;p&gt;Hope you found this article useful :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>performance</category>
    </item>
    <item>
      <title>A Journey to Prevent the Collapse of the Web</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Tue, 30 Jan 2024 09:23:54 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/a-journey-to-prevent-the-collapse-of-the-web-52o2</link>
      <guid>https://dev.to/digitalcrafting/a-journey-to-prevent-the-collapse-of-the-web-52o2</guid>
      <description>&lt;p&gt;&lt;em&gt;This article is an introduction, or rather, a starting point to a tree of articles, that will (hopefully) explain how the software actually works from top to bottom.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Inspiration
&lt;/h2&gt;

&lt;p&gt;As some of you probably guessed, title of this article is inspired by Jonathan Blow's talk &lt;a href="https://www.youtube.com/watch?v=ZSRHeXYDLko" rel="noopener noreferrer"&gt;Preventing the Collapse of Civilization / Jonathan Blow (Thekla, Inc)&lt;/a&gt; on YouTube.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/ZSRHeXYDLko"&gt;
&lt;/iframe&gt;
 &lt;/p&gt;

&lt;p&gt;I highly suggest you watch it, even if you've seen it before, because Jon explains everything much better than I ever could in an article.&lt;/p&gt;

&lt;p&gt;Here is the gist of it: civilizations collapse, when there is no longer transfer of knowledge between generations, causing younger people to not understand how things that they are using work, and when those things break, they are unable to fix them and so the collapse happen.&lt;/p&gt;

&lt;p&gt;Jon also gives a number of examples of software failing as a proof of there being something wrong with software development nowadays, and they are spot on.&lt;/p&gt;

&lt;p&gt;Second talk that inspired me is &lt;a href="https://www.youtube.com/watch?v=kZRE7HIO3vk" rel="noopener noreferrer"&gt;The Thirty Million Line Problem&lt;/a&gt; by Casey Muratori (actually, the first 0.5h mostly). &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/kZRE7HIO3vk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In this one, Casey focuses on where the current problems with software come from, giving an excellent hypothesis on the origin of said problem.&lt;/p&gt;

&lt;p&gt;The gist of it being: when the USB and "Plug and Play" was introduced, it became the OS's responsibility to handle all different kinds devices that could now be plugged into the computer, which caused exponential increase in the lines of code in the OS, which spilled over into other areas, which causes people to not understand what is happening in the said systems, as there is simply too much code to allow anyone reason about it. They can reason about &lt;em&gt;part&lt;/em&gt; of the code, but it's unlikely they can reason about everything. And if you can't reason about it, it's effectively a magical black box that hopefully works fine if you just leave it alone. &lt;/p&gt;

&lt;p&gt;These 2 are the videos that sparked my interest and pushed me into learning in depth about how software actually works, and how can we minimize the number of Lines Of Code (LOC), which I would like to share with you in the future articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problems we face
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Moore's Law is dead
&lt;/h3&gt;

&lt;p&gt;Transfer of knowledge and the number of LOC come from the fact that we, programmers, became too comfortable writing code that is nowhere near what actually happens on CPU and the reason for that is the &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/Moore%27s_law" rel="noopener noreferrer"&gt;Moore's Law&lt;/a&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;In short (and simplified): the speed of processors doubled every 2 years or so, RAM improved, and we could get away with getting more and more away from the actual hardware, because it could compensate for abstraction. And so, you didn't have to allocate memory manually - there's VM and Garbage Collection. Then you didn't even have to worry about types - you have dynamically typed languages like Python and JS.&lt;/p&gt;

&lt;p&gt;Here's the thing though: this law is dead, and has been for quite some time. The CPUs are not getting any faster, and to battle that they got more cores now, but they are not faster. And since most of programs are still single-threaded, that doesn't help anyways. However, it looks like software development didn't caught up onto that fact. We keep creating more and more complicated frameworks, which abstract away many problems but introduce other ones. The software is not getting better, in fact, we might be regressing in some areas, not every area, but most of them. &lt;/p&gt;

&lt;p&gt;The websites are slow as hell, backend systems fail with cryptic messages, Windows update forces itself onto you, "smart" phones are getting slower with each update forcing you to buy new phone, the same with "smart" TVs. And these are just the things you probably experienced yourself, there are also fails we will never hear about, because they're just too embarrassing for the companies, like failures in the plane software.&lt;/p&gt;

&lt;p&gt;It's also kind of interesting that we all experience those things, but we're so used to it, that we just go "Eh, let's buy newer faster phone/laptop/pc", "Let's refresh the page and try again", "Let's restart the system", and I was like that too. It wasn't until I saw &lt;a href="https://www.youtube.com/watch?v=ZSRHeXYDLko" rel="noopener noreferrer"&gt;Jon's video&lt;/a&gt;, that I actually stopped thought about it, and decided that that's actually pissing me off quite a bit.&lt;/p&gt;

&lt;h3&gt;
  
  
  Companies value speed
&lt;/h3&gt;

&lt;p&gt;Another problem is that companies, especially startups, value speed over quality, at least until it comes back to bite them in the behind. And it will come to bite them, but it's usually too late to quickly switch framework or programming language. &lt;/p&gt;

&lt;p&gt;It might not be apparent right away, after all, you build a new app and it's great - it's fast, easy to manage, what not to love? &lt;/p&gt;

&lt;p&gt;Well, until it grows and you need to scale up. Suddenly, Python or JS is not enough, but you are not going to rewrite it in C or GO, right? Microservices are a solution for scaling, or at least, that's what you were taught so far, so you do that, and then it becomes a nightmare to manage. You introduce Docker, K8S, you suddenly loose ability to debug whole process easily, changing anything at all becomes super hard and the spiral of death starts.&lt;/p&gt;

&lt;p&gt;And it's not my imagination, I've seen this. This is how it goes when you start with a programming language that is easy and quick to write in, and it grows faster than you anticipated.&lt;/p&gt;

&lt;p&gt;Casey Muratori &lt;a href="https://www.youtube.com/watch?v=x2EOOJg8FkA" rel="noopener noreferrer"&gt;in yet another video&lt;/a&gt; gives examples of when companies actually cared, however, it was usually a large company that could afford it.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Stack
&lt;/h3&gt;

&lt;p&gt;There is a quote I once read and it stuck:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fullstack is not an achievement, it's a minimum f*****g requirement&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And I think it should be a standard, but how can it? The minimum requirement for a job went from algorithms and a programming language (+ css in case of frontend) to a whole stack of stuff built on top of another stuff. Let's try to list what a Fullstack position would require nowadays, shall we? &lt;/p&gt;

&lt;p&gt;Angular/React, Typescript, NodeJS, UI library, Websocket, REST, gRPC, Java, Spring BOOT/Micronaut, JPA/Hibernate, SQL database, NoSQL database, Docker, Kubernetes, AWS/Azure/GCP...&lt;/p&gt;

&lt;p&gt;And probably something I forgot about. That's around &lt;strong&gt;15&lt;/strong&gt; things to grasp. Even if you combine backend and frontend to just using Javascript, it's not feasible for anyone to learn about anything deeper than just &lt;strong&gt;using&lt;/strong&gt; those things in less than a few years. And I'm not even talking about memory management, I'm talking about things like all the possible HTTP Headers you can use, and what you would do with them, or lesser known classes in Vanilla Java. &lt;/p&gt;

&lt;h3&gt;
  
  
  Getting the knowledge is kind of hard
&lt;/h3&gt;

&lt;p&gt;The videos or articles I usually see on YouTube or LinkedIn are targeting Junior Devs. It seems like every month I see the same articles shared again and again: "How to create CRUD application in Spring BOOT", "How to handle errors in Spring BOOT" and similar in other frameworks/languages. How many times are going to repeat the same stuff, instead of going forward?&lt;/p&gt;

&lt;p&gt;Don't get me wrong, there are people with the knowledge willing to share it, there are videos, articles and books, that explains how software works, however, what I found is that there is no single resource that would explain it in relatively simple terms, from top to bottom. Meaning, if I write code a certain way, how does it look on the CPU and in the memory? All the resources I found either explained only one small part and left it at that, or were a full fledged books with intricate details right from the start, which took way too much time to go through for someone like me, who likes to learn by example, get a general understa and get the details later.    &lt;/p&gt;

&lt;h2&gt;
  
  
  So, what now?
&lt;/h2&gt;

&lt;p&gt;Well, first of all, start with yourself. The title of this article doesn't contain word "journey" just for show. All these issues that I listed, bothered me greatly. The videos I posted at the beginning, launched me on a journey to understand how software works, and the last problem I listed gave me an idea to create something like this myself.&lt;/p&gt;

&lt;p&gt;I already implemented a &lt;a href="https://github.com/DigitalCrafting/letter-rdp" rel="noopener noreferrer"&gt;simple language parser&lt;/a&gt;, &lt;a href="https://github.com/DigitalCrafting/eva-interpreter" rel="noopener noreferrer"&gt;interpreter&lt;/a&gt; and &lt;a href="https://github.com/DigitalCrafting/eva-vm" rel="noopener noreferrer"&gt;VM&lt;/a&gt;. I wrote a disassembler for Assembly 8086 and very simple 8086 CPU simulator, and tried to learn many more things, which was quite a journey. And it's still going.&lt;/p&gt;

&lt;p&gt;What I found out is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It's way easier than you think&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It's just a bit tedious sometimes, and developers hates tedious. But it's also rewarding, by understanding how things work, you will be able to make a better decisions.&lt;/p&gt;

&lt;p&gt;A quick example: &lt;a href="https://github.com/DigitalCrafting/benchmark-adventures/tree/master/src/main/java/org/digitalcrafting/benchmarkadventures/arrayvslinkedlist" rel="noopener noreferrer"&gt;LinkedList vs ArrayList in Java&lt;/a&gt;. Which one to use? Text-book answer would be that LinkedList is better if you add and remove a lot's of elements, however ArrayList is better if you mostly iterate over it. However, that answer &lt;strong&gt;only&lt;/strong&gt; takes into consideration the &lt;strong&gt;Big O notation&lt;/strong&gt; and not what actually happens on the CPU. If you dig a bit deeper, the ArrayList is &lt;em&gt;usually&lt;/em&gt; the best solution, and it's because of how CPU accesses data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance awareness
&lt;/h3&gt;

&lt;p&gt;There are a number of resources you can use to learn about internals of programming, but the one that gave me the most, is this one: &lt;a href="https://www.computerenhance.com/p/table-of-contents" rel="noopener noreferrer"&gt;Performance-Aware Programming Series by Casey Muratori&lt;/a&gt;. It's unfortunately paid, but the money is well worth the content, especially because Casey gives a &lt;strong&gt;very detailed&lt;/strong&gt; explanation of how CPU works and gives excellent examples to get you started.&lt;/p&gt;

&lt;p&gt;I actually learned Assembly 8086 from this course, and it was super easy thanks to Casey's explanations.&lt;/p&gt;

&lt;h2&gt;
  
  
  The articles
&lt;/h2&gt;

&lt;p&gt;I will try to write a number of articles which, hopefully, will provide people with useful insights into how the software works and how to use this knowledge in everyday programming life.&lt;/p&gt;

&lt;p&gt;I will update this section with articles as I go, there will be few different topics, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How programming works top to bottom &lt;/li&gt;
&lt;li&gt;How to implement stuff from scratch (HTTP Server, DB, etc)&lt;/li&gt;
&lt;li&gt;Performance benchmarks and explanations of different structures/programming styles (this one in Java mostly)
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As I am a Java and Angular developer mostly, most of the code will be written in, well, Java and JS :) with some addition of C, C++ and Assembly when needed.&lt;/p&gt;

&lt;p&gt;Also, note that my explanations will contain &lt;strong&gt;many&lt;/strong&gt; simplifications. It's not feasible to write every single detail accurately, and it's not even that useful, since we tend to forget details anyways. I'm aiming this articles at web developers, both backend and frontend, so that they can get a better understanding of what they're actually writing, and maybe think if it's worth it before jumping onto the next new and shiny thing :)&lt;/p&gt;

&lt;p&gt;Happy reading! :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Java Benchmark Adventures
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://dev.to/digitalcrafting/java-benchmark-adventures-arraylist-vs-linkedlist-4oho"&gt;ArrayList vs LinkedList&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Programming from Top to Bottom
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/digitalcrafting/programming-from-top-to-bottom-the-basics-ba"&gt;The Basics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Let's create microservices system from scratch - post mortem</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Tue, 02 Aug 2022 12:08:57 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/lets-create-microservices-system-from-scratch-post-mortem-i15</link>
      <guid>https://dev.to/digitalcrafting/lets-create-microservices-system-from-scratch-post-mortem-i15</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-4-4jl3"&gt;The last part&lt;/a&gt; of this series was posted almost 1.6 year ago and I am way overdue with the post mortem. So, what happened?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I tried to bite more, than I could chew.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's pretty much it. It's not going to be another 10-minute long story about that. Instead, I have 3 simple things I learned:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Start simple&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of going straight to microservices, start with monolithic application first, and then expand it as needed. This would apply well in your professional job as well. &lt;/p&gt;

&lt;p&gt;Since this series died, I actually completed another project doing exactly that - started with monolith, and when the functionality was done, I split and refactored the code.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Do small increments every day&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want to finish a project, don't do marathons on weekends, instead, force yourself to do at least a single commit every day. It doesn't even have to be a big one. I had commits where I just renamed a class or moved it to another package. Focus on one step at a time, and before you know it, you will be done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Focus on a single technology&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My initial idea was to try writing microservices in different languages since this is one of the big selling points of that architecture - you can have as many different languages interacting with each other as you need. While this might be a good idea on paper, in practice it's unmaintainable (this point comes both from this project and my job experience).&lt;/p&gt;

&lt;p&gt;While the languages themselves are not that hard to learn, the frameworks and ecosystems are a nightmare. Each has a different toolset, different philosophy behind them, and different problems they come with. Avoid that as much as you can.&lt;/p&gt;

&lt;p&gt;Additionally, if each team uses different technology, you can't just switch teams if you are bored or exhausted with your project - you have to look for another job. You also don't have anyone, besides your teammates, who can take a look at a problem with a fresh eye and ideas. &lt;/p&gt;




&lt;p&gt;As mentioned before, this failure was not in vain, as I actually created and finished another project, about which I will talk in another set of articles. &lt;/p&gt;

&lt;p&gt;I hope this post will help someone finish their project :)&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>architecture</category>
      <category>microservices</category>
    </item>
    <item>
      <title>Let's create microservices system from scratch - part 4</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Sat, 20 Mar 2021 15:18:26 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-4-4jl3</link>
      <guid>https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-4-4jl3</guid>
      <description>&lt;p&gt;As stated in the &lt;a href="https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-3-42hi"&gt;previous article&lt;/a&gt;, this one will be more of an overview about testing the microservices. As it turns out, this is equally if not more complicated than managing them and took me quite a lot of time to go through various articles and YT videos, but in the end, it boils down to few simple steps.&lt;/p&gt;

&lt;p&gt;First things first, we can just ignore &lt;a href="https://softwaretestingfundamentals.com/unit-testing/" rel="noopener noreferrer"&gt;unit testing&lt;/a&gt; for now since this is done on a single class/method, as well as &lt;a href="https://softwaretestingfundamentals.com/system-testing/" rel="noopener noreferrer"&gt;System/UI testing&lt;/a&gt; since this is usually done on the whole system and there is no debate here - you start the whole system and test it end to end.&lt;/p&gt;

&lt;p&gt;The type of testing that is a bit tricky in microservices is &lt;a href="https://softwaretestingfundamentals.com/integration-testing/" rel="noopener noreferrer"&gt;intergration testing&lt;/a&gt; which should test interactions between the components of the system, in our case - between the services, and that's where the trouble begins.&lt;/p&gt;

&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;The problem is with setting up the testing environment for integration tests. Since we should test only specific interactions there is no need to run everything, especially locally on your own machine. We should just start &lt;strong&gt;some&lt;/strong&gt; of the services - but &lt;strong&gt;which&lt;/strong&gt; of them? How much of the environment do you need to successfully test some features? And in the case of &lt;em&gt;The One&lt;/em&gt; system - how much do you need to run in order to test if &lt;em&gt;API Gateway&lt;/em&gt; works properly?&lt;/p&gt;

&lt;h1&gt;
  
  
  The approach
&lt;/h1&gt;

&lt;p&gt;The step-by-step approach is crucial here - yeah, I know, obvious, but I've seen people doing everything at once more than a few times. So, without further ado, here's what my approach is right now. &lt;/p&gt;

&lt;h2&gt;
  
  
  Ask yourself some questions
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Which feature do we want to test?&lt;/li&gt;
&lt;li&gt;Which services are needed to run?&lt;/li&gt;
&lt;li&gt;Do we need a database to run?&lt;/li&gt;
&lt;li&gt;Which services do we need running and which can be mocked?&lt;/li&gt;
&lt;li&gt;Which database do we need running and which can be mocked? &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are the first questions, you should ask yourself to determine the next steps.&lt;/p&gt;

&lt;p&gt;As an example: in The One project, we want to test, whether the proxying requests work properly. The answers to the above questions will look like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Proxying request to correct service,&lt;/li&gt;
&lt;li&gt;API Gateway, Service Discovery, one of the &lt;em&gt;business logic&lt;/em&gt; services, let's say, User Service,&lt;/li&gt;
&lt;li&gt;No,&lt;/li&gt;
&lt;li&gt;Two-part answer:

&lt;ul&gt;
&lt;li&gt;Need Running: API Gateway, Service Discovery&lt;/li&gt;
&lt;li&gt;Can Be Mocked: User Service&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Not applicable&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Determine the environment you need
&lt;/h2&gt;

&lt;p&gt;So, we need 3 services running, 1 of which can be Mocked or Stubbed. Since at the time of writing this article, the User Service doesn't have any actual logic in it, we could just start it, but, to humor ourselves, let's pretend it already has database connection and complex business logic and we need to somehow mock that. &lt;/p&gt;

&lt;p&gt;The additional questions to ask yourself would be:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Mock or stub the service?&lt;/li&gt;
&lt;li&gt;Mock or stub connection to DB or the whole DB? (ask only if we need DB) &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Stub vs Mock
&lt;/h2&gt;

&lt;p&gt;As for the difference between the Mock and Stub, I find &lt;a href="https://stackoverflow.com/a/3459491" rel="noopener noreferrer"&gt;this StackOverflow answer&lt;/a&gt; very helpful. As I understand it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mock - the response from mock is configured and can vary between runs (like in &lt;a href="https://site.mockito.org/" rel="noopener noreferrer"&gt;Mockito&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Stub - the response is hardcoded and &lt;strong&gt;always&lt;/strong&gt; the same&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Stub
&lt;/h3&gt;

&lt;p&gt;So, as I see it, if you decide to use &lt;strong&gt;Stub&lt;/strong&gt;, you essentially need to create a second service with the same API and hardcode the responses to &lt;strong&gt;always&lt;/strong&gt; return the same data. In a monolithic application, you would just create a second implementation of an interface, in the case of microservices, I would suggest creating a second microservice or adding a profile to the existing one. Each approach will have its own benefits.&lt;/p&gt;

&lt;p&gt;If you create a second service:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it will be lightweight, with no &lt;em&gt;if&lt;/em&gt; statements to determine what to do,&lt;/li&gt;
&lt;li&gt;the original service will be unaffected, no redundant code in production,&lt;/li&gt;
&lt;li&gt;but you will have to support two implementations in case of refactoring.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you just add a profile:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;no need to support two implementations,&lt;/li&gt;
&lt;li&gt;you can have stub implementation right next to the actual one,&lt;/li&gt;
&lt;li&gt;but testing it will be a bit slower since you will have more code to start. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Mock
&lt;/h3&gt;

&lt;p&gt;Mocking will be primarily used in the test file itself, you will basically say:&lt;br&gt;
&lt;code&gt;When I call method X with these parameters, I want that response&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For Java, the most popular library is &lt;a href="https://site.mockito.org/" rel="noopener noreferrer"&gt;Mockito&lt;/a&gt; which is easy to use.&lt;/p&gt;

&lt;p&gt;In the case of our example, if we wanted to test &lt;strong&gt;only&lt;/strong&gt; the proxy, we would mock the call to Discovery Service and check if proxied request points to correct ip, but I think it's pointless to test it that way. I want to know if User Service registers itself with Discovery and will be available through Gateway.&lt;/p&gt;

&lt;h3&gt;
  
  
  Database
&lt;/h3&gt;

&lt;p&gt;As for the database, you can also Mock it, Stub it, use Test Environment Database or provide an instance for the duration of the test only. I would suggest avoiding Test Env DB if you can do that because it is usually used by the QA team and may be unstable.&lt;/p&gt;

&lt;p&gt;The rule of thumb for DB will be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if you need only to get values from db - Mock or Stub the methods called on DB,&lt;/li&gt;
&lt;li&gt;if you need to check how different services transform the data in DB - provide special testing DB if you can, if not, use Testing Env DB&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implement it and run it
&lt;/h2&gt;

&lt;p&gt;The last step is to implement what you decided on. Create testing docker-compose.yml, mock or stub services, provide database if necessary and run the tests. I will do that in the near future, but one idea is to create another Java project which will only contain integration tests.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Testing is &lt;em&gt;huge&lt;/em&gt; topic which adds another layer to already complex topic of running and managing microservices. I hope this overview will be of some help in your projects.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>microservices</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Let's create microservices system from scratch - part 3</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Tue, 16 Feb 2021 16:11:19 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-3-42hi</link>
      <guid>https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-3-42hi</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-2-5eoh"&gt;last article&lt;/a&gt; we decided that the most important thing while developing microservices system is to have a good way of managing and running them. &lt;/p&gt;

&lt;p&gt;After that, the first feature I wanted to implement was to create proxy and service discovery mechanism. How would that work ?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The first to start should be &lt;em&gt;discovery-service&lt;/em&gt; which will hold the list of available services and their location,&lt;/li&gt;
&lt;li&gt;After &lt;em&gt;discovery&lt;/em&gt; is started, all other services should start and register themselves with it using REST Api,&lt;/li&gt;
&lt;li&gt;Requests to the services will go through &lt;em&gt;api-gateway&lt;/em&gt; which will query &lt;em&gt;discovery&lt;/em&gt; for the location of the service and then proxy the requests.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Sounds simple, and the actual implementation is, but there are a few things that needed to be done before I could finish it: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create Rest API standard to follow,&lt;/li&gt;
&lt;li&gt;Control startup order,&lt;/li&gt;
&lt;li&gt;Tell each service where it is,&lt;/li&gt;
&lt;li&gt;Connect services to each other,&lt;/li&gt;
&lt;li&gt;Be able to test it, which is the reason we need to #6,&lt;/li&gt;
&lt;li&gt;Refactor the CLI&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's walk through them.&lt;/p&gt;

&lt;h1&gt;
  
  
  REST API standard
&lt;/h1&gt;

&lt;p&gt;For our proxy to work, it needs to be able to somehow connect the request to a specific service. We should also consider the versioning of our services. What I came up with for &lt;em&gt;root&lt;/em&gt; URI is this:&lt;br&gt;
&lt;code&gt;GET /api/&amp;lt;service-name&amp;gt;/&amp;lt;version&amp;gt;&lt;/code&gt;&lt;br&gt;
In example:&lt;br&gt;
&lt;code&gt;GET /api/user/v1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Which is pretty straightforward, the &lt;em&gt;gateway&lt;/em&gt; can just split request URI by &lt;code&gt;/&lt;/code&gt; and use the second value to determine the target service. &lt;/p&gt;

&lt;p&gt;As for versioning there are actually few methods to do this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;through URI path (that's what I did)&lt;/li&gt;
&lt;li&gt;through header of a request&lt;/li&gt;
&lt;li&gt;through query parameter&lt;/li&gt;
&lt;li&gt;through content negotiation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While #2 is probably &lt;em&gt;more&lt;/em&gt; correct from a RESTful point of view, I feel that #1 is more natural and easier to implement. As for the others, I didn't even consider them as they seem weird to me. &lt;/p&gt;

&lt;p&gt;This is by no means the complete standard, just something to let me finish current feature.&lt;/p&gt;
&lt;h1&gt;
  
  
  Controlling startup order
&lt;/h1&gt;

&lt;p&gt;While this feature is available from &lt;code&gt;docker-compose.yml&lt;/code&gt; configuration it doesn't work as expected because it waits for the &lt;em&gt;container&lt;/em&gt; to start and &lt;strong&gt;not&lt;/strong&gt; the service. &lt;br&gt;
Pretty neat solution is to use &lt;a href="https://www.gnu.org/software/bash/" rel="noopener noreferrer"&gt;bash&lt;/a&gt; script and &lt;a href="https://curl.se/" rel="noopener noreferrer"&gt;curl&lt;/a&gt; to determine whether the &lt;em&gt;discovery-service&lt;/em&gt; is up or not. If yes, then we can start other services.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;discovery-service&lt;/em&gt; URL will be different on &lt;em&gt;localhost&lt;/em&gt; and in &lt;em&gt;docker-compose&lt;/em&gt; so, for now, I created 2 different scripts with different URLs (yeah, I know, I could use one and pass parameters, but the problems were piling up, so I just used what works for now, I will refactor it later) which will be copied to a correct place by CLI when building the project using:&lt;br&gt;
&lt;code&gt;theonecli (local|docker) build&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The script itself is rather simple:&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;serviceUp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;false

&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt; &lt;span class="nv"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0&lt;span class="p"&gt;;&lt;/span&gt; i&amp;lt;10&lt;span class="p"&gt;;&lt;/span&gt; i++ &lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;do
  if&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;curl &lt;span class="nt"&gt;--output&lt;/span&gt; /dev/null &lt;span class="nt"&gt;--silent&lt;/span&gt; &lt;span class="nt"&gt;--head&lt;/span&gt; &lt;span class="nt"&gt;--fail&lt;/span&gt; http://the-one-discovery:8081/discovery/v1/&lt;span class="si"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;then
    &lt;/span&gt;&lt;span class="nv"&gt;serviceUp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true
    break
  &lt;/span&gt;&lt;span class="k"&gt;else
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Service not up - try &lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;fi
  &lt;/span&gt;&lt;span class="nb"&gt;sleep &lt;/span&gt;2
&lt;span class="k"&gt;done

if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$serviceUp&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Service is up - starting command"&lt;/span&gt;
  &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nv"&gt;$@&lt;/span&gt;
&lt;span class="k"&gt;else
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Service is unavailable"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the example usage can be look up, for example in the &lt;a href="https://github.com/DigitalCrafting/microservices-the-one-project/blob/master/python/user-service/Dockerfile" rel="noopener noreferrer"&gt;user service Dockerfile&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Services location
&lt;/h1&gt;

&lt;p&gt;This is actually something, that would be done by administrators. Each service should have it's own IP and PORT, which should be passed to them via &lt;em&gt;config&lt;/em&gt; file. They should also know the location of the &lt;em&gt;discovery-service&lt;/em&gt;, which will be one of the few that have not dynamically assigned IP, but a static one. I did that by extending the &lt;em&gt;config&lt;/em&gt; files from &lt;a href="https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-2-5eoh"&gt;previous article&lt;/a&gt;. &lt;/p&gt;

&lt;h1&gt;
  
  
  Interservice communication
&lt;/h1&gt;

&lt;p&gt;This part is actually the easiest one. On &lt;em&gt;localhost&lt;/em&gt; the services are available, well, on &lt;code&gt;localhost&lt;/code&gt; so the only think we need to do is to assign different PORT numbers.&lt;/p&gt;

&lt;p&gt;When using &lt;code&gt;docker-compose.yml&lt;/code&gt; it's almost as easy - we just just need  create internal network in the config file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;the-one-network&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;driver&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bridge&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and specify this network, as the one, each service belongs to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;the-one-user-service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./python/user-service&lt;/span&gt;
    &lt;span class="na"&gt;container_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;the-one-user-service&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;8083:8083&lt;/span&gt;
    &lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;the-one-network&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Testing the proxy and discovery mechanism
&lt;/h1&gt;

&lt;p&gt;Now, that's where all the hell broke loose when I started developing this feature. As it turns out, testing microservices is the second biggest obstacle right after managing them and there are some solutions to consider but I'll leave that topic for another whole article. Suffice to say, there is no easy way to do this, and the most complete approach would be to start everything you need to and test it then. In my case, I need to start 4 services:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;discovery-service&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;api-gateway&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;user-service&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;outpost-service&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And check if &lt;em&gt;user&lt;/em&gt; and &lt;em&gt;outpost&lt;/em&gt; are correctly registered withing &lt;em&gt;discovery&lt;/em&gt;, and if the &lt;em&gt;api-gateway&lt;/em&gt; correctly proxies the request. That's when I realized that my CLI Tool is not good enough to actually do this and I had to refactor it. After that, I simply run:&lt;br&gt;
&lt;code&gt;curl localhost:8080/api/user/v1&lt;/code&gt;&lt;br&gt;
and checked if the response is what I expected. This is by no means sufficient testing strategy and I will work on that.&lt;/p&gt;

&lt;h1&gt;
  
  
  Refactoring the CLI
&lt;/h1&gt;

&lt;p&gt;First of all, I decided to move &lt;code&gt;docker-compose&lt;/code&gt; functionality to &lt;code&gt;docker&lt;/code&gt; command in the CLI, meaning, when typing:&lt;br&gt;
&lt;code&gt;theonecli docker start&lt;/code&gt;&lt;br&gt;
we will actually start &lt;code&gt;docker-compose.yml&lt;/code&gt; configuration. After killing it, we have to call&lt;br&gt;
&lt;code&gt;theonecli docker stop&lt;/code&gt;&lt;br&gt;
and there is no option to run individual docker images right now. After that, I also had to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fix the build process, so that &lt;em&gt;wait-for-service.sh&lt;/em&gt; script will be copied into correct directory of each serivice,&lt;/li&gt;
&lt;li&gt;change the Dockerfiles so that the services start in correct order&lt;/li&gt;
&lt;li&gt;fix starting multiple services on &lt;em&gt;localhost&lt;/em&gt; by spawning new gnome-terminal for each service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After all that, we end up with 2 levels of configuration files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;project level&lt;/li&gt;
&lt;li&gt;service level&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On &lt;em&gt;project level&lt;/em&gt; we have the &lt;em&gt;wait-for-service.sh&lt;/em&gt; scripts for each environment and on the &lt;em&gt;service level&lt;/em&gt; we have service specific configurations, again, for each of our environments. Right now we only have 2 environments and it will probably not change, since it's only personal project and not a commercial one, but we can already see that the amount of configuration needed for running this project is going to bubble up, especially when we add databases and UI application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;All of that took me waaay to much time to finish, but, I'm learning about these challenges right now 😉&lt;/p&gt;

&lt;p&gt;This article was more of a log of what happened, &lt;a href="https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-4-4jl3"&gt;the next one&lt;/a&gt; will be more theoretical, probably about testing options for microservices. &lt;/p&gt;

&lt;p&gt;I hope this article will be of some help, and will let you avoid some problems with your projects. Current version is available on &lt;a href="https://github.com/DigitalCrafting/microservices-the-one-project" rel="noopener noreferrer"&gt;my github&lt;/a&gt;, feel free to download it and try it out.  &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>microservices</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Let's create microservices system from scratch - part 2</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Mon, 18 Jan 2021 10:02:01 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-2-5eoh</link>
      <guid>https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-2-5eoh</guid>
      <description>&lt;p&gt;We ended my &lt;a href="https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-series-intro-24g4"&gt;previous article&lt;/a&gt; with a basic architecture diagram as well as a tech stack which we want to use and a rough idea for the first milestone we want to achieve.&lt;/p&gt;

&lt;p&gt;Having that, I happily started coding. Created basic REST Apis for few services (just "I'm alive"), dockerized them and even connected &lt;em&gt;Gateway&lt;/em&gt; to &lt;em&gt;Service discovery&lt;/em&gt; using docker-compose which was pretty easy. But that's not what we are going to talk about in this article. We are going to talk about something that I feel is not stressed enough when talking about microservices - &lt;em&gt;how do you manage them&lt;/em&gt;? &lt;/p&gt;

&lt;p&gt;Granted, for some of us developing services, there will be whole another team of DevOps Engineers managing and running everything BUT there is still an issue of testing this thing locally. &lt;/p&gt;

&lt;p&gt;For some reason I thought that simply using Docker will solve all my problems. Boy, was I wrong.&lt;/p&gt;

&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;The problem lies in the number of things you need to do to start even the simplest test case. Suppose we need to start just 2 services connected to each other using Docker, say &lt;code&gt;api-gateway&lt;/code&gt; and &lt;code&gt;user-service&lt;/code&gt;, for each of the services we need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Remove old docker image&lt;/li&gt;
&lt;li&gt;Rebuild the service&lt;/li&gt;
&lt;li&gt;Rebuild the docker image&lt;/li&gt;
&lt;li&gt;Start the docker image&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Which means &lt;strong&gt;8&lt;/strong&gt; steps for &lt;strong&gt;2&lt;/strong&gt; services. It gets worse if you forget to run docker image with &lt;code&gt;--rm&lt;/code&gt; flag set - you will have to clean up the docker containers (unless you force clear everything later). Also, step 1 is required because docker image is rebuilt when it detects the changes to &lt;code&gt;Dockerfile&lt;/code&gt; which will not happen if we only rebuild the app. Even with Docker-compose, the &lt;em&gt;remove old image&lt;/em&gt; still applies since, at least for me, Docker-compose will sometimes not pick up changes. &lt;/p&gt;

&lt;p&gt;With 3 services it becomes unbearable and we start to create some scripts to ease the task. And so, we create the script, and then another, and another... After a while, there comes a problem of running these scripts one by one which basically means we are back to square one, only this time, we run scripts and not individual commands. Then we write scripts to run other scripts... and suddenly! A lightbulb moment! We should create a  &lt;a href="https://en.wikipedia.org/wiki/Command-line_interface" rel="noopener noreferrer"&gt;CLI Tool&lt;/a&gt;! And that's what I did.&lt;/p&gt;

&lt;h1&gt;
  
  
  The solution
&lt;/h1&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Command-line_interface" rel="noopener noreferrer"&gt;CLI Tool&lt;/a&gt; will allow us to run everything from terminal, but we need to take it into account when designing the architecture of each service. In order to do that, we need to back up a little (not too much fortunately, we didn't write that much code yet) and design a proper structure for our services so that we can easily add new ones in the future. &lt;/p&gt;

&lt;p&gt;As for how do we design it, there are few questions we need to answer regarding the CLI:&lt;/p&gt;

&lt;h3&gt;
  
  
  What do we need the CLI to do ?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Rebuild the projects and docker images&lt;/li&gt;
&lt;li&gt;Run the projects and docker images&lt;/li&gt;
&lt;li&gt;Run Docker-compose configurations&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Simple enough. &lt;/p&gt;

&lt;h3&gt;
  
  
  What do we need in order to be able to do that ?
&lt;/h3&gt;

&lt;p&gt;Well, it might depend on your needs but since services can be build and deployed individually by different teams (that's the whole point, really) I came up with following list:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;build&lt;/code&gt; script for each service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;start&lt;/code&gt; script for each service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Dockerfile&lt;/code&gt; for each service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker-compose.yml&lt;/code&gt; files to run different configurations&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What environments are we going for ?
&lt;/h3&gt;

&lt;p&gt;Now, we actually have &lt;strong&gt;2&lt;/strong&gt; environments we want to build for: &lt;strong&gt;local&lt;/strong&gt; and &lt;strong&gt;docker&lt;/strong&gt;, for my project both will be build with &lt;strong&gt;dev&lt;/strong&gt; configuration. To achieve that, I'll use &lt;strong&gt;profiles&lt;/strong&gt; (like &lt;code&gt;maven&lt;/code&gt; profiles):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;dev-local&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dev-docker&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Which will determine what properties are going to be used in our services.&lt;/p&gt;

&lt;h3&gt;
  
  
  Project directory structure
&lt;/h3&gt;

&lt;p&gt;Having decided on that, it's time for the services folder structure. Ultimately, the structure I went for looks like this:&lt;br&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%2Fi%2F65oprtimhwpm9z4qc8ug.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%2Fi%2F65oprtimhwpm9z4qc8ug.png" alt="Alt Text" width="356" height="660"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first level is &lt;em&gt;language directory&lt;/em&gt;. It serves as the &lt;strong&gt;root&lt;/strong&gt; for the projects in that language, thanks to which all python services will use one &lt;em&gt;virtual environment&lt;/em&gt;, java services will have common &lt;code&gt;pom.xml&lt;/code&gt; and so on.&lt;/p&gt;

&lt;p&gt;The second level is the service itself, containing &lt;code&gt;Dockerfile&lt;/code&gt; for each service, since that is the standard.&lt;/p&gt;

&lt;p&gt;The third level are the directories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;src&lt;/em&gt; which contains the source files of the service&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;scripts&lt;/em&gt; which contains &lt;code&gt;build.sh&lt;/code&gt; and &lt;code&gt;start.sh&lt;/code&gt; scripts for each service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each script will handle the profiles on its own, the profile will be passed as a parameter, ex:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;build.sh dev-local&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Having that out of the way...&lt;/p&gt;

&lt;h3&gt;
  
  
  In what language do we write the CLI ?
&lt;/h3&gt;

&lt;p&gt;My first thought was to simply do it in &lt;em&gt;bash&lt;/em&gt; scripting language since it comes by default in linux but, ultimately, I went with mix of &lt;em&gt;bash&lt;/em&gt; and &lt;em&gt;Python&lt;/em&gt; with &lt;code&gt;click&lt;/code&gt; library. &lt;em&gt;Python&lt;/em&gt; also comes with linux nowadays (at least with most popular choices) and it only lacks the &lt;code&gt;click&lt;/code&gt; library, which is easily installed using &lt;code&gt;pip&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Most of the CLI will be written in Python for ease of writing and the simple &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;build&lt;/code&gt; scripts will be written in bash.&lt;/p&gt;

&lt;p&gt;These are the sources I used to write a simple CLI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pymbook.readthedocs.io/en/latest/click.html" rel="noopener noreferrer"&gt;pymbook&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://click.palletsprojects.com/en/7.x/" rel="noopener noreferrer"&gt;click website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;plus occasional google search which usually led to concrete subpage of &lt;a href="https://click.palletsprojects.com/en/7.x/" rel="noopener noreferrer"&gt;click website&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;and of course &lt;em&gt;bash&lt;/em&gt; tutorials&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What the interface will look like ?
&lt;/h3&gt;

&lt;p&gt;It can be done in a various different way, what I settled for, for now, is something similar to &lt;em&gt;docker&lt;/em&gt; commands. I divided the CLI into 3 major commands, each with subcommand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;local (build|start) (--services|-s) SERVICES&lt;/li&gt;
&lt;li&gt;docker (build|start) (--services|-s) SERVICES&lt;/li&gt;
&lt;li&gt;docker-compose

&lt;ul&gt;
&lt;li&gt;start - for now it only runs &lt;code&gt;docker-compose up&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;stop - for now it only run &lt;code&gt;docker-compose down&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;For the version 0.0.1 of the CLI, I left the services config inside the script but I intend to move it to some &lt;code&gt;json&lt;/code&gt; or &lt;code&gt;yml&lt;/code&gt; file in the future. And, of course, the division I did might change in the future.&lt;/p&gt;

&lt;p&gt;The source code for the CLI can be seen &lt;a href="https://github.com/DigitalCrafting/microservices-the-one-project/blob/master/cli/theonecli" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;If you decide to move from monolith to microservices, before you start coding, think how are you going to build and run them, both locally and to production. Thinks of it as the foundation of the whole architecture. Without it, the number of microservices would soon make the development unbearable and the project would crumble.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed this one and that it will help you 😉&lt;br&gt;
The source code for the whole project is on &lt;a href="https://github.com/DigitalCrafting/microservices-the-one-project" rel="noopener noreferrer"&gt;my github&lt;/a&gt;. See you in the &lt;a href="https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-3-42hi"&gt;next part&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>microservices</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Let's create microservices system from scratch - series intro</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Tue, 12 Jan 2021 17:33:13 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-series-intro-24g4</link>
      <guid>https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-series-intro-24g4</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;Well, the title seems like a bold statement, so let's clarify what I mean by &lt;em&gt;microservices system&lt;/em&gt;. I &lt;strong&gt;do not&lt;/strong&gt; mean a framework of any kind to be used in production, only a project to learn about the challenges of the microservices architecture and how it &lt;em&gt;might&lt;/em&gt; work under the hood.&lt;/p&gt;

&lt;p&gt;I do not intend this series to be step by step guide since it would probably be at least 100 articles long by the end. I will, instead, try to write about design choices, how to write some of the mechanisms and highlight some interesting problems (from full stack developer point of view) I stumble upon along the way.  &lt;/p&gt;

&lt;h1&gt;
  
  
  What are we building
&lt;/h1&gt;

&lt;p&gt;First of all, what type of system are we aiming for ? This will determine what kind of services we will need, as well as non-functional requirements of the system.&lt;/p&gt;

&lt;p&gt;Recently I've been playing Middle-earth: Shadow of War and I thought to myself: why not create management application for the Saurons army ? Hence the idea for &lt;em&gt;The One System to Rule Them All&lt;/em&gt; (&lt;em&gt;The One&lt;/em&gt; for short). Then I decided to turn it into a learning experience and so I will not limit myself to Java and Angular which I know, but will also try to learn other languages along the way. &lt;/p&gt;

&lt;h1&gt;
  
  
  Technology stack
&lt;/h1&gt;

&lt;p&gt;The technologies I intend to use and learn at least the basics of, are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For services:

&lt;ul&gt;
&lt;li&gt;Java (&lt;a href="https://www.azul.com/downloads/zulu-community/?package=jdk" rel="noopener noreferrer"&gt;Zulu&lt;/a&gt;) and &lt;a href="https://spring.io/projects/spring-boot" rel="noopener noreferrer"&gt;Spring BOOT&lt;/a&gt; (there is a limit to what I can write from scratch ;))&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.haskell.org/" rel="noopener noreferrer"&gt;Haskell&lt;/a&gt; - because I never used Functional Language before&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.python.org/" rel="noopener noreferrer"&gt;Python&lt;/a&gt; - because it's widely popular&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://golang.org/" rel="noopener noreferrer"&gt;GoLang&lt;/a&gt; - because why not&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;For frontend:

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://angular.io/" rel="noopener noreferrer"&gt;Angular&lt;/a&gt; for the UI&lt;/li&gt;
&lt;li&gt;Java (&lt;a href="https://www.azul.com/downloads/zulu-community/?package=jdk" rel="noopener noreferrer"&gt;Zulu&lt;/a&gt;) and &lt;a href="https://spring.io/projects/spring-boot" rel="noopener noreferrer"&gt;Spring BOOT&lt;/a&gt; for the server side&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;For deployment:

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.docker.com/" rel="noopener noreferrer"&gt;Docker&lt;/a&gt; and Docker-compose&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Architecture pattern:

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://en.wikipedia.org/wiki/Microservices" rel="noopener noreferrer"&gt;Microservices&lt;/a&gt; for the services&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://en.wikipedia.org/wiki/Single-page_application" rel="noopener noreferrer"&gt;SPA&lt;/a&gt; for frontend&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, having decided that for a start, are we going to use libraries such as Netflix Hystrix, Ribbon ? Are we going to use RabbitMQ ? Well, &lt;strong&gt;no&lt;/strong&gt;. Since this is a learning experience, we will try to implement it ourselves. It may not end up being the best code ever, but it will give us overview of how this libraries are implemented which in turn, can help us debug or implement other solutions better in the professional life.&lt;/p&gt;

&lt;h1&gt;
  
  
  The architecture (v0.0.1)
&lt;/h1&gt;

&lt;p&gt;Ok, having more or less decided on the tech stack, we now need to design the actual architecture. &lt;em&gt;The One&lt;/em&gt; is basically a resources management system where the resources range from uruks through the outposts up to the food chains and weapon storages. &lt;/p&gt;

&lt;p&gt;Every management system needs to have some kind of users database to allow access control, authentication and authorization services and UI app to allow user friendly access. But since the chosen architecture is microservices, we cannot simply write one Spring BOOT app and be done with it, we also need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://microservices.io/patterns/apigateway.html" rel="noopener noreferrer"&gt;API Gateway&lt;/a&gt; to connect to different services&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://microservices.io/patterns/server-side-discovery.html" rel="noopener noreferrer"&gt;Service Discovery&lt;/a&gt; to allow us dynamically starting and stopping services&lt;/li&gt;
&lt;li&gt;Logger Service to aggregate logs from every service&lt;/li&gt;
&lt;li&gt;Service monitoring &lt;/li&gt;
&lt;li&gt;And a few other things&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The basic diagram will look like this (I omitted some things in order to make it a little smaller):&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%2Fi%2Ftj2vm1d3aj5h4773ybbj.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%2Fi%2Ftj2vm1d3aj5h4773ybbj.png" alt="Alt Text" width="691" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It may seem like it won't be much work - and normally in a company it probably wouldn't - but we are going to build this from scratch, therefore, the first milestone will be:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create API Gateway Service, that proxies requests to User Service, to address obtained from Service Discovery. &lt;/li&gt;
&lt;li&gt;User Service and API Gateway will wait for Service Discovery startup before starting themselves.&lt;/li&gt;
&lt;li&gt;After startup, User Service will register itself with Service Discovery, passing the IP and PORT on which it's available.&lt;/li&gt;
&lt;li&gt;There will be script to build and run everything either locally or using docker.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;I think the milestone we've set for ourselves is relatively easy but will be a bit challenging to implement from scratch :) See you in the &lt;a href="https://dev.to/digitalcrafting/let-s-create-microservices-system-from-scratch-part-2-5eoh"&gt;next part&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/DigitalCrafting/microservices-the-one-project" rel="noopener noreferrer"&gt;Here's the repo for this project.&lt;/a&gt; Feel free to copy it and tinker around :)&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>architecture</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A case for extending Angular Forms - part 5</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Wed, 18 Nov 2020 13:10:57 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/a-case-for-extending-angular-forms-part-5-393g</link>
      <guid>https://dev.to/digitalcrafting/a-case-for-extending-angular-forms-part-5-393g</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;In my &lt;a href="https://dev.to/digitalcrafting/a-case-for-extending-angular-forms-part-4-1518"&gt;previous article&lt;/a&gt; we changed the &lt;code&gt;reset()&lt;/code&gt; methods of angular forms and added &lt;code&gt;clear()&lt;/code&gt;. This time, we are going to differentiate the user input from the programmer (or code) input.&lt;/p&gt;

&lt;h1&gt;
  
  
  The how
&lt;/h1&gt;

&lt;p&gt;We have actually two options to do this, both of them require adding at least one new method which we will call: &lt;code&gt;setControlValue()&lt;/code&gt; since we will be changing the &lt;code&gt;FormControl&lt;/code&gt; value:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add another method &lt;code&gt;setInputValue()&lt;/code&gt; which will signify that the input has changed&lt;/li&gt;
&lt;li&gt;Use existing &lt;code&gt;setValue()&lt;/code&gt; as the one for the user input&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since the first option is only viable if you, for some reason, separate &lt;em&gt;input&lt;/em&gt; from the &lt;em&gt;forms&lt;/em&gt;, we are going the second path which is also easier to implement.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;setControlValue()&lt;/code&gt; we will enable setting the value &lt;em&gt;even if the control is disabled&lt;/em&gt;, because sometimes it's nice to have. We will also use the &lt;code&gt;setValue()&lt;/code&gt; method to change the internal value &lt;strong&gt;but&lt;/strong&gt; we will not trigger the &lt;code&gt;valueChanges&lt;/code&gt; event unless programmer wants it. Hope this becomes clearer with the implementation.&lt;/p&gt;

&lt;h1&gt;
  
  
  Implementation
&lt;/h1&gt;

&lt;p&gt;Similarly to previous functionality, we first need to declare extra method. We will do that in the &lt;code&gt;AbstractControl&lt;/code&gt; for compatibility with &lt;code&gt;formGroup.get()&lt;/code&gt; method, and in the &lt;code&gt;FormControl&lt;/code&gt; for actual functionality:&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;declare&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@angular/forms&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;AbstractControl&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="nx"&gt;controlValueChanges&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;setControlValue&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;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;emitValueEvent&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;      
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;FormControl&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;AbstractControl&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setControlValue&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;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;emitValueEvent&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&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;In the &lt;code&gt;AbstractControl&lt;/code&gt; we initialize the &lt;code&gt;EventEmitter&lt;/code&gt; and just proxy the &lt;code&gt;setControlValue&lt;/code&gt; arguments to &lt;code&gt;setValue&lt;/code&gt; for compatibility:&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;AbstractControl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;controlValueChanges&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;any&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;AbstractControl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setControlValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &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;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;emitValueEvent&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;}):&lt;/span&gt; &lt;span class="k"&gt;void&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;opt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emitValueEvent&lt;/span&gt;&lt;span class="p"&gt;}&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setValue&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;opt&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;And in the &lt;code&gt;FormControl&lt;/code&gt; we actually implement the desired functionality:&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;FormControl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setControlValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &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;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;emitValueEvent&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;}):&lt;/span&gt; &lt;span class="k"&gt;void&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enable&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="nf"&gt;setValue&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="na"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emitValueEvent&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="nf"&gt;disable&lt;/span&gt;&lt;span class="p"&gt;();&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setValue&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="na"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emitValueEvent&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;options&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;controlValueChanges&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&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;And that's it. We now have an option to override user input even for the disabled control.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;This concludes this series. During just few articles, we have extended the Angular Forms quite a bit. We can show and hide controls from a single place (and also do not consider hidden controls for validation), we can reset the control to default value or clear it completely, we can determine whether the value change came from user input or from our internal logic. And it wasn't even that hard when using &lt;code&gt;prototype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Obviously there might be some corner cases which I forgot about and it needs &lt;strong&gt;a lot&lt;/strong&gt; more testing, especially automated, but otherwise, it works. &lt;/p&gt;

&lt;p&gt;Hope this series will be of use to you and that you learned something new along the way :)&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
    </item>
    <item>
      <title>A case for extending Angular Forms - part 4</title>
      <dc:creator>DigitalCrafting</dc:creator>
      <pubDate>Mon, 09 Nov 2020 12:36:12 +0000</pubDate>
      <link>https://dev.to/digitalcrafting/a-case-for-extending-angular-forms-part-4-1518</link>
      <guid>https://dev.to/digitalcrafting/a-case-for-extending-angular-forms-part-4-1518</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://dev.to/digitalcrafting/a-case-for-extending-angular-forms-part-3-1c3l"&gt;Previously&lt;/a&gt; we added &lt;code&gt;visibility&lt;/code&gt; to AbstractControl, but, we are missing one crucial part - treating hidden control as &lt;code&gt;VALID&lt;/code&gt;. After that we will add another missing functionality: &lt;code&gt;defaultValue&lt;/code&gt; and &lt;code&gt;clear()&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;As always, full code examples are on &lt;a href="https://github.com/DigitalCrafting/dev-to-examples/blob/main/angular/projects/core/forms/dc-forms.ts" rel="noopener noreferrer"&gt;my github.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What I also forgotten before is that in order for our application to apply all this changes, we must import our file in &lt;code&gt;app.component&lt;/code&gt; file as such:&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../../../core/forms/dc-forms&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;h1&gt;
  
  
  Handling hidden control
&lt;/h1&gt;

&lt;p&gt;As it turns out, this part is the easiest one by far. We simply need to &lt;code&gt;if statement&lt;/code&gt; in &lt;code&gt;updateValueAndValidity&lt;/code&gt; 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;AbstractControl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;updateValueAndValidity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;onlySelf&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&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="k"&gt;void&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;visible&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;false&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;this&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;VALID&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="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &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;enabled&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="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;&lt;strong&gt;Note&lt;/strong&gt; that we add the &lt;code&gt;if (this.enabled)&lt;/code&gt; as an &lt;code&gt;else&lt;/code&gt; to our if because there is no need to run any validators on a hidden control. &lt;/p&gt;

&lt;h1&gt;
  
  
  Default value
&lt;/h1&gt;

&lt;p&gt;When we first create &lt;code&gt;FormControl&lt;/code&gt; we can pass options to contructor, either as a initial value or value and other parameters. So why isn't this value treated as default when we reset the control? This only makes sense in the &lt;code&gt;FormControl&lt;/code&gt; so we will not do it &lt;code&gt;FormGroup&lt;/code&gt; and &lt;code&gt;FormArray&lt;/code&gt; (but of course you can).&lt;/p&gt;

&lt;p&gt;First, we declare another interface in our augmented module:&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;declare&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@angular/forms&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;FormControl&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;AbstractControl&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;defaultValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&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;And then, override the &lt;code&gt;_applyValue&lt;/code&gt; method, adding another line just below the visibility handling from &lt;a href="https://dev.to/digitalcrafting/a-case-for-extending-angular-forms-part-3-1c3l"&gt;previous article:&lt;/a&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="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;defaultValue&lt;/span&gt;&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;defaultValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formState&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it for storing the defaultValue. Now we can enhance &lt;code&gt;reset()&lt;/code&gt; and &lt;code&gt;clear()&lt;/code&gt; methods.&lt;/p&gt;

&lt;h1&gt;
  
  
  clear() and reset()
&lt;/h1&gt;

&lt;p&gt;For compatibility, clear and reset will be declared both in &lt;code&gt;AbstractControl&lt;/code&gt; and in &lt;code&gt;FormControl&lt;/code&gt; interfaces:&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;AbstractControl&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="nf"&gt;clear&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;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// @ts-ignore&lt;/span&gt;
    &lt;span class="nf"&gt;reset&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;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;FormControl&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;AbstractControl&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;defaultValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// @ts-ignore&lt;/span&gt;
    &lt;span class="nf"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Object&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 reason for different signature is that, as mentioned before, &lt;code&gt;defaultValue&lt;/code&gt; is mainly useful in &lt;code&gt;FormControl&lt;/code&gt;. For compatibility's sake the &lt;code&gt;AbstractControl.clear()&lt;/code&gt; method will be declared as follows:&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;AbstractControl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clear&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &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;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reset&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;options&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, for the actual functionality, we implement the &lt;code&gt;FormControl&lt;/code&gt; methods:&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;FormControl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_applyValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&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;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_applyFormState&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;markAsPristine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&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="nf"&gt;markAsUntouched&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&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="nf"&gt;setValue&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;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&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;_pendingChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;FormControl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_applyValue&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;defaultValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;FormControl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clear&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_applyValue&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="nx"&gt;options&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;code&gt;_applyValue()&lt;/code&gt; method is basically what &lt;code&gt;reset()&lt;/code&gt; looked liked before, so in terms of Vanilla Angular: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;our new reset() method sets the &lt;code&gt;FormControl&lt;/code&gt; value to &lt;code&gt;defaultValue&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;our clear() method sets the &lt;code&gt;FormControl&lt;/code&gt; value to null&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What else ?
&lt;/h1&gt;

&lt;p&gt;Now we can enhance our visibility functionality by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;calling &lt;code&gt;clear()&lt;/code&gt; when we hide our control&lt;/li&gt;
&lt;li&gt;calling &lt;code&gt;reset()&lt;/code&gt; when we show our control
&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="nx"&gt;declare&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@angular/forms&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;FormGroup&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;AbstractControl&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;reset&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;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onlySelf&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;}):&lt;/span&gt; &lt;span class="k"&gt;void&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;AbstractControl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hide&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;visible&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;this&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;visible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;visible&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&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;visibilityChanges&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&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;visible&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="nf"&gt;clear&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="nf"&gt;updateValueAndValidity&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;AbstractControl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;show&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &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="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;visible&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;this&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;visible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;visible&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;visibilityChanges&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&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;visible&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="nf"&gt;reset&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="nf"&gt;updateValueAndValidity&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;One other thing we can do is to override the &lt;code&gt;FormGroup.reset()&lt;/code&gt; method to account for different signature of &lt;code&gt;reset()&lt;/code&gt; and &lt;code&gt;clear()&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;FormGroup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &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;any&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onlySelf&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&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="k"&gt;void&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="nf"&gt;_forEachChild&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="na"&gt;control&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AbstractControl&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;string&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;defaultValue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;control&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;control&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;FormControl&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;);&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="nx"&gt;control&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reset&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;name&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;onlySelf&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="na"&gt;emitEvent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emitEvent&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Doesn't this look more natural in terms of usability ? We clear control when hiding it, and restore default when we show it again. Also, the hidden control is treated as &lt;code&gt;VALID&lt;/code&gt;, hence not iterfering in the FormGroup as a whole.&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/digitalcrafting/a-case-for-extending-angular-forms-part-5-393g"&gt;next article&lt;/a&gt;, we will tackle the last shortcoming of Angular Forms: lack of differentiation between user and programmer input.&lt;/p&gt;

&lt;p&gt;Hope you found this article usefull :)&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
