Share to: share facebook share twitter share wa share telegram print page

 

Don't repeat yourself

"Don't repeat yourself" (DRY), also known as "duplication is evil", is a principle of software development aimed at reducing repetition of information which is likely to change, replacing it with abstractions that are less likely to change, or using data normalization which avoids redundancy in the first place.

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.[1] They apply it quite broadly to include database schemas, test plans, the build system, even documentation.[2] When the DRY principle is applied successfully, a modification of any single element of a system does not require a change in 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.

Single choice principle

A particular case of DRY is the single choice principle. It was defined by Bertrand Meyer as: "Whenever a software system must support a set of alternatives, one and only one module in the system should know their exhaustive list."[3] It was applied when designing Eiffel.

Alternatives

WET

The opposing view to DRY is called WET, a backronym commonly taken to stand for write everything twice[4] (alternatively write every time, we enjoy typing or waste everyone's time). WET solutions are common in multi-tiered architectures where a developer may be tasked with, for example, adding a comment field on a form in a web application. The text string "comment" might be repeated in the label, the HTML tag, in a read function name, a private variable, database DDL, queries, and so on. A DRY approach eliminates that redundancy by using frameworks that reduce or eliminate all those editing tasks except the most important ones, leaving the extensibility of adding new knowledge variables in one place.[5] This conceptualization of "WET" as an alternative to "DRY" programming has been around since at least 2002 in the Java world, though it is not known who coined the term.[6]

AHA

Another approach to abstractions is the AHA principle. AHA stands for avoid hasty abstractions, described by Kent C. Dodds as optimizing for change first, and avoiding premature optimization.[7] and was influenced by Sandi Metz's "prefer duplication over the wrong abstraction".[8]

AHA is rooted in the understanding that the deeper the investment engineers have made into abstracting a piece of software, the more they perceive that the cost of that investment can never be recovered (sunk cost fallacy). Thus, engineers tend to continue to iterate on the same abstraction each time the requirement changes. AHA programming assumes that both WET and DRY solutions inevitably create software that is rigid and difficult to maintain. Instead of starting with an abstraction, or abstracting at a specific number of duplications, software can be more flexible and robust if abstraction is done when it is needed, or, when the duplication itself has become the barrier and it is known how the abstraction needs to function.

AHA programming was originally named "moist code" by Dodds, later again by Daniel Bartholomae,[9] and originally referred to as DAMP (Don't Abstract Methods Prematurely) by Matt Ryer.[10] There was a different programming principle already named DAMP (Descriptive And Meaningful Phrases) and described by Jay Fields,[11] and the community pushed back against the usage of MOIST, due to the cultural aversion to the word moist.[12] Dodds called for alternatives on Twitter, and suggested DATE as an alternative before settling on Cher Scarlett's suggestion of AHA.[7][13][14]

See also

References

  1. ^ Hunt, Andrew; Thomas, David (1999). The Pragmatic Programmer : From Journeyman to Master (1 ed.). US: Addison-Wesley. pp. 320. ISBN 978-0201616224.
  2. ^ Dave Thomas, interviewed by Bill Venners (2003-10-10). "Orthogonality and the DRY Principle". Retrieved 2006-12-01.
  3. ^ Object Oriented Software Construction, 2nd edition, page 63
  4. ^ Pai, Praseed; Xavier, Shine (2017-01-31). .NET Design Patterns. Packt Publishing Ltd. ISBN 978-1-78646-186-5.
  5. ^ Justin Lee (2006-03-08). "DRY is for losers". Retrieved 2013-08-31.
  6. ^ Zig Zichterman (2002-08-08). "JavaOne 2002: Zig's Notes". Retrieved 2024-01-09.
  7. ^ a b Kent C. Dodds (2019-04-01). "AHA Programming". Retrieved 2021-05-08.
  8. ^ Sandi Metz (2016-01-20). "The Wrong Abstraction". Retrieved 2021-05-08.
  9. ^ Bartholomae, Daniel (21 August 2020). "Moist code - Why code should not be completely DRY". The Startup CTO. Retrieved 11 November 2021.
  10. ^ Haus, Ev (24 December 2020). "Using DRY, WET & DAMP code". Medium. Retrieved 11 November 2021.
  11. ^ Fields, Jay. "DRY code, DAMP DSLs". Jay Fields' Thoughts. Retrieved 11 November 2021.
  12. ^ Resnick, Brian (28 April 2016). "Why do so many people dislike the word "moist"? This scientist has a theory". Vox Media. Retrieved 11 November 2021.
  13. ^ Dodds, Kent (27 March 2021). "3 Minutes with Kent: Write the code first, then make the abstraction". Briefs. Retrieved 11 November 2021.
  14. ^ Dodds, Kent; Bostian, Emma; Nisi, Nick (30 July 2021). "JS Party – Episode #186: Getting hooked on React". The Changelog. Retrieved 11 November 2021.
Kembali kehalaman sebelumnya


Index: pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve 
Prefix: a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9