4 simple ways to start achieving DRY in React

4 simple ways to start achieving DRY in React

The DRY definition

The DRY principle is a simple concept, stands for "DON'T REPEAT YOURSELF". This concept has aims simplify the development with reusing block of codes throughout your application.

Advantages

Make it easier to maintain. By reusing functions, it is less difficult to change the logic. We only need to make changes in one place, and the entire application will update accordiling.

Make it easier to read. The codebase will be less redundancy, so the reading is more fluid.

However, be sure the functions and abstractions will facilite your life. Functions and abstractions that not can be reusing will be a shot in your foot (as we said in Brazil, when you do something that is harmful to yourself).

The opposite of DRY

The opposite of DRY is WET, right ? So, WET can standfor "We Enjoy Typing", "Write Every Time" or "Waste Everyone’s Time". When rewrite the same things in different modules, we are not reusing anything, only copying and pasting the same logic.

Therefore, the code will be a mess. The difficulty of reading the code increases, maintenance becomes much harder. Imagine if the logic need to change, you would need to rewriter in throughout the intire codebase because you did not follow de DRY principle.

So, you need to stay DRY, not WET.

4 simple ways to start achieving DRY in React

Start with the obvius, make Components

Components are the core of React. Components are designed for reuse in your application. A good component can be reuse a cross your basecode, facilitating maintenance and the readbility.

Again, make sure that your component can be reused, otherwise,your application will several similar components that cannot be reused.

Custom hooks

Instead of repeating the same hook in all your new components, create a custom hook and optimize your time! I Here you get an ideia of how create a custom hook. Check it out.

Helpers functions

Helpers functions are a way to reuse common tasks that arise in a development. For exemple, maybe you need a function that transforms a date format. It is a common task that any developer mighty need use. So, create a function and export for all can use.

Styling Components

Styling with ".css" is a simple way to duplicate a lot of styling logic throughout your codebase, for exemple. So, we have many ways to reduce duplication. One of these is to use styling libraries, such as Tailwind CSS, and create a proper theme to decrease CSS logic redundancy.

Conclusion

The DRY principle is a simple concept, but it is not always easy to apply in real life. Normally, an application has many developers, so we need clear rules that everyone can follow to create an ecosystem that is easier to maintain. So, what do yout think about DRY ?

Comments and tips are welcome. See you soon.




SUMANTH M

Frontend developer @gravityer

7 个月

Helpful

Danilo Pereira

Mobile Engineer | React Native Developer | React | TypeScript | JavaScript | Mobile Developer | Node

7 个月

Great point! The DRY principle is very important for code quality. ??

要查看或添加评论,请登录

Otavio Soares的更多文章

  • BEM Methodology: Simplifying CSS

    BEM Methodology: Simplifying CSS

    Have you ever experienced the chaos of a project where modifying the CSS felt like navigating a maze? You try to tweak…

    3 条评论
  • Using Open/Closed Concept in hooks

    Using Open/Closed Concept in hooks

    SOLID is a design pattern commonly used in various programs. Some developers think that the concepts from SOLID can…

    2 条评论
  • The Prop Drilling Problem

    The Prop Drilling Problem

    React is a great framework for building larger applications using scalable, reusable, and maintainable components…

  • Creating a Simple Toast Component in React with Custom Hooks

    Creating a Simple Toast Component in React with Custom Hooks

    But first, what are Custom Hooks? A Custom Hook is a technique that allows you to encapsulate a component's logic into…

    3 条评论
  • Context Module Pattern

    Context Module Pattern

    Particulamente, gosto de aprender alguns padr?es de desenvolvimento pois na maioria das vezes sempre s?o solu??es que…

  • Porque evitar utilizar muitos re-exporting ?

    Porque evitar utilizar muitos re-exporting ?

    Publica??o incentivada pela eNe Soluc?es no nosso pilar Never Stop Learning. Você já deve ter visto algum arquivo que a…

社区洞察

其他会员也浏览了