<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Valentino Pellegrino</title>
    <description>The latest articles on Forem by Valentino Pellegrino (@vpellegrino).</description>
    <link>https://forem.com/vpellegrino</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%2F131552%2F78b81f94-e615-4706-8d8e-68347b1dc448.jpg</url>
      <title>Forem: Valentino Pellegrino</title>
      <link>https://forem.com/vpellegrino</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vpellegrino"/>
    <language>en</language>
    <item>
      <title>Regular Expressions made easy: a declarative approach</title>
      <dc:creator>Valentino Pellegrino</dc:creator>
      <pubDate>Mon, 06 Apr 2020 10:12:20 +0000</pubDate>
      <link>https://forem.com/vpellegrino/regular-expressions-made-easy-a-declarative-approach-39og</link>
      <guid>https://forem.com/vpellegrino/regular-expressions-made-easy-a-declarative-approach-39og</guid>
      <description>&lt;p&gt;Be honest: every time you find a regular expression in the code, you start wondering if you can avoid to change it, or maybe if a colleague can help you to understand it.&lt;br&gt;
How many seconds do you need to understand that &lt;br&gt;
&lt;code&gt;&amp;lt;(\[A-Z\][A-Z0-9]*)\b[^&amp;gt;]*&amp;gt;(.*?)&amp;lt;/\1&amp;gt;&lt;/code&gt; &lt;br&gt;
is a regex to grab HTML tags?&lt;br&gt;
If you are searching for a smart way to write and maintain a regular expression, relax and continue reading.&lt;/p&gt;

&lt;h1&gt;
  
  
  First of all - What is a Regular Expression?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WeNW9tT3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgs.xkcd.com/comics/regular_expressions.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WeNW9tT3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgs.xkcd.com/comics/regular_expressions.png" alt="I know Regular Expressions - XKCD comic" width="600" height="607"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;“A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as &lt;em&gt;.txt to find all text files in a file manager. The regex equivalent is `^.&lt;/em&gt;.txt$`" - &lt;a href="https://www.regular-expressions.info/"&gt;https://www.regular-expressions.info/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are a lot of use cases where regular expressions fit well:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want to analyze command lines.&lt;/li&gt;
&lt;li&gt;In general, you want to parse user input.&lt;/li&gt;
&lt;li&gt;An huge text file: let’s parse it to find some useful stuff (e.g. specific logged errors).&lt;/li&gt;
&lt;li&gt;Pattern matching (e.g. you want a password to follow a specific format).&lt;/li&gt;
&lt;li&gt;Replace a repeating sub-string in a characters sequence.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order to use regex, you have to &lt;em&gt;understand&lt;/em&gt; and remember a lot of symbols and methods:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5lNAFWa5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://www.ks.uiuc.edu/Research/vmd/vmd-1.2/ug/vmdug_img50.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5lNAFWa5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://www.ks.uiuc.edu/Research/vmd/vmd-1.2/ug/vmdug_img50.gif" alt="Regular expression methods - www.ks.uiuc.edu" width="664" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Regular Expressions are used so much?
&lt;/h2&gt;

&lt;p&gt;The reason why regex are widely used is for their &lt;strong&gt;performance&lt;/strong&gt;. The more precise your regex is, the less likely you are to accidentally match text that you didn’t mean to match.&lt;br&gt;
Regex are really fast when they are accurate. Good regular expressions are often longer than bad regular expressions because they make use of specific characters/character classes and have more structure. This causes good regular expressions to run faster as they predict their input more accurately.&lt;/p&gt;

&lt;h1&gt;
  
  
  VerbalExpressions
&lt;/h1&gt;

