Don’t Repeat Yourself

DRY原则强调通过抽象消除逻辑重复,并通过自动化消除过程重复。本文深入探讨了如何使用设计模式来重构条件语句,减少代码中的冗余,提高软件质量。

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

转载于:https://www.cnblogs.com/luchuanbing/p/7920417.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值