<?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: Anton</title>
    <description>The latest articles on DEV Community by Anton (@antmik).</description>
    <link>https://dev.to/antmik</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%2F594331%2F8ecacecd-1a62-48bf-bc32-5ab7928bb640.jpeg</url>
      <title>DEV Community: Anton</title>
      <link>https://dev.to/antmik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/antmik"/>
    <language>en</language>
    <item>
      <title>Object-Oriented User Interface</title>
      <dc:creator>Anton</dc:creator>
      <pubDate>Fri, 05 May 2023 08:36:43 +0000</pubDate>
      <link>https://dev.to/playfulprogramming/object-oriented-user-interface-2e3f</link>
      <guid>https://dev.to/playfulprogramming/object-oriented-user-interface-2e3f</guid>
      <description>&lt;p&gt;Object-Oriented User Interface (OOUI) is a design methodology that aims to separate the implementation layers of a user interface. In contrast to actual Object-Oriented Programming (OOP), OOUI is more of a metaphorical expression that helps developers, QA testers, and designers reduce cognitive complexity and view UI development in small chunks. With OOUI, developers can break down the development process into smaller steps, see results earlier, and improve the overall efficiency of the development process. This article will explore the different stages of implementing OOUI, from the code abstraction stage to integration into a real application, and how it can help developers build UI components in an isolated environment, which allows for easier debugging and testing. Additionally, we will discuss why CSS styles should be implemented as independent units and not directly tied to functionality.&lt;/p&gt;

&lt;p&gt;It’s not a rare case when developers are having hard time starting a new project or a feature. There are always a bunch of unknowns. In addition, almost every engineer faced the case of an “infinite” development process. When results are visible only in the last 5-10% stages of their work. OOUI helps to separate the process into smaller steps and see results much earlier than in the regular approach.&lt;/p&gt;

&lt;p&gt;A UI component should be a black box. It receives an input, then returns an output. A component may have two types of outputs: visual representation, and output data.&lt;/p&gt;

&lt;p&gt;Components should not be coupled. They should not interact with the store, or database or make HTTP requests. &lt;/p&gt;

&lt;h2&gt;
  
  
  The code (abstract class)
&lt;/h2&gt;

&lt;p&gt;Let’s call it an “abstract component”. In the code internal logic, html structure, and styles are defined. There also we define which parts, sub-components it will use.&lt;/p&gt;

&lt;p&gt;An abstract component is the code a developer writes. The major part of code writing is blind. Blind in the meaning that a developer sees only the code, but doesn’t see any visual representation. &lt;/p&gt;

&lt;p&gt;This stage should be separated into small sub-tasks. Otherwise, a developer’s motivation shrinks if the blind phase lasts a long period of time.&lt;/p&gt;

&lt;p&gt;This implementation should contain &lt;strong&gt;unit tests&lt;/strong&gt;. The type of tests which usually check the correctness of the work of functions/methods. In other words, a function receives some data and is expected to return some data or perform an action.&lt;/p&gt;

&lt;h2&gt;
  
  
  Live example of a component (class)
&lt;/h2&gt;

&lt;p&gt;Build UI components in an isolated environment where a developer can focus on a specific component instead of running the whole application. Isolation allows running and debugging a component without the fear of being impacted by other pieces of HTML, CSS, or Javascript.&lt;/p&gt;

&lt;p&gt;That’s not a component in the real application. It’s a stand-alone example where a component receives an input, then shows or does something.&lt;/p&gt;

&lt;p&gt;As input, we use mock data. An example should be created per each different usage of a component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://storybook.js.org/" rel="noopener noreferrer"&gt;Storybook&lt;/a&gt; provides a wide variety of tools to run isolated components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Integration into an application (object, implementation, integration)
&lt;/h2&gt;

&lt;p&gt;After all the previous steps are passed a component is ready to be integrated into the real application. Usually, after the previous stages, there should not be a real reason for the component not to work as expected. Most issues that pop up will be related to the missing functionality. It’s not rare when during the development process an engineer forgets to implement some feature or didn’t have a clear enough technical overview.&lt;/p&gt;

&lt;p&gt;This implementation should be tested via &lt;strong&gt;e2e tests&lt;/strong&gt;. Accessibility tests are among them. Interaction tests as well. They show how a component reacts to user interactions such as clicks, keyboard interaction, scrolling, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS framework
&lt;/h2&gt;

&lt;p&gt;This layer purposefully was not mentioned in the OOUI approach. CSS styles should be implemented as independent units which can be reused in any part of the application without any relation to the functionality. Later these styles are applied via HTML classes.&lt;/p&gt;

&lt;p&gt;In conclusion, Object Oriented UI (OOUI) is not actual OOP, but a metaphorical expression that means separating implementation layers in UI development. By using this approach, developers, QA testers, and designers can reduce cognitive complexity and see results much earlier in the development process. The OOUI approach involves creating an abstract component, building live examples of the component in isolation, and integrating the component into the real application. This process should be separated into small subtasks to keep developers motivated, and each stage should be tested thoroughly to ensure the component works as expected. While CSS styles are an essential part of UI development, they should be implemented as independent units that can be reused in any part of the application without any relation to functionality. Overall, OOUI provides a structured approach to UI development that can lead to more efficient and effective results.&lt;/p&gt;




&lt;p&gt;The post &lt;a href="https://anton-m.dev/2023/05/05/object-oriented-user-interface/" rel="noopener noreferrer"&gt;Object-Oriented User Interface&lt;/a&gt; first appeared on &lt;a href="https://anton-m.dev" rel="noopener noreferrer"&gt;Anton Mi&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>qatesting</category>
      <category>frontend</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Why Web Accessibility Matters</title>
      <dc:creator>Anton</dc:creator>
      <pubDate>Thu, 04 May 2023 08:09:18 +0000</pubDate>
      <link>https://dev.to/playfulprogramming/why-web-accessibility-matters-1o60</link>
      <guid>https://dev.to/playfulprogramming/why-web-accessibility-matters-1o60</guid>
      <description>&lt;p&gt;The internet has become an essential part of our lives. We use it for work, learning, entertainment, and communication. However, not everyone has equal access to the web. In fact, a significant percentage of people are unable to use many websites due to accessibility issues. As a Senior Front End Developer, I am aware of this problem and its importance. In this article, I will discuss why web accessibility matters and how we can implement it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Web Accessibility Statistics
