Don’t Repeat Yourself

Don’t Repeat Yourself

Steve Smith

OF ALL THE PRiNCiPLES OF PROGRAMMiNG, Don’t Repeat Yourself (DRY) is perhaps one of the most fundamental. The principle was formulated by Andy Hunt and Dave Thomas in The Pragmatic Programmer, and underlies many other well- known software development best practices and design patterns. The developer who learns to recognize duplication, and understands how to eliminate it through appropriate practice and proper abstraction, can produce much cleaner code than one who continuously infects the application with unnecessary repetition.

Duplication is Waste

Every line of code that goes into an application must be maintained, and is a potential source of future bugs. Duplication needlessly bloats the codebase, resulting in more opportunities for bugs and adding accidental complexity to the system. The bloat that duplication adds to the system also makes it more difficult for developers working with the system to fully understand the entire system, or to be certain that changes made in one location do not also need to be made in other places that duplicate the logic they are working on. DRY requires that “every piece of knowledge must have a single, unambiguous, authoritative representation within a system.”

Repetition in Process Calls for Automation

Many processes in software development are repetitive and easily automated. The DRY principle applies in these contexts, as well as in the source code of the application. Manual testing is slow, error-prone, and difficult to repeat, so automated test suites should be used where possible. Integrating software can be time consuming and error-prone if done manually, so a build process should be run as frequently as possible, ideally with every check-in. Wherever painful manual processes exist that can be automated, they should be auto- mated and standardized. The goal is to ensure that there is only one way of accomplishing the task, and it is as painless as possible.

??60 97 Things Every Programmer Should Know

?

???????????????Repetition in Logic Calls for Abstraction

Repetition in logic can take many forms. Copy-and-paste if-then or switch- case logic is among the easiest to detect and correct. Many design patterns have the explicit goal of reducing or eliminating duplication in logic within an application. If an object typically requires several things to happen before it can be used, this can be accomplished with an Abstract Factory or a Factory Method pattern. If an object has many possible variations in its behavior, these behaviors can be injected using the Strategy pattern rather than large if-then structures. In fact, the formulation of design patterns themselves is an attempt to reduce the duplication of effort required to solve common problems and discuss such solutions. In addition, DRY can be applied to structures, such as database schema, resulting in normalization.

A Matter of Principle

Other software principles are also related to DRY. The Once and Only Once prin- ciple, which applies only to the functional behavior of code, can be thought of as a subset of DRY. The Open/Closed Principle, which states that “software entities should be open for extension, but closed for modification,” only works in practice when DRY is followed. Likewise, the well-known Single Responsibility Principle, which requires that a class have “only one reason to change,” relies on DRY.

When followed with regard to structure, logic, process, and function, the DRY principle provides fundamental guidance to software developers and aids the creation of simpler, more maintainable, higher-quality applications. While there are scenarios where repetition can be necessary to meet performance or other requirements (e.g., data denormalization in a database), it should be used only where it directly addresses an actual rather than an imagined problem.

时间: 2024-08-25 13:52:43

Don’t Repeat Yourself的相关文章

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

1.while循环 DELIMITER $$ DROP PROCEDURE IF EXISTS `sp_test_while`$$ CREATE PROCEDURE `sp_test_while`( IN p_number INT, #要循环的次数 IN p_startid INT #循环的其实值 ) BEGIN DECLARE v_val INT DEFAULT 0; SET v_val=p_startid; outer_label: BEGIN #设置一个标记 WHILE v_val<=p_

Talbot basically sweating the idea and can be happy for the repeat functionality

Practically no one enjoyed more NHL hockey when compared with Cam Talbot last time. It has not been even shut. The Edmonton Oilers goaltender made 73 appearances, which has been seven over his closest thing competitor, Toronto's Frederik Andersen. Th

JDBC程序执行步骤--repeat

package com.lucia.repeat; import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException; /** * 主要是练习jdbc程序的运行步骤 * @author lenovo * */public class JDBCRepeat {    publ

Repeat Number(数论)

Repeat Number 题目描述: Definition: a+b = c, if all the digits of c are same ( c is more than ten), then we call a and b are Repeat Number. My question is How many Repeat Numbers in [x,y]. 输入 There are several test cases. Each test cases contains two int

SWIFT中的repeat...while

SWIFT中的repeat...while类似于JAVA\.NET中的 do while.大同小异只是把do换成了repeat var index = 10 repeat{ print(index) index-- } while(index>0)

DRY(Don&#39;t Repeat Yourself )原则

凡是写过一些代码的程序猿都能够意识到应该避免重复的代码和逻辑.我们通过提取方法,提取抽象类等等措施来达到这一目的.我们总能时不时的听到类似这样的话:”把这些公用的类放到shared项目去,别的项目还要使用...“,什么算是公用(重复)的代码?是不是公用(重复)的代码就要放到一个叫shared的地方? 为什么说重复的代码和逻辑会带来问题呢? 你从一个类中复制了一段代码到另一个类中,但是这段代码足够的稳定,百年不变,这样的重复会带来问题吗? 也许不会. 如果这段代码需要时不时的修改,那么你就要花时间

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

if语法 : if [ expression ]    then   commandselif [ expression2 ]   then   commandselse   commandsfi case 语法: case string1 in   str1)    commands;;   str2)    commands;;   *)    commans;;esac 循环语句 for 语法:    for  var in list do commands done     在此形式时,

android 图片水平重复平铺(repeat x)

<=用来重复显示的图 1.最简单方式 创建wave_repeat.xml <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/wave" android:tileMode="repeat&q

CQUOJ 10672 Kolya and Tandem Repeat

A. Kolya and Tandem Repeat Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d      Java class name: Any Submit Status PID: 10672 Kolya got string s for his birthday, the string consists of small English letters. He immediately

Longest Substring Without Repeat Characters

1 var lengthOfLongestSubstring = function(s) { 2 if (s === '') { 3 return 0; 4 } 5 6 var lenMax = 1, 7 lenCurr = 1, 8 i, repeat; 9 10 for (i = 1; i < s.length; i++) { 11 repeat = s.substr(i - lenCurr, lenCurr).indexOf(s.substr(i, 1)); 12 13 if (repea