JavaScript Code Style
ScandiPWA follows a strict JavaScript style guide for maintainability and consistency
Code style recommendations in ScandiPWA consist of two main categories: functional programming, which is enforced to make the codebase easier to maintain, and ScandiPWA best practices, which have been implemented to guarantee that code is extensible, both by overriding the theme and writing plugins.
We strongly recommend you use ESlint to check your code style. This article was written to help you understand the code style rules we enforce and write better code.
Writing Maintainable Code
Keep Functions Short
Functions should do only one thing, and do it well. If you notice that a function has become longer than necessary, consider breaking it up into parts. Not only will this make your codebase easier to navigate and manage, but it will also make your theme easier to extend via plugins and theme overrides.
This is especially relevant when writing functions that return JSX. Breaking them down into multiple functions can reduce nesting, improve readability, and make them easier to extend.
Avoid writing long functions such as this:
Issues:
Code is harder to understand and navigate
Re-ordering or re-using sub-components requires a lot of changes
Instead, try breaking your code into smaller functions:
Advantages:
Each function has a clear responsibility and is easy to change
The structure of the resulting JSX is more clear
The component is more extensible via plugins and overrides
Separate Concerns
Containers should be responsible for business logic, and components should be responsible for presentation logic. Making this distinction will make it easier to structure your code.
Use Destructuring
ScandiPWA prefers destructuring all required variables at the beginning of a function over direct field access, as it offers several benefits:
More concise code with reduced repetition if the same value is used multiple times
Ability to provide default values
By moving destructuring to the first line of each function, the dependencies of that function are clear
Use Meaningful Names
To make code easier to understand, avoid generic names such as x
or abbreviations such as prdct
. Give meaningful names that describe what the variable/function/class is for.
Avoid Magic Numbers
It can be tempting to pass a hard-coded literal value to a function:
However, the purpose of the number 300
is not clear, and might confuse a developer looking at this for the first time.
Instead, consider creating a constant to describe the meaning of the value:
Advantages:
The intention of the code is clear, and the math makes sense
ANIMATION_DURATION
can be easily reused if neededThe duration can be adjusted without worrying about breaking something
Functional Programming
Functional programming aims to make code easier to reason about and maintain by avoiding mutable values. It can make your codebase easier to navigate as well as more concise and elegant.
Avoid let
; Use const
let
; Use const
Use const
for every variable and do not reassign values.
Avoid let
and reassigments:
Potential problems that can occur as the codebase grows and price
is used more times:
price
can have multiple meanings; a developer looking at line 1 might miss the reassignment and assumeprice
is a numberThe type of
price
can change and can get hard to guessAny code needing the original value of price can't get it
Instead prefer:
Benefits:
You are forced to give a meaningful name to each variable, making your intentions more clear
The values are immutable and easier to reason about
Any previous value can be re-used if necessary
Avoid Loops
In functional programming, loops are discouraged in favor of iterative functions that signal intent better. In addition, they are often elegant and concise.
Avoid using a loop to combine the array's elements:
Instead prefer reduce
:
Advantages:
More concise and elegant
Avoids an imperative loop and a mutable value
If you need to transform the values of the array into different values, use map
Avoid using a loop to transform an array:
Advantages:
More concise and elegant
Intentions are clear - this is a typical use of
map
In general, you should be able to write code in JavaScript without needing to use loops at all. Following this practice will result in cleaner and more maintainable code.
Working with Arrays
Creating a new array by transforming each item
Reducing the array to 1 value
Copying part of the array
Finding an item in the array
Reordering the array
While sort
and reverse
return the re-ordered array, they also change the original array. For this reason, they are not ideal from the functional programming perspective.
Most of the time, mutating the original array might be acceptable. If you do not wish to mutate the array, create a copy of it and re-order the copy instead.
Adding an item to the array
Instead of mutating the array, you can create a new array with the additional value:
Removing an item from an array
Instead of mutating the array, you can create a new array with the value removed, using filter
:
Extensibility Best Practices
In addition to using functional programming, ScandiPWA also recommends that you follow certain guidelines to ensure that your code can be easily extended by plugins and theme overrides.
Export Everything
Add Namespaces
Plugins can only affect values that have namespaces. For this reason, it is highly recommended that you add a namespace to all functions and classes:
Namespaces should consist of:
The alias of the directory (Component/Store/Route, etc)
The name of this component (Checkout in this case)
The current file's responsibility, if applicable (Component/Container for components, Dispatcher/Reducer for stores)
The name of the target function/class, or some other meaningful name if it is anonymous
ScandiPWA Conventions
Follow the File Structure
One Class Per File
Each file should define at most one class. Adding additional classes can make the codebase harder to navigate.
Last updated
Was this helpful?