-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathDubiousDowncastOfThis.qhelp
More file actions
29 lines (25 loc) · 1.03 KB
/
DubiousDowncastOfThis.qhelp
File metadata and controls
29 lines (25 loc) · 1.03 KB
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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
Downcasting <code>this</code> to a derived type creates a dependency cycle. Derived types already depend
on their parent type and the cast creates a dependency in the other direction.
</p>
<p>Dependency cycles should be avoided as they make code both difficult to read and difficult to test.
In addition, a type should not know about its specific descendants, even though it may impose some
constraints on them as a group (for example, abstract classes may require every derived type to implement
a method with a specific signature).</p>
</overview>
<recommendation>
<p>
The base and derived types should be redesigned so that there is no need for the base type to depend
on the types deriving from it.</p>
</recommendation>
<example>
<p>In this example, <code>BadBase</code> introduces a dependency cycle with <code>Derived</code> by
coercing the type of <code>this</code> to a derived type. </p>
<sample src="DubiousDowncastOfThis.java" />
</example>
</qhelp>