Code checklist
Overview
- Is your code deployed to a lower environment and ready to test?
- Is the feature easy to test, if not, did you add instructions?
- Does the code run without errors? (Including console errors)
- Does the feature meet all requirements in the ticket?
- Does it look good on all screen sizes and dark/light mode?
- Is the markup minimal and only necessary elements are added to the DOM?
- Remove all extra nested wrapper elements that serve no purpose
- Optimise your markup by using the correct MUI element
- If building a custom markup make refer to the HTML5 docs for correct usage
- When adding custom styling
- Are all added CSS atributes valid
- Are all added styles applied? (if not remove unused styles)
- DO NOT target core MUI classes directly
- When adding layout styles prefer using the
sxprop - Use styled components rather than
JSS
- Make sure no dist files, editor/IDE files, etc are checked in
Performance
Page load times (both real and perceived) should be a key consideration for users of all browsers and device types.
There are some general things we can do in front-end development:
- Send fewest bytes possible down the wire
- Avoid unnecessary use of display: none;
- Minimise HTTP requests
- Minimise blocking – content should be readable before client side processing
- Lazy load 'supplementary' content (especially images)
- Use existing MUI components, or more specifically use the right MUI component for the use case
- If building your own custom markup refer to the HTML5 docs and pick the correct element
- Avoid adding unnecessary markup as it leads to unexpected layout shifts and also has a performance impact when re-rendering
- Review your components DOM tree and accessibility tree in the browser (remove any unneeded elements)
- Review all the CSS you added, check that all attributes and values are valid (remove any unused styles that might have been added while trying to make something work)
- Build your markup with a Mobile first approach
Testing
- Tests are readable, maintainable, trustworthy.
- Test names (describe, it) are concise, explicit, descriptive.
- Avoid logic in your tests.
- Don't test multiple concerns in the same test.
- Cover the general case and the edge cases.
- Test the behaviour, not the internal implementation.
- Are there unit tests for crucial parts of the feature?
- Remove any unnecessary tests. For example:
- Tests that test React framework (or other frameworks)
- Tests that test static data
- Use the right render for tests
- Components that don't use the
AuthContextdon't need theAuthProviderwrapper - Components that don't add custom styling don't need the
ThemeProviderwrapper - Components that don't use the
UserPreferencecontext don't need theUserPreferencesProvider
- Components that don't use the
- Keep your tests simple and DRY
- Testing user input is time consuming so try not to repeat it in each test case
- Testing these using the
userEventlibrary which mimics user actions better thanfireEvent
- Check your tests stats
- How long does it take to run your test?
- For example it takes 10s to run it, but in the stats only 4s of that are attributed to the actual tests. This means you might be renderring more than what your test requires (consider a different render method, remove providers like Auth, Location, Theme if not required)
Naming
- Do filenames have consistent casing and extension?
- Do variables/functions/modules have consistent casing?
- Do variables/functions/modules have descriptive names?
Code
- Look for blocks of code with more than a few lines of code that look similar
- Is it possible to refactor to reduce duplication?
- Simplify “too smart” and over-engineered code
- Remove unused/unreachable code
- Remove commented out code
- Remove console.logs (unless you have strong motivation why you need it)
- Remove unnecessary comments: comments that describe the
how - Add necessary comments where needed. Necessary comments are comments that describe the
why - Use direct imports instead od barrel imports
Don't use barell imports
This applies to any imports not just MUI and should be avoided
/* Components */
import { Box, Grid, Typography, Divider } from '@mui/material/Divider';Use direct imports
This prevents loading any unnecessary bundles in our code, especially when running tests where vitest doesn't utilise code splitting
/* Components */
import Box from '@mui/material/Box';
import Grid from '@mui/material/Grid';
import Typography from '@mui/material/Typography';
import Divider from '@mui/material/Divider';ES6
- Can you use spread operator?
- Can you use destructuring?
- Change all var to let or const (prefer const)
- Be consistent in your usage of arrow functions
React
- Minimize logic in the render method. Logic should be in helper methods, container components or in Redux actions
- Move all code with side effects (for example api calls) out from the render methods
- Use
UseEffectsparingly. If the component has more than 2UseEffectblocks consider a refactor useMemoanduseCallbackshould be used with consideration as they are only useful duringre-rendersand DOM rehydration (use them to prevent unnecessary re-renders)- Check if you can split up React components to reduce duplication.
- React components should be small and follow single responsibility principle
- Destructe props
- Use functional components consistently
- Make sure you don’t edit props within a component (they are read-only)
Folder structure
When adding new or refactoring existing component please align the folder structure to the following
└─ MyComponent
├─ MyComponent.jsx
├─ MyComponent.styles.jsx
└─ __test__
├─ MyComponent.test.jsx
└─ MockData.jsMUI
- Use MUI components with their standard styling and avoid customising them outside of the providsed api and props
- For styling use styled components in a dedicated
.styles.file - Avoid directly targetting
.Muiclasses when ever possible - Use the sx prop for inline styles mainly to adjust layout and no complex custom styling
- Don't use
headingelements directly, instead use the MUI Typography component with the correctvariantandcomponentattributes - Avoid adding custom colors to the theme, instead use what already exists or even better use the default theme colors
Redux
Prefer Redux Toolkit for new/changed state
Much of the store below is still hand-rolled Redux. Redux Toolkit (createSlice) is the preferred approach for new or changed state — reach for it before writing a new hand-rolled reducer like the ones described here.
- Each api request must have
pending, error, successstates
- Provide a reset state action that can be dispached to set specific state back to
initialState- Example:
fetchInvoicesapi call fails. Theinvoices.errorstate is set - The FE can receive this error, handle it by showing an alert then navigating away from to the start of the flow
- At this point the reset invoices action is dispatched so the user can start again
- Example:
- Make sure you don’t do state mutations in reducers
Don't
A reducer like this is wrong because it mutates the state
const todos = (state = [], action) => {
switch (action.type) {
case 'ADD_TODO':
// Wrong! This mutates state
state.push({
text: action.text,
completed: false
})
return state
case 'COMPLETE_TODO':
// Wrong! This mutates state[action.index].
state[action.index].completed = true
return state
default:
return state
}
}Do
It needs to be written like this
const todos = (state = [], action) => {
switch (action.type) {
case 'ADD_TODO':
// Return a new array
return [
...state,
{
text: action.text,
completed: false
}
]
case 'COMPLETE_TODO':
// Return a new array
return state.map((todo, index) => {
if (index === action.index) {
// Copy the object before mutating
return Object.assign({}, todo, {
completed: true
})
}
return todo
})
default:
return state
}
}