DEV Community

loading...

Discussion on: The Pros and Cons of DRY Code

Collapse
pinotattari profile image
Riccardo Bernardini

I do not have a "decision algorithm" for deciding if "factorizing out" a piece of code or not, but roughly I would say that I apply DRY if at least one of the following conditions is satisfied

  1. The action has a meaning by itself. For example, the code necessary to download a file after authentication could be implemented as an independent piece of code.
  2. The duplicated code is quite complex and long and/or the probability of updating it in the future are quite large.

Actually, sometimes I do kind of opposite and put in code procedures that are maybe just called once in to improve readability. Sometimes it can happen that a procedure is quite long and does lots of different actions; in order to make some "order" I split the code in different procedures (with nice descriptive names). In my opinion this makes the code easier to understand. This is quite convenient in Ada (the language I code with) since you can define procedures inside other procedures, limiting the visibility (and the need for hunting) of the "readability procedures."

Collapse
ronnewcomb profile image
Ron Newcomb

I also sometimes do/don't factor out a piece of code based on whether I have a good, obvious name for it.