In my years developing front-end applications both individually and as part of a team, there are some practices I’ve noticed which I really dislike. Some are personal preferences while others are also identified by the industry. I’ve developed front-end applications with several frameworks/libraries and these issues arise all the time. However, some of the points mentioned below are peculiar to React even though they could occur with other frameworks. I’ll update this list from time to time to include more points.
Strive to avoid:
- Having Ambiguous/unclear variable names
- Having unused props/state fields
- Having extremely large components – Be modular and encapsulate special logic (separation of concerns)
- Having a very large render() method – Still connects to the principle of modularity. Your render method should be as small as possible. Break down certain aspects of the view into separate components or functions.
- Not having interface definitions for component props or function parameters – It defeats the purpose of using TypeScript or Flow.
- Repeating components during conditional rendering instead of placing the condition in the attribute that changes.
- Code repetition – Use a service to encapsulate common logic.
- Mutating Redux state – VERY DANGEROUS. Always make a deep copy of your redux state before making changes. Read https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns for more information.
- Hard-coding state values for values that ought to change.
- Placing too much logic in the reducer – This makes it difficult to trace what is being updated and where. It’s better to extract API response from your action-creators before passing data to the reducer.
- Using regular CSS files for styling – Use CSS modules or styled-components instead to prevent style conflicts. (I didn’t encounter this issue much in Angular due to the shadow DOM pattern), but still, there are ways to ensure you don’t have style conflicts.
- Updating API request body and Redux store from several components – This makes it hard to trace what is going on. Try to have just 1 single source of truth in your code.
Before pushing your code or creating a pull request, also check to remove:
- Console logs/errors
- Unused methods/functions
- Unused files
- Unused import statements
By adhering to these and other best practices, we gain the following:
- Smaller bundle size
- Increased application speed
- More maintainable code – very useful when debugging
- More predictable app behavior – very useful when debugging
- Better code quality – very useful when debugging