• Home
  • About Us
  • GDPR Policy
  • Disclaimer
  • Privacy Policy
  • Contact Us
bdma
  • Business
  • Knowledge
    • Google Adsense
    • Visa
  • Sports
  • Travel
  • Technology
  • Trending
  • Entertainment
    • Bollywood
No Result
View All Result
  • Business
  • Knowledge
    • Google Adsense
    • Visa
  • Sports
  • Travel
  • Technology
  • Trending
  • Entertainment
    • Bollywood
No Result
View All Result
bdma
No Result
View All Result

A Framework For Building Clean, Easy-To-Maintain JavaScript Apps

bdma by bdma
August 24, 2023
in Technology
0

(This story is an original creation generated automatically from the Syndicated News feed. The content body has not been modified or edited by the BDMA Team.)

A Framework For Building Clean, Easy-To-Maintain JavaScript Apps

I, for one, can barely remember a world without the internet. 

In truth, I can scarcely remember life without a smartphone. Looking at the stats, I think it’s fair to say many people are in the same boat.

Source: StatCounter Global Stats – Platform Comparison Market Share

Given the trajectory we’re on, we’re unlikely to see a drop in the need for developers (of all kinds) any time soon, and the below studies attest to that:

This developer shortage means that now more than ever, we need repeatable ways to build apps that use clean code that’s also easy to maintain.

The following article distills down our experience from multiple projects into a JavaScript framework we now call ‘Modular Architecture’ — and it’s a guideline that both new and more experienced engineers can adopt.

By reading this text, we hope you’ll learn:

  • How DLabs.AI approaches JavaScript
  • Jargon functioning in JS development
  • How to build JS apps with a clear semantic project structure and well-defined data models and flows 
  • How to design an easily readable and testable codebase

At the end of the article, you’ll also find a repository with a working example that uses the guideline, helping you put what you’ve learned to use. And to make sure everyone can follow what we’re talking about…

 We’ll start with a glossary of terms used in this text.

Table of Contents

  • Glossary
  • What is Software Architecture? And Why Does it Matter?
    • Software architecture
    • Software design
  • Modular Architecture: Components
    • Module
    • View
    • Component
    • Service
    • Helper
    • Store
  • Code Structure
  • Components Split
  • Data Flows
  • Naming Conventions
  • Cons of Modular Architecture
  • Pros of Modular Architecture
  • How to Test Modular Architecture
  • See Modular Architecture In Action

Glossary

The part of the program that encodes the real-world business rules determining how data can be created, stored, and changed. Prescribes how business objects interact with one another and enforces the routes and methods by which business objects are accessed and updated. In simple terms: this is the meat in your app.

Components are UI building blocks in the app.

A variable whose value cannot be changed (in the case of a constant object in JS, the reference cannot be changed).

Movement of data through an app.

Fake data which is artificially inserted into a piece of software, often used for testing in isolation.

Organizes elements of data and standardizes how they relate to one another (and to the properties of real-world entities).

A classification that specifies which type of value a variable has — alongside what type of operations can be applied to it without causing an error.

A technique that tests the entire software product from beginning to end to ensure the application behaves as expected.

An application architecture developed by Facebook. It complements composable view components by utilizing a unidirectional data flow.

Place for small, repeatable operations used across the app.

The fundamental element in Modular Architecture that gathers code elements related to a domain in one place.

The code responsible for business logic within a module.

Defines functional and non-functional requirements and high-level technical dependencies.

Defines low-level requirements for the software, including aspects like code structure, data models, UI, and business logic composition.

The place for data management tools (e.g., Redux).

A way of testing a unit (which is the smallest piece of code that can be logically isolated in a system).

A skeletal blueprint or framework that outlines the basic design and functions of a user interface.

The UI component that represents a single page or major route in the application.

What is Software Architecture? And Why Does it Matter?

Let’s kick things off by reviewing two fundamental aspects of engineering: software architecture and software design.

First up, software architecture.

Software architecture

People often confuse software architecture for software design. But these two terms are not interchangeable.