&lt;/h2&gt;

&lt;p&gt;Did you know that only 5% of the most popular websites fully support accessibility? This means that a significant number of people are unable to use them. According to statistics, 15% of the world’s population has some kind of disability. This means that more than one billion people worldwide are affected by accessibility issues.&lt;/p&gt;

&lt;p&gt;The population with differing abilities in the United States alone contains $500 billion in spending power, with global spending power estimated to be $1.2 trillion. By not providing proper accessibility, we are blocking people from working, learning, finding jobs, and enjoying the benefits of the internet. This is not only a matter of equality, human rights, and respect, but it also makes good business sense. Non-accessible websites are turning down a $1.2 trillion market share.&lt;/p&gt;

&lt;h2&gt;
  
  
  Devices have all the Accessibility Functionality, but Websites Have to be in the Right Structure
&lt;/h2&gt;

&lt;p&gt;Most devices have accessibility features built-in, but websites have to be in the right structure to enable all the interactions. The internet has to be not limited to text-only interfaces. The information can be transferred in different ways. We need to consider different people with different types of disabilities and provide them with access to information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Who Needs Web Accessibility?
&lt;/h2&gt;

&lt;p&gt;Web accessibility is not only for people with limited abilities. It is also necessary for people from lower socio-economic layers, people with old and slow devices, and people with low internet speeds. By not considering different people, we are taking a technocentric approach, which is not right.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Web Accessibility
&lt;/h2&gt;

&lt;p&gt;Implementing web accessibility is not easy, but it is essential. Many developers don’t understand accessibility, while others realize it’s needed but don’t know where to start. We can start with semantic HTML tags, headings order, search, ARIA attributes.&lt;/p&gt;

&lt;p&gt;Web accessibility is essential for everyone. We need to provide equal access to the internet and information. Modern web technologies almost have no limits. They can support a variety of colours and shapes. The main problem is not the technological limitations, but the fact that websites are produced for an average user. We need to think beyond the average user and provide accessibility for everyone. The internet is a tool to transfer information, and it can be transferred in different ways. Let’s make it accessible to everyone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Helpful resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Learn accessibility:

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://a11yphant.com/" rel="noopener noreferrer"&gt;https://a11yphant.com/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Information about accessibility:

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.webaccessibility.com/resource-library/platform/?platform=1" rel="noopener noreferrer"&gt;https://www.webaccessibility.com/resource-library/platform/?platform=1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://monsido.com/web-accessibility" rel="noopener noreferrer"&gt;https://monsido.com/web-accessibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.accessibilityassociation.org/s/wascertification" rel="noopener noreferrer"&gt;https://www.accessibilityassociation.org/s/wascertification&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Stats:

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://webaim.org/projects/million/" rel="noopener noreferrer"&gt;https://webaim.org/projects/million/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://webaim.org/projects/screenreadersurvey8/#intro" rel="noopener noreferrer"&gt;https://webaim.org/projects/screenreadersurvey8/#intro&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Tools:

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://marketplace.visualstudio.com/items?itemName=deque-systems.vscode-axe-linter" rel="noopener noreferrer"&gt;Axe vscode&lt;/a&gt; linter&lt;/li&gt;
&lt;li&gt;Chrome devtools lighthouse&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.deque.com/axe/devtools/" rel="noopener noreferrer"&gt;Axe devtools&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;The post &lt;a href="https://anton-m.dev/2023/05/04/why-web-accessibility-matters/" rel="noopener noreferrer"&gt;Why Web Accessibility Matters&lt;/a&gt; first appeared on &lt;a href="https://anton-m.dev" rel="noopener noreferrer"&gt;Anton Mi&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>posts</category>
      <category>a11y</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How to create reliable and scalable frontend applications</title>
      <dc:creator>Anton</dc:creator>
      <pubDate>Fri, 21 Jan 2022 14:41:33 +0000</pubDate>
      <link>https://dev.to/playfulprogramming/how-to-create-reliable-and-scalable-frontend-applications-5578</link>
      <guid>https://dev.to/playfulprogramming/how-to-create-reliable-and-scalable-frontend-applications-5578</guid>
      <description>&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;A software engineer day to day work usually focused on some specific application or even piece of that software. When a long period of time developer works on only one program he may experience difficulties in the case he needs to start with a &lt;strong&gt;new project&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this article, I would like to share my findings which come from frontend applications built by me in recent years.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Overview&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;First, a walk when planning a new application it has to be decided what is the main purpose.&lt;/p&gt;

&lt;p&gt;There are two main directions for a web-based application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A website with a &lt;strong&gt;public content&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Web/Native&lt;/strong&gt; &lt;strong&gt;application&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For content websites, it’s highly suggested to use server-side rendering such as Next.Js, Angular SSR, Gatsby, or similar. These technologies will provide a better performance as well as better Search engine optimization.&lt;/p&gt;

&lt;p&gt;On the other hand, web or native applications are used when it’s required a high level of in-app interactions.&lt;/p&gt;

