Code duplication

From HaskellWiki
Revision as of 19:53, 28 December 2008 by Lemming (talk | contribs) (explanation, laziness issues)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.

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