
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
60 lines
1.6 KiB
Plaintext
60 lines
1.6 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Because ``++IEnumerable++``s are lazy-evaluated, each iteration causes a re-retrieval of the values, which could involve considerable overhead. For instance, when the ``++IEnumerable++`` is backed by a database, each iteration requires an additional round of database interactions. For that reason, any time the set represented by an ``++IEnumerable++`` must be iterated multiple times, it should first be converted to a ``++List++``, which will retrieve the values and store them in memory. From that point they can be iterated as often as needed without an additional performance hit.
|
|
|
|
|
|
This rule raises an issue for each iteration of an ``++IEnumerable++`` after the first one.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,csharp]
|
|
----
|
|
IEnumerable<int> numbers = GetNumbers();
|
|
|
|
var count = numbers.Count(); // causes an iteration
|
|
var last = numbers.Last(); // Noncompliant; causes an iteration
|
|
|
|
foreach(var x in numbers) // Noncompliant
|
|
{
|
|
// ...
|
|
}
|
|
----
|
|
|
|
[source,csharp]
|
|
----
|
|
List<int> numbers = GetNumbers().ToList(); // iterable to your heart's content
|
|
|
|
var count = numbers.Count();
|
|
var last = numbers.[count -1];
|
|
|
|
foreach(var x in numbers)
|
|
{
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Convert this "IEnumerable" to a list before it is iterated.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 8 Jul 2015, 08:50:25 Tamas Vajk wrote:
|
|
\[~ann.campbell.2] Looks good
|
|
|
|
=== on 21 Aug 2015, 06:17:39 Tamas Vajk wrote:
|
|
\[~ann.campbell.2] Do you think this is a security issue as well?
|
|
|
|
endif::env-github,rspecator-view[]
|