60 lines
1.2 KiB
Plaintext
60 lines
1.2 KiB
Plaintext
![]() |
Using parallel collections or arrays rather than classes to hold and process related pieces of data is an antipattern. Instead, define a type for the entity the arrays represent and use an array or collection of that type.
|
||
|
|
||
|
|
||
|
This rule raises an issue when the index from one array or collection is used to access an element from another array or collection.
|
||
|
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
----
|
||
|
class Leg
|
||
|
{
|
||
|
public int Length { get; set; }
|
||
|
}
|
||
|
|
||
|
var rightLegs = //...
|
||
|
var leftLegs = //...
|
||
|
|
||
|
for(var i = 0; i<rightLegs.Count; i++)
|
||
|
{
|
||
|
var rightLeg = rightLegs[i]; //Noncompliant
|
||
|
var leftLeg = leftLegs[i]; //Noncompliant
|
||
|
if (leftLeg.Length != rightLeg.Length)
|
||
|
{
|
||
|
//... unlucky
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|
||
|
|
||
|
== Compliant Solution
|
||
|
|
||
|
----
|
||
|
class Leg
|
||
|
{
|
||
|
public int Length { get; set; }
|
||
|
}
|
||
|
|
||
|
class Biped
|
||
|
{
|
||
|
public Leg LeftLeg { get; set; }
|
||
|
public Leg RightLeg { get; set; }
|
||
|
}
|
||
|
|
||
|
var creatures = //...
|
||
|
for(var i = 0; i<creatures.Count; i++)
|
||
|
{
|
||
|
var creature = creatures[i];
|
||
|
if (creature.LeftLeg.Length != creature.RightLeg.Length)
|
||
|
{
|
||
|
//... unlucky
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|
||
|
|
||
|
== Exceptions
|
||
|
|
||
|
Because it is a common pattern to copy items from one collection to another collection, no issue is raised if the collection element is being written.
|
||
|
|