Software architecture helps us see the big picture of a project. It covers the high-level components of the design and how they’ll interact within a project, answering questions like ‘where’ and ‘how’ by guiding:

  • What needs to be done from a business perspective 
  • How these needs translate to technical requirements (infrastructure, data storage, services, etc.)
  • How these technical components will be arranged and connected

Deciding on the above helps us determine the functional and non-functional requirements — alongside the high-level technical dependencies. And with these aspects defined, we have a clearer path forward, which will help us keep the overall quality high.

“In general, software architecture helps us monitor performance, scalability, and reliability.”

— Iryna Deremuk, Modern Web Application Architecture Explained

Software design

On the other hand, software design answers the question of ‘how’ — but it does this at the code level.

In place of considering the big picture, this is where we handle the details, deciding how to build the individual parts of the system, including the project structure, data models, UI, and business logic composition, among other aspects.

— ‘Why is that important?’ you ask. Why can’t we just write code? Well, let’s get some perspective. If you’ve ever tried to assemble a Lego set without instructions, you’ll know how important that piece of the puzzle is. 

The instructions are the part of the design that gives us a universal language to follow: a language that enables multiple parties to work together and create well-defined solutions for known problems.

With a clear architecture and code design, development is much more straightforward. Now, let’s dive into the guidelines for building clean, easy-to-maintain JavaScript apps.

 

Note: Even though you can apply these guidelines to backend Node applications, in this article, we’ll only focus on web and mobile apps that we can create in React and React Native, respectively.

Modular Architecture: Components

First up, we’ll look at the core components.

Module

As the ‘Modular Architecture’ name suggests, a module is fundamental to the framework. A module represents a single responsibility within an application (say, authentication, reporting, or user account management). 

A module often corresponds to an epic. And to allow a module to represent a responsibility, it collects all related items in one place.

View

A view is a simple UI component representing a single page or major route in the application. It serves as a place to bootstrap underlying components, rarely holding any UI logic.

Component

Components are the UI building blocks in the app. They’re small pieces of code representing UI elements, mostly holding the majority of the UI logic.

Service

This is the place for business logic. Each service should serve a specific purpose, and the name of each service should reflect said purpose (the service that handles reports = ReportsService; auth = AuthService; you get the point!).

Helper

Here’s where to put small, repeatable operations used across the app. Something that’s not exactly business logic or UI logic, but that might help them.

Store

We keep all things related to data management in the store. Most of the time, we stick to Redux or ContextAPI (only when building with React) and use it for reducers, actions, and store slice.

The store is optional: you might not need Flux architecture in every app.

Now, let’s move on to the code structure and learn how you’ll put each of these components to good use.

Code Structure

A module collects all related items in one place. That said, it still manages to emphasize the division of responsibility within the module.

After all, we want to keep the UI, business logic, and data management separate. And the element that helps us achieve this separation is the code structure, so let’s now take a look at a project that benefits from this modular design.

Structure of an example project grouped by modules

/Project root
|-ui (global UI components)
|-[...] (other directories)
|-modules
 |-ExampleModule
   |-components (UI components related only to the module)
   |-constants
   |-store (place for data management tools)
   |-helpers (place for minor, reusable functions)
   |-mocks (place for data mocks)
   |-services (module’s business logic)
   |-types (module’s type declarations)
   |-views

Structuring a project as above improves the semantics, making it easier to position new elements and look for existing code pieces. 

In our experience, it’s also offered better unit test coverage. That’s because the business logic was well organized and separated from the UI logic.

Components Split

The approach to use depends on whether we have UI designs available.

Assuming we do, we can use them as a base to draw boundaries between components and decide what belongs where (which is also helpful when verifying if we should split the underlying user story into correct pieces and define data models).

Let’s take the wireframe of the reports feature as an example.

Knowing that a module should gather related items and the expected code structure, we could draw certain boundaries.

 

This yields in the following module:

