mirror of
https://github.com/github/codeql.git
synced 2025-12-28 14:46:33 +01:00
44 lines
1.5 KiB
XML
44 lines
1.5 KiB
XML
<!DOCTYPE qhelp PUBLIC
|
|
"-//Semmle//qhelp//EN"
|
|
"qhelp.dtd">
|
|
<qhelp>
|
|
|
|
|
|
<overview>
|
|
<p>Anonymous classes are a common way of creating implementations of an interface
|
|
or abstract class whose functionality is
|
|
really only needed once. Duplicating the definition of an anonymous class in several
|
|
places is usually a sign that refactoring is necessary.</p>
|
|
|
|
<p>Code duplication in general is highly undesirable for a range of reasons. The artificially
|
|
inflated amount of code is more difficult to understand, and sequences of similar but subtly different lines
|
|
can mask the real purpose or intention behind them. Also, there is always a risk that only one
|
|
of several copies of the code is updated to address a defect or add a feature.</p>
|
|
|
|
</overview>
|
|
<recommendation>
|
|
|
|
<p>Introduce a concrete class
|
|
that contains the definition just once, and replace the anonymous classes with instances
|
|
of this concrete class.</p>
|
|
|
|
</recommendation>
|
|
<example>
|
|
|
|
<p>In the following example, the definition of the class <code>addActionListener</code> is duplicated for
|
|
each button that needs to use it. A better solution is shown that defines just one class,
|
|
<code>MultiplexingListener</code>, which is used by each button.</p>
|
|
|
|
<sample src="DuplicateAnonymous.java" />
|
|
|
|
</example>
|
|
<references>
|
|
|
|
<li>E. Juergens, F. Deissenboeck, B. Hummel, S. Wagner.
|
|
<em>Do code clones matter?</em> Proceedings of the 31st International Conference on
|
|
Software Engineering,
|
|
485-495, 2009.</li>
|
|
|
|
</references>
|
|
</qhelp>
|