&lt;p&gt;In addition, it has to be defined whether the application will have offline mode or features. In that case, the application is considered a Progressive Web App and the usage of service workers will be required.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Proposed Technologies list&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;State — Redux&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React, react-router&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;UI — MUI or Bootstrap&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Linting — Husky, eslint, prettier&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Testing — Jest&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CI/CD — Gitlab&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Folders structure&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The following folder structure can be used for the medium as well as small apps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Components — all components. Each can have input/output&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Containers — components defining specific layout&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pages — A page will use one of the containers and contains components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Routes — contains route declarations&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Config — Constants&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Services&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Api specific files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Authorization&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;General services — such as traces/logs, system notifications, etc&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Store — Redux’s store files. Such as reducers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The root folder will contain package.json, eslint, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;.ENV — environment-specific constants&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For large and multi-application projects consider reading the article &lt;a href="https://dev.to/this-is-learning/semantic-grouping-folders-with-nx-3467"&gt;“Semantic Grouping Folders with Nx”&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;General essential features&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Logging, tracing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Authorization: Send credentials -&amp;gt; receive token. All manipulations with sensitive data should be working via the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication" rel="noopener noreferrer"&gt;Authorization header&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Centralized system notifications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Generic pop-ups: Confirmation popup&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;User activity statistics: Backend should save each user action/request for further analysis or an external service can be used.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Code modularity&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Modularity is achieved by separating functionality into &lt;strong&gt;components&lt;/strong&gt;. Each of them should have one responsibility. Components will have Input/Output data.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;State management&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://redux.js.org/tutorials/fundamentals/part-1-overview" rel="noopener noreferrer"&gt;Redux &lt;/a&gt;&lt;/strong&gt;is one of the suggested options for state management. State flow in react app &lt;strong&gt;unidirectional **and **immutable&lt;/strong&gt;. Immutability allows stability and debugging features such as states history. Where it’s possible to go “back in time” analyzing all the state changes.&lt;/p&gt;

&lt;p&gt;There are two types of components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;Stateless *&lt;/em&gt;— has I/O data and does not manage states&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;Stateful *&lt;/em&gt;— manages states and transfers into the components. Also are dividing when to transfer state to regular components in order to reduce component rerendering&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Performance&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;APi calls In browser *&lt;em&gt;caching *&lt;/em&gt;— rarely updated data should be stored in Browser’s cache. It is achieved by setting cache &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control" rel="noopener noreferrer"&gt;headers for HTTP&lt;/a&gt; responses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;App files caching — images, fonts, js bundles should be cached in the browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reduce components &lt;strong&gt;re-rendering&lt;/strong&gt; by limiting states flow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lazy loading&lt;/strong&gt; — the application will load only necessary bundle files. Achieved by &lt;a href="https://reactjs.org/docs/code-splitting.html" rel="noopener noreferrer"&gt;code splitting&lt;/a&gt; techniques.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Code quality&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In general, the code has to obey industry best practices. The code has to be easily readable and built of small components/methods/classes where each has one responsibility focused.&lt;/p&gt;

&lt;p&gt;However, each developer may have his own code style preferences. Therefore it’s strongly suggested to force project-specific code standards. Alignment with code standards can be achieved by using &lt;strong&gt;eslint&lt;/strong&gt; library. &lt;strong&gt;Pre-commit hooks&lt;/strong&gt; will secure that no non-standard code arrives in the Git repository.&lt;/p&gt;

&lt;p&gt;In addition code formatting could be done by &lt;strong&gt;Prettier&lt;/strong&gt;. This operation can be attached to the pre-commit hook as well.&lt;/p&gt;

&lt;p&gt;Code readability can be achieved with the help of the &lt;strong&gt;CodeMetrics, SonarLint, SonarQube&lt;/strong&gt; vscode plugins or similar. The tool will analyze the &lt;strong&gt;cognitive complexity&lt;/strong&gt; of the code and will suggest improvements. In general functions/methods should be short and avoid multilevel nested loops or conditionals.&lt;/p&gt;

&lt;p&gt;Type declarations are another important point in frontend development. Static type definition provides greater reliability as well as readability. The application should be written on &lt;strong&gt;&lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt;&lt;/strong&gt; which has wide support and community.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Unit testing&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Each component has to be test covered by at least 70%. &lt;strong&gt;&lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;Jest&lt;/a&gt;&lt;/strong&gt; is one of the well-supported libraries for that purpose.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Versioning&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Git is the most preferred option for version control.&lt;/p&gt;

&lt;p&gt;Each commit has to obey commit message standards. &lt;a href="https://www.conventionalcommits.org/en/v1.0.0/" rel="noopener noreferrer"&gt;This link&lt;/a&gt; gives a good explanation. Following the standard will provide good readability of the app development history.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Deployment&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Gitlab can be used for managing deployments and Continous Integration. Repository updates have to be pushed as new branches. On each commit Gitlab will run unit tests.&lt;br&gt;
After the code review and pipeline passes, a merge request can be created. After the MR is approved commits will become a part of the master/main branch and the original branch will be erased.&lt;/p&gt;

&lt;p&gt;The application should have multiple deployment environments such as Stage, Dev, Production. The stage will have the latest master version. After it passes QA tests it can be promoted to Production.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Accessibility&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The application has to be accessible to people with all, abilities. It has to support screen readers, color schemes, and font size adaptability.&lt;/p&gt;

&lt;p&gt;Chrome Lighthouse dev tool can be used to analyze the level of accessibility covered by an application.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;UI&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use one of the well-supported UI frameworks such as &lt;strong&gt;&lt;a href="https://mui.com/" rel="noopener noreferrer"&gt;Mui &lt;/a&gt;&lt;/strong&gt;or &lt;strong&gt;&lt;a href="https://getbootstrap.com/" rel="noopener noreferrer"&gt;Bootstrap&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multiple themes support. At least two: light and dark modes should be&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Responsive — mobile-first approach. Guarantees the app will not lack functionality on all devices.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Security&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At least the following points should be considered when creating a Frontend application.&lt;/p&gt;

&lt;p&gt;Frontend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;User-generated data &lt;strong&gt;sanitation.&lt;/strong&gt; React and Angular natively support sanitation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Auth &lt;strong&gt;token secure storage&lt;/strong&gt; in HttpOnly only cookies. Refer to the &lt;a href="https://owasp.org/www-community/HttpOnly" rel="noopener noreferrer"&gt;explanation &lt;/a&gt;on the OWASP page.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Backend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Limit&lt;/strong&gt; number of &lt;strong&gt;HTTP requests&lt;/strong&gt; per user in order to avoid DDOS attacks&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Limit &lt;strong&gt;login attempts&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://owasp.org/" rel="noopener noreferrer"&gt;OWASP &lt;/a&gt;rules&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Migration
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Styles separation&lt;/strong&gt; — when creating custom styles separate a set of SCSS files containing all the common styles. In the case of migration to another SPA library, the styles can be reused.&lt;/p&gt;

