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
Don’t Repeat Yourself的更多相关文章
- mysql while,loop,repeat循环,符合条件跳出循环
1.while循环 DELIMITER $$ DROP PROCEDURE IF EXISTS `sp_test_while`$$ CREATE PROCEDURE `sp_test_while`( ...
- MySQL中的while、repeat、loop循环
循环一般在存储过程和存储函数中使用频繁,这里只给出最简单的示例 while delimiter $$ create procedure test_while() begin declare sum i ...
- MySQL-procedure(loop,repeat)
在 MySQL-procedure(cursor,loop) 中将spam_keyword表中的文字全部分割到t表当中,且每一行的字都不重复,那t表可以用来当作一个小字典,只有1000来个字符,这次把 ...
- Shell 语法 if 、 case 、for 、 while、 until 、select 、repeat、子函数
if语法 : if [ expression ] then commandselif [ expression2 ] then commandselse commandsfi ...
- unity mathf.repeat 截取操作
截取操作,可用于浮点数. Mathf.Repeat(Time.realtimeSinceStartup, 3*blinkTime) > blinkTime;
- repeat语句
一.repeat语句格式repeat语句用于"重复执行循环体,直到指定的条件为真时为止" repeat语句格式:repeat 语句1; 语句2; -- 语句n;until ...
- freeCodeCamp:Repeat a string repeat a string
重复一个指定的字符串 num次,如果num是一个负数则返回一个空字符串. /*思路 fo循环将字符串重复num次并组成数组 将数组组成新的字符串并返回 */ function repeat(str, ...
- 从js的repeat方法谈js字符串与数组的扩展方法
js将字符串重复N次的repeat方法的8个版本 /* *@desc: 将一个字符串重复自身N次 */ //版本1:利用空数组的join方法 function repeat(target, n) { ...
- HDU 4342History repeat itself 数学
C - History repeat itself Time Limit:1000MS Memory Limit:32768KB Description Tom took the D ...
- [转]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 ...
随机推荐
- 区别JS和DOM对象
<div> <button id="bt" onclick="ChangeColor()">Clike To Change Color& ...
- C#字符串操作,转自韩迎龙博客
1.1 字符串大小写 方法原型 string <strName>.ToUpper(); //返回字符串转换的大写形式 string <strName>.ToLower( ...
- windows管理员利器之用Log Parser Studio分析IIS日志(附逐浪CMS官方命令集)
原文:windows管理员利器之用Log Parser Studio分析IIS日志(附逐浪CMS官方命令集) Log Parser Studio是一个强大的IIS图形分析工具,值得推荐. 1. 安装L ...
- C# Oracle数据库操作类
using System; using System.Data; using System.Collections.Generic; using System.Configuration; using ...
- ARTS 12.31 - 1.4
Algorithm 这是一道需要用动态规划的问题.求字符串的最长回文子序列. 复习了一遍动态规划,重点是要分析出最优解所包含的子问题的最优解,把过程描述为数学公式. 题目https://leetcod ...
- C# 查农历 阴历 阳历 公历 节假日
原文:C# 查农历 阴历 阳历 公历 节假日 using System;using System.Collections.Generic;using System.Text; namespace ca ...
- 二叉树基本操作C代码
#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct ChainTree) struct ChainTr ...
- UWP入门(三) -- StackPanel与Grid的区别
原文:UWP入门(三) -- StackPanel与Grid的区别 ##1.Grid 下布局 <Grid Background="{ThemeResource ApplicationP ...
- 如果您想确保Windows 10在新用户登录时不安装内置应用程序,则必须删除所有配置的应用程序。
原文 如果您想确保Windows 10在新用户登录时不安装内置应用程序,则必须删除所有配置的应用程序. 本文的内容 已安装与配置的应用程序 删除配置的应用程序 安装与配置的应用程序^ 在介绍如何删除所 ...
- 【canvas】基础练习一 图形
Demo1[绘制一条线] <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...