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

Don’t Repeat Yourself的更多相关文章

  1. mysql while,loop,repeat循环,符合条件跳出循环

    1.while循环 DELIMITER $$ DROP PROCEDURE IF EXISTS `sp_test_while`$$ CREATE PROCEDURE `sp_test_while`( ...

  2. MySQL中的while、repeat、loop循环

    循环一般在存储过程和存储函数中使用频繁,这里只给出最简单的示例 while delimiter $$ create procedure test_while() begin declare sum i ...

  3. MySQL-procedure(loop,repeat)

    在 MySQL-procedure(cursor,loop) 中将spam_keyword表中的文字全部分割到t表当中,且每一行的字都不重复,那t表可以用来当作一个小字典,只有1000来个字符,这次把 ...

  4. Shell 语法 if 、 case 、for 、 while、 until 、select 、repeat、子函数

    if语法 :   if [ expression ]    then   commandselif [ expression2 ]   then   commandselse   commandsfi ...

  5. unity mathf.repeat 截取操作

    截取操作,可用于浮点数. Mathf.Repeat(Time.realtimeSinceStartup, 3*blinkTime) > blinkTime;

  6. repeat语句

    一.repeat语句格式repeat语句用于"重复执行循环体,直到指定的条件为真时为止" repeat语句格式:repeat  语句1;  语句2;  --  语句n;until ...

  7. freeCodeCamp:Repeat a string repeat a string

    重复一个指定的字符串 num次,如果num是一个负数则返回一个空字符串. /*思路 fo循环将字符串重复num次并组成数组 将数组组成新的字符串并返回 */ function repeat(str, ...

  8. 从js的repeat方法谈js字符串与数组的扩展方法

    js将字符串重复N次的repeat方法的8个版本 /* *@desc: 将一个字符串重复自身N次 */ //版本1:利用空数组的join方法 function repeat(target, n) { ...

  9. HDU 4342History repeat itself 数学

    C - History repeat itself Time Limit:1000MS     Memory Limit:32768KB      Description Tom took the D ...

  10. [转]Using the Group Pane to Repeat Page Titles

    转自:http://www.wiseowl.co.uk/blog/s148/group-pane-advanced-mode.htm Repeating Page Headers in Reporti ...

随机推荐

  1. 【C#】wpf自定义calendar日期选择控件的样式

    原文:[C#]wpf自定义calendar日期选择控件的样式 首先上图看下样式 原理 总览 ItemsControl内容的生成 实现 界面的实现 后台ViewModel的实现 首先上图,看下样式 原理 ...

  2. php延时执行

    sleep(秒) usleep(毫秒) 让它睡上一会.

  3. Qt SizePolicy 属性(每个控件都有一个合理的缺省sizePolicy。QWidget.size()默认返回值是(640, 480),QWidget.sizeHint()默认返回值是(-1, -1))

    控件的sizePolicy说明控件在布局管理中的缩放方式.Qt提供的控件都有一个合理的缺省sizePolicy,但是这个缺省值有时不能适合 所有的布局,开发人员经常需要改变窗体上的某些控件的sizeP ...

  4. .net EF Join 关联表分页查询

    Join中第二个参数和第三个参数即为两张表关联对应的字段 第四个参数new 后面如果不写Model名,系统会自动生成带字段的list类型 IQueryable<Operator> oper ...

  5. 运维不仅仅是Linux,居然还要知道这么多?

    摘要: 运维不仅仅是懂Linux就行,因为还有一大部分的Windows运维,向windows运维人员致敬.当然我们这篇文章不是说运维除了懂Linux,还要懂Windows,而是涉及运维的其他方方面面. ...

  6. Dedecms 中,获取某一栏目所有子栏目

    以前从来没写过递归(其实想想,对算法完全没概念),刚好有这个需求,试着写了一下,发现也挺容易的,特别记录一下. 数据库是dedecms默认的,dede_arctype是保存栏目的表,reid是栏目的父 ...

  7. 不仅仅是百万级TCP长连接框架 t-io

    t-io: 不仅仅是百万级TCP长连接框架 t-io是基于jdk aio实现的易学易用.稳定.性能强悍.将多线程运用到极致.内置功能丰富的即时通讯框架(广义上的即时通讯,并非指im),字母 t 寓意t ...

  8. Ruby元编程:动态添加类属性及其实际应用

    上个星期测试道的Monkey老师和我聊到测试用例参数过多的问题,其实这样的问题在我这里也同样经历过.比如我的测试用例必须面对不同的测试环境,每个环境有无数的参数,开发的最初阶段,因为参数少,所以就放在 ...

  9. java多线程之生产者-消费者

    public class Product { private String lock; public Product(String lock) { this.lock = lock; } public ...

  10. 626. Exchange Seats-(LeetCode之Database篇)

    问题表述 数据库表如下: id student 1 Abbot 2 Doris 3 Emerson 4 Green 5 Jeames 现在要通过SQL语句将表变换成如下: id student 1 D ...