-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathDuplicateFunction.qhelp
43 lines (34 loc) · 1.93 KB
/
DuplicateFunction.qhelp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>A function should never be duplicated verbatim in several places in the code. Of course
the severity of this anti-pattern is higher for longer functions than for extremely short
functions of one or two statements, but there are usually better ways of achieving the same
effect.</p>
</overview>
<recommendation>
<p>Code duplication in general is highly undesirable for a range of reasons: The artificially
inflated amount of code hinders comprehension, and ranges of similar but subtly different lines
can mask the real purpose or intention behind a function. There's also an omnipresent risk of
update anomalies, where only one of several copies of the code is updated to address a defect or
add a feature.</p>
<p>In the case of function duplication, how to address the issue depends on the functions themselves
and on the precise classes or modules in which the duplication occurs. At its simplest, the duplication can
be addressed by simply removing all but one of the duplicate function definitions and making
callers of the removed functions refer to the (now canonical) single remaining definition
instead.</p>
<p>This may not be possible for reasons of accessibility. A common example might
be where two classes implement the same functionality but neither is a subtype of the other,
so it is not possible to inherit a single method definition. In such cases, introducing a
common superclass to share the duplicated code is a viable option. Alternatively, if the methods
don't need access to private object state, they can be moved to a module-level function.</p>
</recommendation>
<references>
<li>Elmar Juergens, Florian Deissenboeck, Benjamin Hummel, and Stefan Wagner. 2009.
Do code clones matter? In <em>Proceedings of the 31st International Conference on
Software Engineering</em> (ICSE '09). IEEE Computer Society, Washington, DC, USA,
485-495.</li>
</references>
</qhelp>