DRY(don't repeat yourself)软件思想(1)

来源:互联网 发布:矩阵开关芯片 编辑:程序博客网 时间:2024/06/10 00:27

In software engineering, Don't Repeat Yourself (DRY) or Duplication is Evil (DIE) is a principle of software development aimed at reducing repetition of information of all kinds, especially useful in multi-tier architectures. The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system." The principle has been formulated by Andy Hunt and Dave Thomas in their book The Pragmatic Programmer. They apply it quite broadly to include "database schemas, test plans, the build system, even documentation."[1] When the DRY principle is applied successfully, a modification of any single element of a system does not change other logically-unrelated elements. Additionally, elements that are logically related all change predictably and uniformly, and are thus kept in sync. Besides using methods and subroutines in their code, Thomas and Hunt rely on code generators, automatic build systems, and scripting languages to observe the DRY principle across layers.

Also known as Single Source of Truth, this philosophy is prevalent in model-driven architectures, in which software artifacts are derived from a central object model expressed in a form such as UML. DRY code is created by data transformation and code generators, which allows the software developer to avoid copy and paste operations. DRY code usually makes large software systems easier to maintain, as long as the data transformations are easy to create and maintain.

Kent Beck has mentioned that he does not "subscribe to [DRY] for test code because [he] want[s] [his] tests to read like a story".


翻译:

在软件工程 , 不要编写同一代码(DRY)或重复劳动是一种浪费 ( DIE )是一个软件开发的原则,其目的是减少重复信息,尤其是用在多层次的体系结构中 。 DRY原则的为“每一个知识(此处处可理解为代码)都必须在系统内必须是单一的,明确的,权威的,具有代表性。” 该原则是由Andy Hunt 和 Dave Thomas在他们的著作《The Pragmatic Programmer》中提出的 。 他们将它应用在相当广泛的领域,包括“ 数据库架构, 软件测试 ,系统架构,甚至是文件管理 。“ 当DRY的原则成功应用,在系统中,任何单一元素的修改不影响与其逻辑无关的元素。 此外,和元素逻辑相关的一切变化都是可以预见和统一,因此保持同步 。 除了使用这种方法(此处的method和subroutines是同一个意思,只是一个是面向对象的说法,一个是面向过程的说法),在他们的代码, Thomas和Hunt 依靠代码生成 ,自动生成系统,并依赖脚本语言,观察DRY原则在跨层中的运用。

DRY这个思想被称为真理的一个来源(这里上升到哲学高度) ,这种哲学是普遍的模型驱动架构(个人认为可以理解为分层复用的设计思想) ,这种软件构件(的思想)来源于中间体( central object model是指硬件和应用程序中间的抽象接口模型)类似UML的的形式表示出来 。 通过 用DRY思想创建的数据转换和代码生成器,允许软件开发人员,避免复制和粘贴操作。 DRY代码通常使得大型软件系统更容易维护,因为数据转换很容易创建和维护。

Kent Beck提到,他不“赞同用[DRY]去测试代码,因为[他]希望, [他的]测试,读起来想个故事。”(这句话是说DRY分层反而导致了阅读的不便利,读起来不顺,不知道我这个解释对不对。)
 

原创粉丝点击