&lt;p&gt;VerbalExpressions is a set of libraries that represents an easy way to write readable regex. It can ease the pain of regex, and actually make writing expressions fun again.&lt;br&gt;
VerbalExpressions has been ported to so many other languages that a GitHub organization (&lt;a href="https://github.com/VerbalExpressions"&gt;https://github.com/VerbalExpressions&lt;/a&gt;) was created just to host them all.&lt;br&gt;
Obviously, there is also an implementation of such library for JavaScript (&lt;a href="https://github.com/VerbalExpressions/JSVerbalExpressions"&gt;https://github.com/VerbalExpressions/JSVerbalExpressions&lt;/a&gt;).&lt;br&gt;
Given a complex regex that checks for valid URL &lt;code&gt;/^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$/&lt;/code&gt;&lt;br&gt;
Let’s see how it easy to write it, using such library:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const urlTester = VerEx()
    .startOfLine()
    .then('http')
    .maybe('s')
    .then('://')
    .maybe('www.')
    .anythingBut(' ')
    .endOfLine();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  How to use it
&lt;/h2&gt;

&lt;p&gt;There are several ways to use such library:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can download it and import using a standard script tag:
&lt;/li&gt;
&lt;li&gt;You can use a Content Delivery Network, like &lt;a href="http://www.jsdelivr.com/projects/jsverbalexpressions"&gt;http://www.jsdelivr.com/projects/jsverbalexpressions&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;You can install it, using NPM and use it in any Node based application:
npm install verbal-expressions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also use it &lt;em&gt;live&lt;/em&gt; on the site &lt;a href="https://verbalregex.com/"&gt;https://verbalregex.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--__0f-8Wu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://paper-attachments.dropbox.com/s_F51036B7A3A069B839553CACEAB8787CDED38A695EA1857B371E56BC7F38DFCF_1578509423788_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--__0f-8Wu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://paper-attachments.dropbox.com/s_F51036B7A3A069B839553CACEAB8787CDED38A695EA1857B371E56BC7F38DFCF_1578509423788_image.png" alt="" width="880" height="432"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Chatbot Expenses - Simple bot for collecting expenses typed in the terminal
&lt;/h1&gt;

&lt;p&gt;In that example (&lt;a href="https://github.com/vpellegrino/chatbot-expenses"&gt;https://github.com/vpellegrino/chatbot-expenses&lt;/a&gt;), I show how to build complex parsing features, used by a simple NodeJS application, with a prompt interface, used to collect and report expenses from a group of users.&lt;br&gt;
Imagine you want to offer a list of commands, like the ones defined below.&lt;br&gt;
&lt;strong&gt;Store expense&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;EXPENSE&amp;gt;=&amp;lt;PARTICIPANT&amp;gt;\[,&amp;lt;PARTICIPANT&amp;gt;...\][ "&amp;lt;MESSAGE&amp;gt;"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For each participant, you can also specify a different split for the costs, by using the modifiers &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt;.&lt;br&gt;
Examples:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;84.20=MR,VP+0.20 "Pizza"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This means, VP has paid 84.20 USD for a pizza, where 42.00 USD is in charge of MR.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MR&amp;gt; 20=VP "Hamburger"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In that example, MR has paid 20 USD for an Hamburger eat up by VP.&lt;br&gt;
&lt;strong&gt;Retrieve the list of expenses&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HISTORY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Retrieve the group balance&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BALANCE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is the most important command, since behind the scenes an algorithm similar to &lt;a href="http://en.wikipedia.org/wiki/Bin_packing_problem"&gt;Bin Packing&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Partition_problem"&gt;Partition Problem&lt;/a&gt; is used. The goal is to print the minimal set of transaction in order to pay all debts inside the group.&lt;br&gt;
Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alice -&amp;gt; Bill $10
Bill -&amp;gt; Alice $1
Bill -&amp;gt; Charles $5
Charles -&amp;gt; Alice $5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Solution would be:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alice = $4 Bill = $-4 Charles = $0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Declarative Regular Expressions
&lt;/h2&gt;

&lt;p&gt;The service that is responsible for providing all checks for well-formed commands and for grabbing user input is &lt;code&gt;src/services/regExpService.js&lt;/code&gt;.&lt;br&gt;
A series of constants (that can be reused in other complex expressions) have been defined. For instance:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const twoLetters = new VerbalExpression()
                  .then(new VerbalExpression().range('A', 'Z').repeatPrevious(2));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The combination of such constants get assembled in more complex functions, that are still easy to read (or at least, easiest than the related regex).&lt;br&gt;
For example, given a line of text, the function below is able to return an array containing two elements: the sender initials, and the message he sent:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function parseSenderInitialsAndText(line) {
    return new VerbalExpression()
        .startOfLine()
        .beginCapture().then(twoLetters).endCapture().then(ARROW).maybe(WHITESPACE)
        .beginCapture().then(new VerbalExpression().anything()).endCapture()
        .endOfLine().exec(line);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It is quite easy to switch from standard regex to VerbalExpression() and viceversa. So, it is definitely easy to combine them when you don’t know exactly how a specific regex works, but you still need to extend it.&lt;/p&gt;

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

&lt;p&gt;Regular Expressions are mathematically sound and fast. But they &lt;em&gt;suck 😁&lt;/em&gt; really hard in terms of ease of use and maintainability. &lt;br&gt;
So, for good performance, we need longer regular expressions. 😮&lt;br&gt;
But, for good maintainability, we need shorter regular expressions. 🤔&lt;br&gt;
VerbalExpressions represent a good solution 😎, that enables you to use regex, without the pain of maintain them. With a declarative approach, you can simply write your statement, describing the way you expect to check or grab a certain character/group of characters.&lt;/p&gt;

</description>
      <category>regex</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Web Components: a Frameworkless solution</title>
      <dc:creator>Valentino Pellegrino</dc:creator>
      <pubDate>Fri, 06 Dec 2019 11:11:27 +0000</pubDate>
      <link>https://forem.com/vpellegrino/web-components-a-frameworkless-solution-11ch</link>
      <guid>https://forem.com/vpellegrino/web-components-a-frameworkless-solution-11ch</guid>
      <description>&lt;p&gt;Whenever you adopt a framework, you must take in account that you are locking your project into an already defined road-map, that probably has been defined by a third party company/development team.&lt;br&gt;
Frameworks can “die”. The community could move to another technology or a new tool. But things can get worse: a security issue gets found on a framework that is no more maintained.&lt;br&gt;
Adopting a framework, you are also adopting a &lt;strong&gt;risk&lt;/strong&gt;: your project is going to be legacy sooner or later. This is disruptive, especially in an enterprise scenario.&lt;/p&gt;

&lt;h1&gt;
  
  
  Frameworkless Movement
&lt;/h1&gt;

&lt;p&gt;“The &lt;strong&gt;Frameworkless Movement&lt;/strong&gt; is a group of developers interested in developing applications without frameworks. &lt;strong&gt;We don't hate frameworks&lt;/strong&gt;, nor we will ever create campaigns against frameworks, but we perceive the &lt;strong&gt;misuse of frameworks as a lack of knowledge regarding technical debt&lt;/strong&gt; and the availability of alternatives given by the vanilla language or by dedicated libraries.” - &lt;a href="http://frameworklessmovement.org"&gt;http://frameworklessmovement.org&lt;/a&gt;&lt;br&gt;
This movement does not think that frameworks are evil. &lt;br&gt;
Let’s start from a simple principle: if you are able to code without a framework, you are also able to decide whenever to use or not to use them. This decision is based on the knowledge of strengths and weaknesses of the framework itself.&lt;br&gt;
Have you ever asked how a particular framework acts behind the scenes? For example, how IoC (Inversion of Control), in the popular Spring framework, works?&lt;br&gt;
Have you ever tried to build your home-made i18n service?&lt;br&gt;
Do I really need to adopt Angular framework, just because I need client-side routing in my Single Page Application?&lt;br&gt;
If I adopt VueJS, just because it is a growing trend, are my colleagues ready to leverage it?&lt;br&gt;
When this kind of questions come to mind, you are starting to think of frameworks in a critical way.&lt;/p&gt;

&lt;h1&gt;
  
  
  Web Components - a brief introduction
&lt;/h1&gt;

&lt;p&gt;The goal of this section is not to give you a full overview about Web Components. There are a lot of resources in the web that you can rely on. We just need some basic concepts to understand the next proof of concept. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Web Component?
&lt;/h2&gt;

&lt;p&gt;“Web components are a set of web platform APIs that allow you to create new custom, reusable, encapsulated HTML tags to use in web pages and web apps. Custom components and widgets build on the Web Component standards, will work across modern browsers, and can be used with any JavaScript library or framework that works with HTML.” - &lt;a href="https://www.webcomponents.org"&gt;https://www.webcomponents.org&lt;/a&gt;&lt;br&gt;
Building blocks of Web Components are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Custom Elements:&lt;/strong&gt; custom defined types of DOM elements (you can create a new HTML tag).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shadow DOM:&lt;/strong&gt; encapsulation of style and markup in a Web Component (you have an isolated DOM structure).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ES Modules:&lt;/strong&gt; reusable JS modules (you don’t have to use third party libraries such as SystemJS or requireJS).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTML Template:&lt;/strong&gt; fragment of markup that could be dynamically loaded (you can exploit a lazy loading strategy).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How can I use a Web Component?
&lt;/h2&gt;

&lt;p&gt;There are several ready-to-use components, provided for example by the Polymer library (&lt;a href="https://www.polymer-project.org/"&gt;https://www.polymer-project.org/&lt;/a&gt;).&lt;br&gt;
The good news is that you don’t need to adopt an entire framework, to use a particular component.&lt;br&gt;
For example, if I want to use a slider that follows Material Design (&lt;a href="https://material.io/design/components/sliders.html"&gt;https://material.io/design/components/sliders.html&lt;/a&gt;), I just need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install it: &lt;code&gt;npm install @material/mwc-slider&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Include in my page:&lt;br&gt;
    &lt;br&gt;
      import '@material/mwc-slider';&lt;br&gt;
    &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;mwc-slider value="25" min="10" max="50"&amp;gt;&amp;lt;/mwc-slider&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Et voilà:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6mdumSLt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/material-components/material-components-web-components/raw/master/packages/slider/images/basic.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6mdumSLt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/material-components/material-components-web-components/raw/master/packages/slider/images/basic.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Can I customize a ready-to-use Web Component?
&lt;/h2&gt;

&lt;p&gt;If such Web Component is well designed, the answer is definitely &lt;strong&gt;yes&lt;/strong&gt;.&lt;br&gt;
For instance, if you don’t like the style, or you need to listen to the change event of the slider defined above, you just need to look at its APIs (&lt;a href="https://github.com/material-components/material-components-web-components/tree/master/packages/slider"&gt;https://github.com/material-components/material-components-web-components/tree/master/packages/slider&lt;/a&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok, but the Web Component in my mind does not exist.
&lt;/h2&gt;

&lt;p&gt;No issue with that: you can just build your custom Web Component. Look at next section!&lt;/p&gt;

&lt;h1&gt;
  
  
  Parking Widget - A Frameworkless, custom and reusable HTML element
&lt;/h1&gt;

&lt;p&gt;In scenarios where I need a Web Component that is highly customizable, and near to my business requirements, you can also define a coarse grain component.&lt;br&gt;
In that proof of concept (&lt;a href="https://github.com/vpellegrino/parking-widget"&gt;https://github.com/vpellegrino/parking-widget&lt;/a&gt;) I show how to realize an interesting UI widget, without using any framework (VanillaJS).&lt;br&gt;
The requirement was to realize a widget to show a collection of parking slots and let the user buy one. Such widget should be easily embedded in any Web application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Technical Details
&lt;/h2&gt;

&lt;p&gt;The Parking widget is a &lt;strong&gt;simple&lt;/strong&gt;, &lt;strong&gt;light&lt;/strong&gt; and &lt;strong&gt;framework-less&lt;/strong&gt; web-component (HTML &lt;code&gt;CustomElement&lt;/code&gt;). &lt;br&gt;
It can be added to the DOM:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;parking-widget id="my-parking-widget"&amp;gt;&amp;lt;/parking-widget&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Initialization is simple, via two properties:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;model&lt;/code&gt; is the JSON definition (e.g. &lt;code&gt;src/assets/model.json&lt;/code&gt;), used to fill and render all widget dynamic areas.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;onSelectionCallback&lt;/code&gt; is the function provided to the widget that will be called each time a parking slot selection takes place. That function is expecting one argument: the object related to the selected parking slot.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Communication mechanism&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Can be represented as below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://camo.githubusercontent.com/6b6c655fe4bae9b639d68ecd97608891d224a5c0/68747470733a2f2f692e696d6775722e636f6d2f6879356a345a672e706e67" class="article-body-image-wrapper"&gt;&lt;img src="https://camo.githubusercontent.com/6b6c655fe4bae9b639d68ecd97608891d224a5c0/68747470733a2f2f692e696d6775722e636f6d2f6879356a345a672e706e67" alt="Parking-widget: Communication mechanism"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;In a well designed architecture, business logic should be separated by the specific project configuration or framework usage.&lt;br&gt;
How many times you, as developer, are making estimations based on a particular framework? It does not make sense! &lt;br&gt;
Functional requirements like budget, usability, domain-specific constraints should guide architectural choice and hence estimations.&lt;br&gt;
And remember: &lt;strong&gt;you don't need a framework to build a good web application&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>webcomponents</category>
      <category>frameworkless</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Adopting Angular (2+) framework</title>
      <dc:creator>Valentino Pellegrino</dc:creator>
      <pubDate>Fri, 06 Dec 2019 10:58:46 +0000</pubDate>
      <link>https://forem.com/vpellegrino/adopting-angular-2-framework-3l44</link>
      <guid>https://forem.com/vpellegrino/adopting-angular-2-framework-3l44</guid>
      <description>&lt;p&gt;As you may know, AngularJS framework is no longer supported. Creating new pages using that framework is discouraged.&lt;br&gt;
How can we handle existing pages, written with older framework? How can we handle new pages?&lt;br&gt;
There are different strategies that can be used to obtain this goal (at least partially)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Re-coding from scratch the whole AngularJS application.&lt;/li&gt;
&lt;li&gt;Bootstrap an hybrid application that has both Angular and AngularJS frameworks.&lt;/li&gt;
&lt;li&gt;Keep the older AngularJS as is, freezing it from new-feature-development, and booting an Angular app to be kept side-by-side with the older one, by using the micro frontends paradigma.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Re-coding from scratch the whole AngularJS application&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;PROS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easier maintenance: only one framework (Angular).&lt;/li&gt;
&lt;li&gt;Performance improvement for "legacy" part.&lt;/li&gt;
&lt;li&gt;While rewriting the legacy part, you have the possibility to restructure it in a more maintainable way and to write tests for uncovered components/service.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;CONS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A lot of time will be needed. No incremental value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Hybrid Application (ngUpgrade)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When you need a new library or external capabilities, most libraries (Angular wrapper etc.) are now only targeting Angular 2+. Angular offers a series of built-in tools for efficiently migrating AngularJS projects over to the Angular platform, a piece at a time.&lt;br&gt;
One of the keys to a successful upgrade is to do it incrementally, by running the two frameworks side by side in the same application, and porting AngularJS components to Angular one by one. This makes it possible to upgrade even large and complex applications without disrupting other business, because the work can be done collaboratively and spread over a period of time. The upgrade module in Angular has been designed to make incremental upgrading seamless. A detailed guide is on &lt;a href="https://angular.io/guide/upgrade"&gt;https://angular.io/guide/upgrade&lt;/a&gt;.&lt;br&gt;
Built-in APIs offered by Angular are described here: &lt;a href="https://angular.io/api/upgrade"&gt;https://angular.io/api/upgrade&lt;/a&gt;. They supports the upgrade path from AngularJS to Angular, allowing components from both systems to be used in the same application.&lt;br&gt;
Migration steps&lt;br&gt;
There are different migration strategies, but the most preferred one is to run side by side with Angular being the main application.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create Angular application using Angular CLI&lt;/li&gt;
&lt;li&gt;Copy an existing AngularJS app (should be 1.6.x and refactored so that the structure of the code is feature based and each file has a single implementation)&lt;/li&gt;
&lt;li&gt;Migrate JS dependencies (Switch to npm)&lt;/li&gt;
&lt;li&gt;Fix template paths for the legacy AngularJS app, in both JS and HTML&lt;/li&gt;
&lt;li&gt;Migrate css files and fix import paths&lt;/li&gt;
&lt;li&gt;Fix minor CSS issues caused by an introduction of view-container and view-frame&lt;/li&gt;
&lt;li&gt;Regression test (It seems all working at this stage, have a good test by QA)&lt;/li&gt;
&lt;li&gt;Add Angular Routing and introduce a new page&lt;/li&gt;
&lt;li&gt;Switch between AngularJS page and Angular page&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;PROS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Upgrade can be gradual, even if difficult&lt;/li&gt;
&lt;li&gt;Low regression risk&lt;/li&gt;
&lt;li&gt;Using the upgrade/downgrade Angular functionality, it's possible to use legacy components/services inside Angular app&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;CONS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Fast" start but slowness on the mid-long term&lt;/li&gt;
&lt;li&gt;Graphical issues: "legacy" app uses AngularJS and Bootstrap 3. We cannot have on the same page Bootstrap 3 and 4&lt;/li&gt;
&lt;li&gt;Initial loading time of the application could represent a performance issue&lt;/li&gt;
&lt;li&gt;Build issue - built-in ng build from angular CLI does not understand that we are including AngularJS&lt;/li&gt;
&lt;li&gt;The application becomes significantly larger compared to the previous release (4 times larger than AngularJS app)&lt;/li&gt;
&lt;li&gt;Need to apply upgrade/downgrade on existing AngularJS components/services&lt;/li&gt;
&lt;li&gt;If you use UI bootstrap on AngularJS, it seems there is no simple way to have side by side two different versions of Bootstrap&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Micro-frontends&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The idea behind Micro Frontends is to think about a website or web app as a composition of features which are owned by independent teams. Each team has a distinct area of business or mission it cares about and specialises in. A team is cross functional and develops its features end-to-end, from database to user interface.&lt;br&gt;
Something often not mentioned in discussions is that micro-frontends can be split horizontally or vertically.&lt;br&gt;
Horizontally would mean the case described here: Multiple independent apps on one view. I really don't want to see the resulting bundle-size for this approach.&lt;br&gt;
Vertical splitting means you have one app for one view, and separate e.g. by subdomain where &lt;a href="http://login.app.com/"&gt;login.app.com&lt;/a&gt;, &lt;a href="http://news.app.com/"&gt;news.app.com&lt;/a&gt;, &lt;a href="http://editor.app.com/"&gt;editor.app.com&lt;/a&gt; each have their own app. DAZN works like this, for example.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The tool&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Single SPA is a JavaScript framework for frontend microservices. It enables you to use multiple frameworks in a single-page application, allowing the code split by functionality and have different frameworks (Angular, React, Vue, etc.) all living together.&lt;br&gt;
There is a recommended setup, to boost code performance and bundle size. You also have the possibility to interactively configure the Single SPA entry point, with a playground. A bunch of tutorials and coding examples are available here.&lt;br&gt;
Most interesting features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use of multiple frameworks on the same page without refreshing the page,&lt;/li&gt;
&lt;li&gt;lazy load code for improved initial load-time,&lt;/li&gt;
&lt;li&gt;top-level routing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a Proof of Concept about utilization on the same single page application of multiple (separated) frontend application: &lt;a href="https://github.com/Pragmatists/microfrontends"&gt;https://github.com/Pragmatists/microfrontends&lt;/a&gt;&lt;br&gt;
In that POC there is also an example of applications communication, performed by using global events, where data exchange is supported. Also other sharing mechanism are possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PROS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setup of the project should be painless, with the exception of all the initial boilerplate setup&lt;/li&gt;
&lt;li&gt;For the micro-frontend solutions, Single SPA seems like the most mature framework&lt;/li&gt;
&lt;li&gt;There is the possibility to have CSS stylesheets independence by automatically wrapping CSS rules in a namespace dependent on application name (look at &lt;a href="https://github.com/ruslansavenok/postcss-wrap"&gt;https://github.com/ruslansavenok/postcss-wrap&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Each SPA app can have its own repository and different teams can work w/o conflicts&lt;/li&gt;
&lt;li&gt;Open Points&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;CONS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Should we deploy applications as independent frontend or bundle everything together? In the first case we need to expose them, for example, in different ports. In the second case, if one - of such application changes, you have to rebuild all together.&lt;/li&gt;
&lt;li&gt;Take care also of file system total size for both solutions, as well as total size of downloaded resources by the user (via the browser)&lt;/li&gt;
&lt;li&gt;In the single bundle approach, is it possible to have conflicting libraries side by side? (e.g. bootstrap 3 and 4)&lt;/li&gt;
&lt;li&gt;How to manage common dependencies?&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Working on legacy AngularJS projects, you will soon start to elaborate a migration strategy to a modern framework.&lt;br&gt;
If you are thinking to switch to Angular, React or VueJS, there are different strategies you can follow, depending on both technical and business requirements.&lt;br&gt;
There is an optimum way, that is writing from scratch the entire application. It can be very time-wasting and I am sure that your boss will not easily give you so many time to rewrite something that more or less works, w/o adding any customer value.&lt;br&gt;
Angular’s Upgrade APIs represents a good compromise, that can be leveraged only if you are thinking to migrate to Angular.&lt;br&gt;
The Micro Frontends strategy allow you to create the right abstraction between a specific application implementation, which is highly coupled with a framework, and its functionalities that should be exposed with the outside world. The community, overall, is going in that direction.&lt;br&gt;
The last suggestion that I want to give you is: while you are migrating an application or you are adopting an hybrid approach, why don’t you encapsulate your components/services with Web Component standard? Next time that the framework will change, it will be more easy to perform an upgrade.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>frontend</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
