Don’t Repeat Yourself

The Don’t Repeat Yourself (DRY) principle states that duplication in logic should be eliminated via abstraction; duplication in process should be eliminated via automation.
Duplication is Waste
Adding additional, unnecessary code to a codebase increases the amount of work required to extend and maintain the software in the future.

 Duplicate code adds to technical debt.  Whether the duplication stems from Copy Paste Programming or poor understanding of how to apply abstraction, it decreases the quality of the code. 

Duplication in process is also waste if it can be automated.  Manual testing, manual build and integration processes, etc. should all be eliminated whenever possible through the use of automation.

Suspect Conditionals

Often, if-then and switch statements have a habit of being duplicated in multiple places within an application. 

It’s common in secured applications to have different functionality available to users in certain roles, so the code may be littered with if-user-is-in-role checks. 

Other applications may have been extended to deal with several similar but distinct kinds of data structures, with switch() statements at all levels of the workflow used to describe the differences in behavior each data structure should have. 

Wherever possible, refactor these conditionals using well-known design patterns to abstract the duplication into a single location within the codebase.

Related Principles

Once and Only Once can be considered a subset of the DRY principle.

The Open/Closed Principle only works when DRY is followed.

The Single Responsibility Principle relies on DRY.

Resources

SOLID and DRY Principles of Object Oriented Design on Pluralsight (includes 2 modules on DRY)
Don’t Repeat Yourself from 97 Things Every Programmer Should Know

Principles of Object Oriented Design course from Pluralsight

2016 Software Craftsmanship Calendar

posted on 2017-11-29 14:37  鲁传兵  阅读(319)  评论(0编辑  收藏  举报