|-ReportsModule
  |-components
  | |-GenericReport
  | | |-Controls
  | | |-Settings
  | | | |-Filters
  | | | |-Sorting
  | | | |-Metrics
  | | |-ReportTable
  | |   |-Entry
  | |-SavedReports
  |   |-[...]
  |-constant
  |-store
  |-helpers
  |-mocks
  |-services
  | |-ReportsService
  |-types
  |-views
    |-ReportsView

 

We can use the boundaries drawn on the wireframe and a view of what the data might look to determine an initial data model for a future contract between the backend and the frontend.

The below is the data model created from the boundaries drawn on the wireframe.

interface Settings {
 filters: string[];
 sorting: string[];
 metrics: string[];
}

interface Item {
 id: number;
 name: string;
 property: string;
}

interface Report {
 settings: Settings;
 items: Item[];
}

 

Where UI designs aren’t available (or applicable), we can define similar boundaries, structure, and data models using business needs as expressed in a user story.

Data Flows

We’ve touched on data models; now’s the time to talk about them.

I’ve seen the UI manipulate the data in many projects I’ve worked on. This often introduces unnecessary complexity to a project with multiple negative consequences. 

Having UI manipulate data calls for business logic inside the UI, breaking the separation of responsibilities. It also introduces another source of truth within the application and makes the logic inside the UI barely testable. 

You might say, ‘So what?’ — but just wait till you have to debug, test, or maintain an app built in this way.

So please trust me when I say: Business logic that lives in the services should never be mixed with the UI logic. UI simply takes care of rendering the data to the user. Which is why when using modules, we keep the data flow unidirectional (see Flux architecture). 

Views and components can only request an action to be executed by a service, and the process is entirely transparent to the UI. 

Services and helpers take care of the operation (API communication, data processing, pushing data to a store), then a store manager provides new data to the UI.

In the above image, the solid line shows the data flow.

Thanks to this approach, we gain a single source of truth and solid separation of concerns between the UI, business logic, and data handling. 

The UI only ever consumes data, never processing it. And if the code doesn’t need flux (say, in a tiny web app or backend service), you don’t have to use a store management tool at all until the business logic lives only within the services.

Naming Conventions

As you’ve probably noted, we like to have our code easily readable and semantically meaningful, which is why we agreed on a set of naming conventions. 

But what you see below is only a baseline. And it’s not written in stone. Every team can adapt it to fit their purpose. And once you have the changes documented, your team should stick to the established rules.

It’s worth noting: we apply these conventions to our React, React Native and Node apps. That said, they’re not a mandatory aspect of the Modular Architecture approach.

While if you have your own conventions, they’ll also work.

  • Classes, views, helpers, store files, mocks, etc. use PascalCase with the suffix pointing to the role (MainView.tsx, MainStyles.ts, MainModel.ts, and so on). The only exception of the suffix rule are UI components.
  • Classes, variables, and functions are named to indicate their purpose (we favor readability over the name length)
  • Variables serving as flags should have the ‘is’ prefix (e.g., isVisible)
  • Functions serving as event handlers should have the ‘handle’ prefix (e.g., handleOptionSelection)
  • Actions handling components should expose them with the ‘on’ prefix (e.g., onClick)
  • Classes, components, and modules should be named as a singular noun
  • Functions should be named as a verb

Cons of Modular Architecture

Right, so we’ve got this far, and you’re probably thinking, ‘What’s the catch?’ 

I can openly admit: there are some disadvantages to the modular approach, which I have summarized below:

  • The import path can get long (which can be annoying)
  • The config is slightly more confusing than you might expect
  • The prep means it can take a little longer to actually start coding

Pros of Modular Architecture

Despite the cons, we’ve built multiple apps using our Modular Architecture guidelines, and while it takes additional effort and has some disadvantages, the positives easily outweigh the negatives.

Just take a look and see what you’ll get:

  • A clear and readable file structure
  • A clear separation of business and UI logic
  • Simpler testing of business logic and UI
  • Piece together applications like Lego
  • No concerns about unexpected dependencies
  • A single source of truth
  • Spot overloaded components

And to top it all off — the separation of the business logic means you can share the app across mobile and web applications (assuming they both use JavaScript).

