-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathParameterAssignment.qhelp
More file actions
49 lines (37 loc) · 1.42 KB
/
ParameterAssignment.qhelp
File metadata and controls
49 lines (37 loc) · 1.42 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
Programmers usually assume that the value of a parameter is the value that was
passed in to the method or constructor. Assigning a different value to a parameter in a
method or constructor invalidates that assumption.</p>
</overview>
<recommendation>
<p>Avoid assignment to parameters by doing one of the following:</p>
<ul>
<li>Introduce a local variable and assign to that instead.</li>
<li>Use an expression directly rather than assigning it to a parameter.</li>
</ul>
</recommendation>
<example>
<p>
In the following example, the first method shows assignment to the parameter <code>miles</code>.
The second method shows how to avoid this by using the expression <code>miles * KM_PER_MILE</code>.
The third method shows how to avoid the assignment by declaring a local variable <code>kilometres</code>
and assigning to that.
</p>
<sample src="ParameterAssignment.java" />
</example>
<references>
<li>
Help - Eclipse Platform:
<a href="https://help.eclipse.org/2020-12/advanced/content.jsp?topic=/org.eclipse.jdt.doc.user/reference/preferences/java/compiler/ref-preferences-errors-warnings.htm">Java Compiler Errors/Warnings Preferences</a>.
</li>
<li>
Java Basics:
<a href="https://web.archive.org/web/20200223080939/http://leepoint.net/JavaBasics/methods/methods-22-local-variables.html">Methods 4 - Local variables</a>.
</li>
</references>
</qhelp>