&lt;p&gt;It is always difficult to migrate a large codebase. For instance, an Angular app migrates to React. In the major part of the cases, each SPA library has its own architecture and it will not be possible to copy the components. &lt;/p&gt;

&lt;p&gt;However such tools as &lt;a href="https://nx.dev/" rel="noopener noreferrer"&gt;NX &lt;/a&gt;or &lt;a href="https://www.npmjs.com/package/@angular-architects/module-federation" rel="noopener noreferrer"&gt;Module Federation&lt;/a&gt; can manage micro frontends and allow gradual migration from one SPA library to another.&lt;/p&gt;

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

&lt;p&gt;The current article discussed the main best practices which should be considered when planning a new frontend application. All the mentioned techniques may seem like an exaggeration. Though each of them increases the maintainability and reliability of an application.&lt;/p&gt;

&lt;p&gt;Let’s recap what we learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Define the type of the project if it’s content-based or app&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Performance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Modularity&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;State management&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code quality: Typescript, linting&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stability: Unit tests&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Versioning: git&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deployment: GitLab&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;UI: MUI, Bootstrap&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessibility&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Security&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Migration&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@marcojodoin?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Marc-Olivier Jodoin&lt;/a&gt; on &lt;a href="https://unsplash.com/@marcojodoin?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>angular</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Code quality: pre-commit linting</title>
      <dc:creator>Anton</dc:creator>
      <pubDate>Wed, 12 Jan 2022 19:22:14 +0000</pubDate>
      <link>https://dev.to/playfulprogramming-angular/code-quality-pre-commit-linting-348o</link>
      <guid>https://dev.to/playfulprogramming-angular/code-quality-pre-commit-linting-348o</guid>
      <description>&lt;p&gt;In 2012 I started my attempts to develop web applications. As the first project, I chose to build a website constructor. This kind of software requires a ton of client interactions. Since then I knew only one library able to assist me with that mission. It was the legendary jQuery. I put all my efforts to achieve the interactivity goals. And the end result was pretty good. Even now I am still proud of it. It was my first real project. Though since then I knew nothing about the code quality and architecture patterns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“The code works, don’t touch it”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When I finished building the initial phase including backend and frontend parts started to mention that I am not feeling so comfortable with the code I built.&lt;/p&gt;

&lt;p&gt;jQuery gave me a lot of freedom to structure the files and folders. And I have to say it was bad freedom. So my code looked like you were leaving your dog alone at home. All the items were in total disorder and everything upside down. It wasn’t a total disorder, but just a brand new order for each component of the application.&lt;/p&gt;

&lt;p&gt;Initially, all was smooth and clear. I was holding all the code base in my mind. After a while, it grew larger and that’s when the problems started. Each time I needed some component I had to dig up all the code to find the component. Though it’s another topic.&lt;/p&gt;

&lt;p&gt;The main issue with the code was the fact I was writing the code in different code styles. One time I used tabs, the next time I used spaces. Variables, methods, and constants were written in different cases without obeying any code style.&lt;/p&gt;

&lt;p&gt;From one side it was my problem not knowing coding best practices. On the other hand, junior developers cannot know all these nuances.&lt;/p&gt;

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

&lt;p&gt;Every programmer may have his preferences on how to write code. The code style may differ between coders. Nothing bad in that fact. We are all humans and have our perceptions. However, usually, software engineers are working in teams. That’s the point where problems are starting.&lt;/p&gt;

&lt;p&gt;Imagine you’ve been given a task to do a code review. You are trying to read the code, but after a while realize you can understand nothing and the main issue in the code style. Variables are written in unusual cases, as well as functions. Lines are too long and go out of the screen borders. Indents not aligned. Once there is a line of space, another time no lines at all.&lt;/p&gt;

&lt;p&gt;The code style is not the only and the biggest problem. The right usage in code itself means a lot for performance as well as readability.&lt;/p&gt;

&lt;p&gt;What do I mean? For instance, let was used for a variable that has a single value assignment. If it’s not going to change why aren’t you using const? Or another case. You’ve imported some libraries to a file but afterward decided not to use them. It should be removed from the file if it’s not used otherwise the application will have a larger size than it requires.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s compare linted and prettified code against the same code lacking linting. For that purpose, we will take Angular code.&lt;/p&gt;

&lt;p&gt;Non-linted and non-formatted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;CrudService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;src/app/services/crud.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&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="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-example&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./example.component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./example.component.scss&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;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExampleComponent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;constructor&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="nf"&gt;ngOnInit&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;public&lt;/span&gt; &lt;span class="nf"&gt;someMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;rabbitName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Walter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;rabbitName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;anotherMethod&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;one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;one&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;two&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;Linted and formatted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&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="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-example&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./example.component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./example.component.scss&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;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExampleComponent&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="nf"&gt;constructor&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="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;someMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Walter&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;public&lt;/span&gt; &lt;span class="nf"&gt;anotherMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;one&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;two&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;As you can see the second example has nothing unnecessary inside and is more readable. This is only a tiny demonstration. Imagine having an app containing dozens of files and thousands of lines of code.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Linter is the solution&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;It’s good to keep all the best practices in your head. However, even senior developers may miss something unintentionally. There are a couple of tools able to assist developers to keep their code clean and structured.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Eslint&lt;/strong&gt;
&lt;/h3&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%2F4s1gpwfzxo7fcaj7gcmd.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%2F4s1gpwfzxo7fcaj7gcmd.png" alt=" " width="351" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Linter is the main instrument to assist with code style and syntax issues. You may want to use the **eslint **extension if you’re working with vs code. It analyzes the code in a live mode and proposes improvements.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Prettier&lt;/strong&gt;
&lt;/h3&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%2F3p75sddj76ntn227nefr.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%2F3p75sddj76ntn227nefr.png" alt=" " width="351" height="67"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is another useful tool for developers called &lt;strong&gt;Prettier&lt;/strong&gt;. It also can be used as a vscode extension. The main purpose of the instrument is to format the code in a visually nice manner. It aligns all spaces and indents providing developers with beautiful code formatting.&lt;/p&gt;

