2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-06-08 14:23:48 +02:00
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.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-06-08 14:23:48 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2021-06-08 14:23:48 +02:00
----
class Leg
{
public int Length { get; set; }
}
var rightLegs = //...
var leftLegs = //...
for(var i = 0; i<rightLegs.Count; i++)
{
2024-05-31 17:08:01 +02:00
var rightLeg = rightLegs[i]; // Noncompliant
var leftLeg = leftLegs[i]; // Noncompliant
2021-06-08 14:23:48 +02:00
if (leftLeg.Length != rightLeg.Length)
{
//... unlucky
}
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-06-08 14:23:48 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2021-06-08 14:23:48 +02:00
----
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
}
}
----
2023-05-03 11:06:20 +02:00
=== Exceptions
2021-06-08 14:23:48 +02:00
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.