|
3 | 3 | "qhelp.dtd"> |
4 | 4 | <qhelp> |
5 | 5 | <overview> |
6 | | -<p>Programmers sometimes need to iterative over a filtered version of a sequence, rather than the |
7 | | -sequence itself. For example, you might want to print out only the numbers in the range [1,10] that |
8 | | -are even. One standard way of doing this is to write a loop that iterates over the whole sequence, |
9 | | -testing the variable each iteration to determine whether or not it is even. This is often written |
10 | | -using either <code>if(!condition(var)) continue;</code> as the initial statement in the loop, or by |
| 6 | +<p>Programmers sometimes need to iterate over a filtered version of a sequence, rather than the |
| 7 | +sequence itself. For example, you might want to print out only the numbers in the range [1,10] that |
| 8 | +are even. One standard way of doing this is to write a loop that iterates over the whole sequence, |
| 9 | +testing the variable each iteration to determine whether or not it is even. This is often written |
| 10 | +using either <code>if(!condition(var)) continue;</code> as the initial statement in the loop, or by |
11 | 11 | enclosing the entire loop body with <code>if(condition(var))</code>.</p> |
12 | 12 |
|
| 13 | +<p>This recommendation does not apply when the matching branch exits the loop without continuing to |
| 14 | +later iterations, such as with <code>return</code>, <code>yield break</code>, or <code>throw</code>. |
| 15 | +In those cases the loop is searching for a terminal condition rather than filtering the remaining |
| 16 | +loop body.</p> |
| 17 | + |
13 | 18 | </overview> |
14 | 19 | <recommendation> |
15 | | -<p>This pattern works well and is also available as the <code>Where</code> method in LINQ in C# 3.5 |
16 | | -and above. It is better to use a library method in preference to writing your own pattern unless you |
17 | | -have a specific need for a custom version. In particular, this makes the code easier to read by |
| 20 | +<p>This pattern works well and is also available as the <code>Where</code> method in LINQ in C# 3.5 |
| 21 | +and above. It is better to use a library method in preference to writing your own pattern unless you |
| 22 | +have a specific need for a custom version. In particular, this makes the code easier to read by |
18 | 23 | expressing the intent better and by reducing the nesting depth of the code.</p> |
19 | 24 |
|
20 | 25 | </recommendation> |
21 | 26 | <example> |
22 | | -<p>This example shows two ways of iterating over a series of integers and only performing an action |
| 27 | +<p>This example shows two ways of iterating over a series of integers and only performing an action |
23 | 28 | on the even ones.</p> |
24 | 29 | <sample src="MissedWhereOpportunity.cs" /> |
25 | 30 |
|
26 | 31 | <p>This is far better expressed using the <code>Where</code> method.</p> |
27 | 32 | <sample src="MissedWhereOpportunityFix.cs" /> |
28 | 33 |
|
| 34 | +<p>The following example should not use <code>Where</code>, because the matching branch exits the |
| 35 | +method or iterator instead of continuing with filtered loop work.</p> |
| 36 | +<sample src="MissedWhereOpportunityGood.cs" /> |
| 37 | + |
29 | 38 | </example> |
30 | 39 | <references> |
31 | 40 |
|
|
0 commit comments