-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathTooManyRefParameters.qhelp
More file actions
34 lines (30 loc) · 1.59 KB
/
TooManyRefParameters.qhelp
File metadata and controls
34 lines (30 loc) · 1.59 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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Whilst passing method arguments using <code>ref</code> is occasionally a sensible thing to do
(the canonical example is when writing a 'swap' method), overusing reference parameters can make
methods needlessly difficult to understand and call.</p>
<p>Methods end up relying on their parameters being correctly initialized elsewhere and can have
problems such as aliasing (when two parameters refer to the same object). They can be difficult to
call because the references must refer to l-values (rather than temporary objects), so additional
objects must be created to hold the results of the call. Their results can also be difficult to
forward to other methods.</p>
</overview>
<recommendation>
<p>Whilst it is not applicable in every situation, and some judgment must be applied, a common
solution is to create a new class that wraps the values you are trying to set in the method and then
modify the method to return a new instance of it.</p>
</recommendation>
<example>
<p>In this example, populating the <code>name</code>, <code>address</code> and <code>tel</code>
fields is done via a method that takes <code>ref</code> parameters. This is not very good practice
and makes the method confusing.</p>
<sample src="TooManyRefParametersBad.cs" />
<p>It is better to wrap the values in their own <code>Details</code> class and then return a new
instance of <code>Details</code>. It is easier to pass to other functions correctly and is easier
to understand.</p>
<sample src="TooManyRefParametersGood.cs" />
</example>
</qhelp>