-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMissedSelectOpportunity.qhelp
More file actions
33 lines (26 loc) · 1.2 KB
/
MissedSelectOpportunity.qhelp
File metadata and controls
33 lines (26 loc) · 1.2 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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>It is common to see loops that immediately compute a value from their iteration variable and
then never use the iteration variable again in the rest of the loop (see example below). The intent
of such loops is arguably not to iterate over the original sequence at all, but to iterate over the
sequence that results from transforming the original sequence in some manner.</p>
</overview>
<recommendation>
<p>There is a good case to be made that the code is more readable if this intent is expressed
explicitly, which can be done by using LINQ to perform a <code>Select</code> on the input sequence.
The resulting code is clearer due to better separation of concerns.</p>
</recommendation>
<example>
<p>This example iterates over a list of i<sup>2</sup>.</p>
<sample src="MissedSelectOpportunity.cs" />
<p>This could be better expressed by using LINQ's <code>Select</code> method with a lambda
expression.</p>
<sample src="MissedSelectOpportunityFix.cs" />
</example>
<references>
<li>MSDN: <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.select.aspx">Enumerable.Select Method</a>.</li>
</references>
</qhelp>