&lt;p&gt;Only having those extensions within the IDE doesn’t guarantee all the developers on the team are responsible enough and going to be using the tools.&lt;/p&gt;

&lt;p&gt;How do we ensure no unlinted or unformatted code will appear on the git repository? Unfortunately, none of the large frontend libraries/frameworks are supporting forceful code linting. Most of them have lint cmd commands which are running linter and verifying the code following standards.&lt;/p&gt;

&lt;p&gt;Anyways there are npm packages able to assist with this mission.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://www.npmjs.com/package/husky" rel="noopener noreferrer"&gt;Husky&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;There is an npm package coming up to help us to secure none of the unclean commits will drop into our git tree. It’s called Husky. The main purpose of the package is to create hooks for git operations. That way it’s possible to bind Eslint and Prettier actions on the pre-commit hook.&lt;/p&gt;

&lt;p&gt;Please refer to this &lt;a href="https://stackoverflow.com/questions/67176520/how-can-i-run-ng-lint-and-detect-errors-on-pre-commit#fromHistory" rel="noopener noreferrer"&gt;StackOverflow question&lt;/a&gt;. Where there is a basic setup for Husky. In addition, it explains how to use the pre-commit hook.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Recap&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The current article discusses the necessity of code formatting and linting. Keeping eye on code quality is essential especially in large projects when it’s easy to mess up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lint&lt;/strong&gt; — script responsible for alarming developers when the code violates standards.&lt;/p&gt;

&lt;p&gt;Code &lt;strong&gt;formatting/prettification&lt;/strong&gt; — an operation that makes the code more human-readable.&lt;/p&gt;

</description>
      <category>eslint</category>
      <category>javascript</category>
      <category>angular</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Getting rid of NgModule in Angular. Javascript</title>
      <dc:creator>Anton</dc:creator>
      <pubDate>Wed, 05 Jan 2022 20:42:21 +0000</pubDate>
      <link>https://dev.to/playfulprogramming-angular/getting-rid-of-ngmodule-in-angular-javascript-2gcc</link>
      <guid>https://dev.to/playfulprogramming-angular/getting-rid-of-ngmodule-in-angular-javascript-2gcc</guid>
      <description>&lt;p&gt;Working many years with angular I found it hard to create the structure of a project. The main reason for that struggle lay behind the way Angular interacts with components and modules. The following article explains what is the issue with NgModule and whether is going to change in the foreseeable future.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Builtin lazy loading mechanisms&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Angular has NgModule based architecture. In other words, each application has at least one NgModule which connects all the components and routes together. It is a practical approach for applications structure. An application has a central unit that contains all the declarations. That’s right when we are talking about small web apps.&lt;/p&gt;

&lt;p&gt;When working with large Single Page Applications performance becomes a critical pain point. It is a time to start thinking about optimization. One of the ways to reduce loading times is to minify application size on the first load. This goal can be achieved with lazy loading. The technique is supported by angular routing mechanisms.&lt;/p&gt;

&lt;p&gt;The technique allows loading specific parts of an application only when needed. Here is the &lt;a href="https://angular.io/guide/lazy-loading-ngmodules" rel="noopener noreferrer"&gt;full documentation&lt;/a&gt; on how to implement lazy loading within Angular framework.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is wrong with NgModules?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Up to now, all seems correct, right? Let’s zoom in to see whether potential issues could pop up.&lt;/p&gt;

&lt;p&gt;For instance, we will take the Angular Material UI. The framework should comply with best practices because it is built and maintained by the Angular team. Looking at the components provided you may mention that each of them has its own NgModule. It is done to allow importing of a single component without grabbing all them and overloading the application.&lt;/p&gt;

&lt;p&gt;What does all that mean for me as a developer? Practically you will need to create a NgModule for each of the components. Thus double action on each component creation.&lt;/p&gt;

&lt;p&gt;For newbies, the concept may appear quite painful. To be honest for me to work in this way is still frustrating.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;VueJs components interaction&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When I started to work with VueJs I didn’t feel any discomfort. Just after some period of time mentioned I am doing fewer actions to achieve the same component interactions than I was doing in Angular. “How could it be?” I thought. Just after side by side comparison, figured out what was missing through all the development process with VueJs. The answer was “modules”.&lt;/p&gt;

&lt;p&gt;In VueJs all is based on components. Do you want to create a page? Make a component. Do you want to create a reusable UI piece of code? Make component! Nothing more. Just that easy.&lt;/p&gt;

&lt;p&gt;Aren’t modules making a more coherent structure? Correct me if I am wrong, but I didn’t mention any practical benefit of this additional architecture layer yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Is it going to change?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Yes, the Angular &lt;a href="https://angular.io/guide/roadmap#simplified-angular-mental-model-with-optional-ngmodules" rel="noopener noreferrer"&gt;roadmap&lt;/a&gt; gives all Angular developers a ray of hope. The proposal was explained and discussed in this “&lt;a href="https://github.com/angular/angular/discussions/43784" rel="noopener noreferrer"&gt;[Complete] RFC: Standalone components, directives and pipes — making Angular’s NgModules optional&lt;/a&gt;”.&lt;/p&gt;

&lt;p&gt;Yet, pay attention to this statement:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This proposal is not trying to remove the concept of a NgModule from Angular — it is rather making it optional for typical application development tasks.&lt;/em&gt;&lt;br&gt;
 &lt;em&gt;At the same time we believe that it paves the path towards greatly reducing the role of NgModule for typical development scenarios — to the point that some time in the future it would be possible and reasonable for us to consider removing it altogether.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;To conclude&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Getting rid of modules is the first step towards Angular architecture simplification. So far I see only benefits in that act.&lt;/p&gt;

