Code duplication

From HaskellWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Code duplication means that literal copies of a larger piece of code are present in a program. In all programming languages this is considered bad style, because it requires multiple maintenance of those pieces. Also a reader of the program might not immediately recognize the copies as duplicates, thus thinking there must be a subtle difference and trying to find it. Those differences could be small syntactic differences like semicolon vs. comma, or it can also be that the literally same code means something different in different contexts. Code duplication is either due to an undisciplined programmer or due to restricted expressiveness of the language.

The standard way of avoiding code duplicates is writing a function and calling it, instead of copying its body. We shall however admit, that sometimes the different uses of a function require different extensions which may at some point justify to replicate that function.

Laziness

In Haskell code duplication can also lead to unintended laziness breaks. E.g.

if b
  then 'a':x
  else 'a':y

contains the small code duplication of prepending 'a' to another list. The ugly effect is that if b is undefined, then the whole expression is evaluated to undefined. Put differently: As long as the evaluation of b has not finished, nothing from the expression can be evaluated. This situation can be improved by

'a' : if b then x else y

which avoids both code duplication and too much strictness.

See also