Don’t repeat yourself
The don’t repeat yourself (DRY) principle is a fundamental concept in software development that aims to reduce repetition within the code. Following this principle leads to better maintainability, readability, and testability, and can prevent bugs that occur due to the duplication of logic.
Let’s say that you have a shopping website, and you want to display a list of products and the user’s cart side by side, something like this:
The ProductList
component will display the product’s image, name, and price, with an Add to Cart button, while the Cart
component will display a list of cart items with a Remove from Cart button.
A naive implementation of ProductList
might look like this:
type Product = { id: string; name: string; image: string; price: number; }; const ProductList = ({ products, ...