A few ways to DRY code in Rails

Tram Ho

The term DRY is probably not new to developers, and perhaps it is also a software development principle that coders must know because its benefits are not small.

What is DRY?

DRY actually stands for:

Don't Repeat Yourself

In a nutshell, we should not repeat the code
DRY benefits bring:

  • Easy to maintain and expand the code: When we maintain the code, everyone wants to find a single bottleneck to solve an entire issue instead of having to ctrl + shift + F to go through each file to edit or write the same code 😅
  • Limitations of bugs: If we copy a piece of code from one place to another and one day we have to fix all the code that goes everywhere but unfortunately misses any place, why? 😱 ?

For example :

Let's go into a familiar example in rails:

We can see that @micropost = Micropost.find_by id: params

Share the news now

Source : Viblo