How to Test Modular Architecture

If you felt like this detail was missing, rest assured: I deliberately left testing to last. 

That’s because the subject is way too important to sit hidden amidst the other content; it requires its own section.

In fact, I’m going to write an in-depth article on our testing approach alone. But for now, I’ll simply share a quick summary. Basically, we write two types of tests: unit tests — and end-to-end tests. 

  • Unit tests cover business logic using Jest (the minimum code coverage is 80% for most projects; however, with Modular Architecture, 100% isn’t uncommon);
  • End-to-end tests cover the entire product (just to be sure the app behaves exactly as expected).

No great architecture can thrive without a robust testing structure, which is why we invested time in building one for Modular Architecture.

See Modular Architecture In Action

There you have it: this is how DLabs.AI codes JavaScript. 

To help you better understand the principles, you can experience our working example, which is a slice of our Kalkulator WBT app.

I even challenged myself to reuse the business logic across mobile and web, so have fun exploring the repo, and please — do share your thoughts. And if you have an idea on how we can improve the framework or use it in other scenarios.

Drop me a message.


P.S. If you are curious about the photo at the top of the article – yes, it’s me on the right. In DLabs.AI, we have no dress code, so we can work even in pyjamas. If this suits you, check out the open positions and join us.
P.P.S. And more seriously – this is a photo from our company integration 🙂

Previous Post

Durand Cup 2023: East Bengal hopes for semifinal spot against Gokulam Kerala FC

Next Post

Liverpool’s Salah targeted by Saudi Pro League club Al-Ittihad – reports

Next Post
Liverpool’s Salah targeted by Saudi Pro League club Al-Ittihad – reports

Liverpool’s Salah targeted by Saudi Pro League club Al-Ittihad - reports

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • All
  • Sports

FIFA set to approve reintegration of Russian youth football teams amid Ukraine war: Reports

by bdma
October 4, 2023
0

FIFA is set to approve the reintegration of Russian youth teams into under-17 competitions and ease a total international ban...

Iran agrees with Saudi Arabia to reschedule Asian Champions League match after walkout

Iran agrees with Saudi Arabia to reschedule Asian Champions League match after walkout

by bdma
October 4, 2023
0

Iran said on Wednesday it has agreed with Saudi Arabia to reschedule an AFC Asian Champions League match after the...

UEFA accepts Italy-Turkey co-host bid for Euro 2032, leaves UK-Ireland as only candidate for 2028

UEFA accepts Italy-Turkey co-host bid for Euro 2032, leaves UK-Ireland as only candidate for 2028

by bdma
October 4, 2023
0

UEFA has all but confirmed its long-expected host for the European Championship in 2028 and 2032 on Wednesday by formally...

ISL 2023-24, Bengaluru FC vs East Bengal LIVE Streaming info: When, where to watch Indian Super League?

ISL 2023-24, Bengaluru FC vs East Bengal LIVE Streaming info: When, where to watch Indian Super League?

by bdma
October 4, 2023
0

East Bengal will look to continue its winning momentum when its coach Carles Cuadrat gears up to face his former...

Manchester United can still reach Champions League knockout phase: Ten Hag

Manchester United can still reach Champions League knockout phase: Ten Hag

by bdma
October 4, 2023
0

Manchester United is still in the running for a place in the Champions League round of 16 despite Tuesday’s 3-2...

Arsenal suffers double blow with Saka injured in loss to Lens

Arsenal suffers double blow with Saka injured in loss to Lens

by bdma
October 4, 2023
0

Arsenal manager Mikel Arteta said he is worried about Bukayo Saka’s availability for Sunday’s league game against Manchester City after...

Watch: Gazan recycles tyres to help maintain local football pitch

Watch: Gazan recycles tyres to help maintain local football pitch

by bdma
October 4, 2023
0

Can used car tyres help maintain a football pitch? Yes, if you ask 27-year-old Gazan Madian Helles.

Nobody wants to face Newcastle, says PSG manager Luis Enrique

