Dry code
- 2021年2月12日
- 技術情報
Today I would like to make a short introduction about dry code.
What is dry code
There is a principle in programming called DRY, or Don’t Repeat Yourself. It usually means refactoring your code by taking something done multiple times and turning it into a loop or a function. The DRY code is easy to change, because you only have to make any changes in one place.
Advantages of DRY
Maintainability
The biggest benefit of using DRY is ease of maintenance. If the logic of checking permissions was repeated throughout the code, it is difficult to troubleshoot problems that occur in the repeated code. When you fix a problem in one, you can easily forget to fix the problem in other cases. Also, if you have to modify the logic, you have to copy and paste everywhere. By having non-repeating code, you just have to keep the code in one place. New bug and logic fixes can be made in one place instead of many. This leads to robust and reliable software.
Readability
Most of the time, the DRY code is more readable. This is not due to the DRY principle itself, but rather the extra effort that the developer put into the code to make it follow certain principles like DRY.
Re-use
DRY inherently promotes code reuse because we are merging 2 or more instances of repeating code into a single code block. Reusable code pays off in the long run as it speeds up development time.
Cost
If management needs to be convinced to spend more time improving code quality, this is it. More code costs more. More code requires more time to maintain and fix bugs. More time to develop and more mistakes leads to a very unhappy customer.
Tests
We are talking about unit testing and integration testing here, not manual testing. The more routes and functions you have to cover with the tests, the more code you will have to write for the tests. If your code doesn’t repeat, you just have to test one parent path.
By Yuuma.
yuuma at 2021年02月12日 07:27:55