&lt;p&gt;Who knows maybe in the future Angular will adopt a functional components approach as React did instead of classes.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>vue</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Frontend unit testing</title>
      <dc:creator>Anton</dc:creator>
      <pubDate>Thu, 23 Dec 2021 10:32:13 +0000</pubDate>
      <link>https://dev.to/antmik/frontend-unit-testing-15nh</link>
      <guid>https://dev.to/antmik/frontend-unit-testing-15nh</guid>
      <description>&lt;p&gt;Javascript was invented a few decades ago. Initially was used for small in-browser interactions. Though in recent ten years javascript passed through huge evolution. It is mostly related to the invention of SPAs (single page applications) and NPM package manager. Which gave superpowers to the already known Javascript. With those transformations, the code base started to become larger and more complex. A larger code base is more vulnerable to errors. In this article, we will see how to make a codebase resistant to errors. At least to reduce part of them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Unit testing?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We are not gonna go into details about what the Units are. Briefly saying, software engineering best practices suggest separating the code into small components or modules. Such separation will increase the maintainability and readability of code.&lt;/p&gt;

&lt;p&gt;Going back to our topic. Unit tests are responsible for keeping our code correctly functioning when introducing new changes. They are small interactions with specific functions or components. As a result of these interactions, unit tests expect to receive specific responses from the component/function. At least the core functions of an application should be tested.&lt;/p&gt;

&lt;p&gt;To simplify, let’s use a real-life example. You want to buy a box of six eggs. What should you check?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Is it a box of eggs or something else&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Are there eggs at all&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Are there six eggs or less&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Is there a broken egg&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Are they the size you wanted&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What if I don’t write unit tests?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Nothing happens. At least from first sight. But as the larger your codebase grows harder becomes to maintain it and debug. Sometimes you may introduce a bug and don’t mention it. Then after weeks of development, it pops up and crashes the application. You start panicking and debug every bit of your code. After hours or even days spent debugging you figure out the problem was in a small mistake.&lt;/p&gt;

&lt;p&gt;What if somebody told you could’ve avoided this situation? Or at least reduce the number of them. One of the solutions is Unit tests.&lt;/p&gt;

&lt;p&gt;Sometimes it may seem boring to write tests. You just want to be in the free flight with the code with creativity fireworks exploding every single minute of the work. Though I’d like to warn you (like your grandma) if start falling you didn’t take a parachute.&lt;/p&gt;

&lt;p&gt;Yes, there are cases when you don’t need any tests. For example, you develop a prototype that never will go to production. Here is the goal not to develop a stable app, but a proof of concept.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do they work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the modern javascript environment, there are a variety of libraries making testing comfortable for developers. Some of the most popular are &lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt;, &lt;a href="https://jasmine.github.io/"&gt;Jasmine&lt;/a&gt;. They have built-in methods. The core are “expect” methods. Which take a method/component and check whether the output is the one we expect to receive.&lt;/p&gt;

&lt;p&gt;Let’s take jasmine for example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A suite is just a function&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;and so is a spec&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;**=**&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to write them?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are a variety of approaches to unit tests. Each developer has his own preferences related to testing. Also, each company has its guidelines and requirements for testing software.&lt;/p&gt;

&lt;p&gt;TDD approach suggests writing tests beforehand and then implementing code. However, this approach fits systematic software development when almost everything is described already in the technical overview. In many cases, you choose solutions and write methods in the process. That means methods written before tests.&lt;/p&gt;

&lt;p&gt;Even in that case I strongly suggest writing tests step by step. You’ve written a method, write unit tests. Otherwise, you end up with a long code with many methods or dependencies involved. And when you will start testing it can turn into a strong headache.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mocking data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The goal of unit tests is to check specific units such as class, component, function. That means you need to ignore all non-relevant code. Yet many components have external dependencies on third-party libraries or other components. In this case, within the unit test we need to replace these dependencies with static code which will simulate responses and states.&lt;/p&gt;

&lt;p&gt;Jasmine for instance has already built-in methods for mocking data. Such as &lt;strong&gt;createSpyObj&lt;/strong&gt; or &lt;strong&gt;spyOn&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test coverage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each time you build tests ensure they are really testing what you need. There are many cases when test passes and it is green, but in the reality, it didn’t check anything or checked the wrong statement.&lt;/p&gt;

&lt;p&gt;Another mistake could be not putting expectations to the test. But this case is easier to discover because testing frameworks such as Jasmine will write “&lt;strong&gt;NO EXPECTATIONS…&lt;/strong&gt;” before each tets case lacking expectation. Even the test will pass, though you will see it’s useless.&lt;/p&gt;

&lt;p&gt;Be careful with test coverage. You have to check whether all or most of the cases are covered by the test.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To conclude&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The main goal of the unit tests is to provide stable work for development teams.&lt;/p&gt;

&lt;p&gt;Let’s recap:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Test &lt;strong&gt;all&lt;/strong&gt; possible case &lt;strong&gt;scenarios&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check the level of &lt;strong&gt;code coverage&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Mock **any external classes/services&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don’t test &lt;strong&gt;third-party&lt;/strong&gt; libraries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Write tests&lt;/strong&gt; as soon as you progress with the development. Don’t wait until the end&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://jasmine.github.io/"&gt;Jasmine&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cover gif. &lt;a href="https://imgur.com/gallery/tPPNeaJ"&gt;“Completely obliterate the car”&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>angular</category>
      <category>webdev</category>
      <category>testing</category>
    </item>
    <item>
      <title>How to create a simple CSS only dropdown </title>
      <dc:creator>Anton</dc:creator>
      <pubDate>Sat, 18 Dec 2021 13:04:05 +0000</pubDate>
      <link>https://dev.to/antmik/how-to-create-a-simple-css-only-dropdown-529a</link>
      <guid>https://dev.to/antmik/how-to-create-a-simple-css-only-dropdown-529a</guid>
      <description>&lt;p&gt;This is a small CSS tutorial I decided to write this morning. Maybe later I should do more.&lt;/p&gt;

