Front End Standards
These guidelines, principles and standards allow us to:
- Produce code of a consistent quality across all projects we undertake
- Work concurrently with multiple devs on the same codebase at the same time in the same way
- Produce code that is less prone to bugs and regressions, is easier to understand and debug
- Write code that supports re-use.
It is required reading for all devs (internal and external) working on a LUNA development project.
First, the essential philosophy:
Work under the assumption that you could be passing on the project to someone else very soon — or you could be maintaining it forever.
And then code for the maintainer.
I should be able to pass on any of my work at a moment's notice, and not have to explain anything to anyone (because it's already documented), and not need to apologise to anyone (because it's already well-written and maintained).
Write code to be read
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. - Brian Kerninghan.
Follow the principles of Keep It Simple, Stupid (KISS) hard to read or obfuscated code is difficult to maintain and debug. Don't be too clever; write code to be read. Follow existing patterns and if something is unclear ask questions.
Code is a craft - make it your responsibility to ensure it is the best it can be; that it's tested, bug free, and adheres to these guidelines. Testing and QA folks aren't responsible for quality - developers are.
Don't use testers as bug catchers - testers should find no problems after you have committed your code. When testers find bugs, tickets need to be opened, developers assigned and scheduled in to fix the problem. This lengthens the time it takes from identifying to resolving a bug.
Make sure, as much as possible, you have tested your code on a reasonable number of devices so you can catch problems before you commit to the repo.
Working with legacy code
These guidelines and standards are not set in stone, this is a living document and things changed over time. For this reason we want to follow a few ground rules.
The boy scout rule: Always leave the campground cleaner than you found it
Or more aligned to our environment Leave the code cleaner than you found it.
When you are refactoring or even just extending existing code make sure you take the time to align it to the latest standards.
The Broken Windows Theory
One broken window, left without repair for a certain amount time, consequently, will leave the impression of negligence. More windows will be broken, graffiti appears, damage in the structure appears and the sense of abandon becomes present. All it takes is just one broken window and everything can crumble.
Every time you find a broken window in your code, just fix it, don’t let it stay there for someone else to find and potentially copy a bad pattern.
There are many ways to apply the boy scout rule to our code, it doesn’t have to be something big. You can rename a variable to a more meaningful name, get rid of a small duplication or break up a long function.
Have you ever imagined working on a project where the code got better over time?
Continuous improvement is an integral part of our job as software developers.
Examples on when to refactor
Sometimes your task is small and it is difficult to justify a large refactor as part of your ticket so here are a few examples that you might encounter.
To keep this simple lets divide into two categories.
My refactor does not change core logic, it introduces consistency and cleans up code maintainability and readability.
Generally in these cases there is a very low chance that any regression will be casued.
Imagine your task is to correct the typo on line 9 and fix srory to be sorry.
import React, { useEffect, useState } from 'react';
import TextField from '@mui/material/TextField';
import Autocomplete from '@mui/material/Autocomplete';
import CircularProgress from '@mui/material/CircularProgress';
import api from '@utils/api/api';
import { Modal } from '@mui/material';
const SomeComponent = () => {
const [errorMessage, setErrorMessage] = useState('We are srory your search has been unsuccessful due to a technical error!');
<>...</>;
};
export default SomeComponent;However as you open the component you spot that it is using a mix of barrel and direct imports for MUI components. Since we prefer direct imports and above all consistency 👀 it is alright to edit this as part of your typo fix and end up with something like the following:
import React, { useEffect, useState } from 'react';
/* Api */
import api from '@utils/api/api';
/* Components */
import Autocomplete from '@mui/material/Autocomplete';
import CircularProgress from '@mui/material/CircularProgress';
import Modal from '@mui/material/Modal';
import TextField from '@mui/material/TextField';
const SomeComponent = () => {
const [errorMessage, setErrorMessage] = useState('We are sorry your search has been unsuccessful due to a technical error!');
<>...</>;
};
export default SomeComponent;All that was done is the typo fix and a general tidy up of the imports, grouping related imports together and using consistent approach (in this case direct imports).
My refactor improves performance, maintainability and readability of the code but also changes/updates some core logic
This means that depending on where and how the component is used, you changes could cause some regression. Remember that there could be a reason why something was done the way it is, could be a UI issue that was prevented, or it could be some different unrelated bug... make sure you thorougly and fully understand the requirements of the feature you are changing, the extend of it's use throughout the codebase and test it in full before raising a merge request.
Important
In this scenario please talk to your PO about adding a ticket and raise a separate MR with those changes alone. Provide sufficient description and reasoning for your refactor and ask for a review from the core team.
