(explanation, laziness issues)
(section for laziness)
Latest revision as of 19:53, 28 December 2008
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.
 1 Laziness
In Haskell code duplication can also lead to unintended laziness breaks. E.g.
if b then 'a':x else 'a':y
This situation can be improved by
'a' : if b then x else y
which avoids both code duplication and too much strictness.