&lt;p&gt;Today's topic gonna be CSS based dropdown menu. The code itself was written from scratch. It took me around 30 minutes. The only external code was used is CSS reset.&lt;/p&gt;

&lt;p&gt;I am using Pug to write quick HTML and SCSS.&lt;/p&gt;

&lt;p&gt;The first thing is to create a dropdown container div.&lt;/p&gt;

&lt;p&gt;Then put inside a button. It will serve as a trigger. Then we put inside an unordered list with a few items.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.dropdown
 button.trigger-btn Hover me
  ul.dropdown__items
   li.dropdown__item Menu item 1
   li.dropdown__item Menu item 2
   li.dropdown__item Menu item 3
   li.dropdown__item Menu item 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then we add padding, background color, etc.&lt;/p&gt;

&lt;p&gt;For color selection, &lt;a href="https://materialui.co/colors/"&gt;Material colors&lt;/a&gt; were used. There you can quickly choose some nice-looking colors.&lt;/p&gt;

&lt;p&gt;Shadows for the dropdown were generated with a help of a &lt;a href="https://cssgenerator.org/box-shadow-css-generator.html"&gt;CSS generator&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We finished with styles and HTML. Now, how actually does the magic works?&lt;/p&gt;

&lt;p&gt;In order to have a dropdown appear on mouse-over we need to set :hover pseudo-class. When the .dropdown container is mouse-over.dropdown__items element becomes visible by adding display:block CSS rule.&lt;/p&gt;

&lt;p&gt;Check out the code example to understand better how it works:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/AntM/embed/QWMPEgO?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How to create a parallax on pure Javascript</title>
      <dc:creator>Anton</dc:creator>
      <pubDate>Tue, 07 Dec 2021 10:58:24 +0000</pubDate>
      <link>https://dev.to/antmik/how-to-create-a-parallax-on-pure-javascript-5hgj</link>
      <guid>https://dev.to/antmik/how-to-create-a-parallax-on-pure-javascript-5hgj</guid>
      <description>&lt;p&gt;Everyone at least once has seen parallax effects on a website. They could’ve been simple. Such as a static background on a specific spot while scrolling. On the other hand, these effects can be extremely complex. However usually visually this effect looks impressive.&lt;/p&gt;

&lt;p&gt;Parallax always amazed me. How it’s done in web development. It seemed to me so complex and unachievable. Then after a couple of years of contemplation of beautiful websites using this technique I decided to give it a try.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is parallax?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The meaning of the word “parallax” refers to a specific technique used in computer graphics. Multiple layers move at different speeds, while the user scrolls a webpage. This number of movements creates an optical illusion. To be honest the technique is not new. It was used in the early 1980’s Super Mario Bros game to create the sensation of depth. Then this graphic trick got a second life in web design.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tech stack&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As already mentioned above here I used vanillaJS (pure JavaScript). Pug templating language was used to accelerate work with HTML. As well as SCSS instead of pure CSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What was created and how it works?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It started as an experiment. Though at the end turned into a small library. It requires a small HTML and a few Javascript functions. We have specific classes. Each &lt;code&gt;.prlx-item-inner&lt;/code&gt; can have an animation. Also, each one can have multiple animations. So you can combine for instance zoom with spinning.&lt;/p&gt;

&lt;p&gt;This code can be used on any HTML website. Javascript &lt;em&gt;&lt;code&gt;(function () {} )&lt;/code&gt;&lt;/em&gt; wrapping will prevent conflicting with any other library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reusable classes list&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Directional movements classes: let, right,top,bottom&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;speed- — Can have any number. Ex.: &lt;code&gt;speed-3&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;spin — spinning object&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;zoom — zooms out&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;To structure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The basic elements needed for this are the container and the number of inside elements which gonna is moving on scroll.&lt;/p&gt;

&lt;p&gt;Here is the HTML code: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#prlx-container
  .text-top
    p Scroll down to see the parallax effect
  .row
    .prlx-item
      .prlx-item-inner.left.speed-3
    .prlx-item
      .prlx-item-inner.top.speed-2.spin
    .prlx-item
      .prlx-item-inner.right
    .prlx-item
      .prlx-item-inner.left.speed-5
    .prlx-item
      .prlx-item-inner.zoom.speed-2
    .prlx-item
      .prlx-item-inner.right.speed-5
    .prlx-item
      .prlx-item-inner.left
    .prlx-item
      .prlx-item-inner.bottom
    .prlx-item
      .prlx-item-inner.right.spin
  .side-dots
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this example I set &lt;code&gt;.row&lt;/code&gt; element as &lt;em&gt;&lt;code&gt;position:fixed;&lt;/code&gt;&lt;/em&gt;. So it’s always will be visible.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;.prlx-item-inner&lt;/em&gt; has to be &lt;em&gt;position:absolute;&lt;/em&gt; this way we can control it with x,y (top, left) coordinates. It’s parent &lt;em&gt;.prlx-item&lt;/em&gt; is &lt;em&gt;position: relative;&lt;/em&gt;. That way it’s children will move relatively to the parent instead moving relatively the whole viewport.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Javascript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are three functions responsible for movements. &lt;code&gt;zoom()&lt;/code&gt;, &lt;code&gt;spin()&lt;/code&gt;, &lt;code&gt;move()&lt;/code&gt; respectively.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;checkMovement()&lt;/code&gt; check which direction, speed and animation type is set to an element. Then&lt;code&gt;bindMovementsToElement()&lt;/code&gt; binds specific animations to an html element.&lt;/p&gt;

&lt;p&gt;At the end of the file we have &lt;em&gt;&lt;code&gt;window.addEventListener(‘scroll’, ()=&amp;gt;{})&lt;/code&gt;&lt;/em&gt; event which triggered each time scrolling happens and activates animations on html elements.&lt;/p&gt;

&lt;p&gt;That’s it. Nothing more. The code is simple and quick.&lt;/p&gt;