Nobody wants to face Newcastle, says PSG manager Luis Enrique

by bdma
October 4, 2023
0

Paris St Germain coach Luis Enrique said no one wanted to face Newcastle United in the Champions League because of...

Van Gaal returns to struggling Ajax as adviser

Van Gaal returns to struggling Ajax as adviser

by bdma
October 4, 2023
0

Ajax Amsterdam has hired its former head coach Louis van Gaal as an adviser, the Dutch club said on Tuesday,...

UEFA Champions League: Thuram scores to help Inter beat Benfica 1-0

UEFA Champions League: Thuram scores to help Inter beat Benfica 1-0

by bdma
October 4, 2023
0

Marcus Thuram made his mark in the Champions League to help Inter Milan get its campaign up-and-running with a 1-0...

UEFA Champions League: Wahi stars with goal and assist as Lens beats Arsenal 2-1

UEFA Champions League: Wahi stars with goal and assist as Lens beats Arsenal 2-1

by bdma
October 4, 2023
0

Striker Elye Wahi scored one goal and expertly created the other as Lens rallied to beat Arsenal 2-1 in the...

The Evolution of Digital Banking: Banco Bradesco Sa’s Approach

Open Text Stock Offers Potential for Long-Term Investors

by bdma
October 4, 2023
0

Open Text (TSX:OTEX), a Canadian information management company, experienced a significant gain in its stock price in the first half...

Virat Kohli is the WT20 man of the tournament

Virat Kohli is the WT20 man of the tournament

by bdma
October 4, 2023
0

Virat Kohli was awarded the man of the tournament for his brilliant performance in the ICC World Twenty20 that concluded...

Samuels fined for using abusive language

Samuels fined for using abusive language

by bdma
October 4, 2023
0

Hero of West Indies’ World T20 title triumph, Marlon Samuels was fined 30 per cent of his match fee for...

Sammy: U-19, women’s team inspired us to do well

Sammy: U-19, women’s team inspired us to do well

by bdma
October 3, 2023
0

Victorious West Indies captain Darren Sammy said that it was the "disrespect" from its own cricket board (WICB) that made...

At every stride, Windies found a new hero

At every stride, Windies found a new hero

by bdma
October 3, 2023
0

The West Indies forged a winning path upon the brick and mortar of inherent talent and a raging anger to...

Stock Market Falls as Dow Jones Drops Over 400 Points

Major Online Gaming Companies Face GST Notices Totaling Rs 49,000 Crore

by bdma
October 3, 2023
0

The Directorate General of Goods and Services Tax Intelligence (DGGI) Mumbai Zone has sent notices to Dream11 and Play Games...

Highlights: West Indies snatch dramatic World T20 win

Highlights: West Indies snatch dramatic World T20 win

by bdma
October 3, 2023
0

Highlights: West Indies snatch dramatic World T20 win

The Early Beginnings of AstroNova Inc

Beta Value, Trading Volume, and Analyst Recommendations

by bdma
October 3, 2023
0

ASE Technology Holding Co.Ltd ADR (ASX) is a technology company listed on the New York Stock Exchange (NYSE). The stock...

Cristiano Ronaldo sparks fightback as Al-Nassr given Asian Champions League scare

Cristiano Ronaldo sparks fightback as Al-Nassr given Asian Champions League scare

by bdma
October 3, 2023
0

Cristiano Ronaldo was on target as Al-Nassr survived a scare in the Asian Champions League on Monday with the Saudi...

ISL 2023-24: Bengaluru will bank on home support against East Bengal and hope to return to winning ways

ISL 2023-24: Bengaluru will bank on home support against East Bengal and hope to return to winning ways

by bdma
October 3, 2023
0

Following two defeats in its opening two games, Bengaluru FC will look to get back to winning ways when it...

  • Business
  • Knowledge
  • Sports
  • Travel
  • Technology
  • Trending
  • Entertainment

No Result
View All Result
  • About Us
  • Contact Us
  • Disclaimer
  • GDPR Policy
  • Home
  • Privacy Policy