&lt;p&gt;Here is the code itself:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/AntM/embed/YRMoLo?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It started as an experiment. Though at the end turned into a small library. Maybe one day I will publish it on github. At the moment anyone can try it on codepen and use on his own website.&lt;/p&gt;

</description>
      <category>html</category>
      <category>javascript</category>
      <category>css</category>
      <category>codepen</category>
    </item>
    <item>
      <title>How I improved nested arrays performance in our MongoDB</title>
      <dc:creator>Anton</dc:creator>
      <pubDate>Tue, 30 Nov 2021 20:10:15 +0000</pubDate>
      <link>https://dev.to/antmik/how-i-improved-nested-arrays-performance-in-our-mongodb-nko</link>
      <guid>https://dev.to/antmik/how-i-improved-nested-arrays-performance-in-our-mongodb-nko</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GyRDvFsz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/12000/0%2Aoh9neGNt17Nx3vxQ" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GyRDvFsz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/12000/0%2Aoh9neGNt17Nx3vxQ" alt="Photo by [Fotis Fotopoulos](https://unsplash.com/@ffstop?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it started?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In 2019 I started to work on &lt;a href="https://fanficus.com"&gt;fanficus.com&lt;/a&gt;. Which is Russian speaking fiction writers/readers social network. Spent around a month thinking about how to structure the architecture of the web application. In the beginning, I didn’t know exactly what I am working on. Initially, it seemed a small side project for a few months.&lt;/p&gt;

&lt;p&gt;When starting I decided to choose to MEAN full-stack (MongoDB, Angular, ExpressJs, NodeJs). However, it was a dilemma what to choose MySQL or MongoDB. Because previously I had some experience with MySQL and I knew SQL databases had a major market share in web projects. MongoDB was chosen because it is based on javascript objects, therefore it’s natural to use on the current stack.&lt;/p&gt;

&lt;p&gt;The largest part of the web app is based on posts. They can be of different types and include a lot of parameters. Such as age rating, genres, tags, etc.&lt;/p&gt;

&lt;p&gt;Understanding that the database has to be flexible I decided to avoid nested arrays of objects. Because that was creating a risk of misalignment between collections. For example, there is a post that has genres array objects. Genre contains title and id. But at the same time, there is a genres collection. Where all genres are listed. We use it when creating new posts.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;post: {
  genres: [id1, id2, id3],
  tags: [id1, id2, id3]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, what happens if we rename a genre? in the genres collection, it is renamed, but in all posts containing the genre, it remained with the old name. So we end up with a post that contains inexisting genre.&lt;/p&gt;

&lt;p&gt;Yes, you may say each time we update the genre we can update all the posts containing this genre. Though I wanted to avoid this obsolete load on the database.&lt;/p&gt;

&lt;p&gt;The way was chosen is to store in the post only an array of IDs of genres. It seemed the most effective solution. Anyway, it was more optimal than going in an SQL way and having three collections: posts, genres, post-genre.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The website was released and worked well enough. Even on each request, it was populating all the posts with an age rating, genres, etc. As a result on the frontend, I was receiving the full objects and didn’t need to run additional queries.&lt;/p&gt;

&lt;p&gt;However, after a few months number of posts increased. We started to mention slow loading times. After spending some time on the investigation figured out what was the issue. And the problem was in an ****array of IDs. Each post contained up to ten such nested arrays. Meaning each post had to run ten population commands.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PostModel.find({}).populate(‘genres’).populate(‘tags’).populate(‘ageRating’).exec();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It was not the only problem. The way we run search queries on posts also was impacted by the way we store nested IDs. Each time we run a search on the website it was seeking tags titles, then we took IDs and run a posts query.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const tagsFound = await TagModel.find({‘title’: { $in: keywordsRegArr }}).exec();
const tagsIdsArr = tagsFound.map( tag=&amp;gt; tag._id );
PostModel.find({tags:tagsIdsArr}).exec();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In addition to page load performance, we saw our MongoDB Atlas server was charging too much.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it was solved?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After long arguments with myself, I saw that the most optimal solution will be to store the full objects inside each post. Yes, it was increasing database size. However, it was solving the main issue with performance and querying.&lt;/p&gt;

&lt;p&gt;Now posts collection looked this way:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;post: {
  genres: [{id: 1, title: 'one'}, {id: 2, title: 'two'}],
  tags: [{id: 1, title: 'one'}, {id: 2, title: 'two'}]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It was meaning now each time we search posts we don’t need to search tags to get their IDs. Now it’s just running **text search **over all posts.&lt;/p&gt;

&lt;p&gt;One more important thing that was added is &lt;strong&gt;caching&lt;/strong&gt;. For that one, I used node-cache npm package. Part of the queries is cached on the NodeJs. That way we reduce the load on the database. Some requests are cached for hours, some for minutes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As already was said, now we were able to run a &lt;strong&gt;text search&lt;/strong&gt; query and avoid multiple &lt;strong&gt;populations&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Post objects were retrieved from the posts-collection directly without any manipulations.&lt;/p&gt;

&lt;p&gt;As a result query and loading times were reduced at least by four times. And it’s on the current database which is in comparison to social networks is small.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Drawbacks&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Now each time we change genres, age ratings, etc, we need to &lt;strong&gt;update all posts&lt;/strong&gt; containing these objects. But these items change rarely, so we can absorb this one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then also I had to modify &lt;strong&gt;search queries&lt;/strong&gt; from the &lt;strong&gt;client app&lt;/strong&gt;. Because post-collection contained a nested array of objects instead of an array of IDs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Storage size&lt;/strong&gt; increased. In the end, the database size didn’t increase significantly. We didn’t even mention it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The project was released a year ago. At the moment we have 22k users and it’s growing. We have a lot of features implemented, though a lot more are in development. There are many issues we meet on the road. However, overcoming them and conquering the next mountain peaks.&lt;/p&gt;

&lt;p&gt;Now the database refactoring is tested on our testing server and gonna be released soon.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>mongodb</category>
      <category>angular</category>
    </item>
  </channel>
</rss>
