Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3221

Parallel collections should not be maintained

    XMLWordPrintable

    Details

    • Type: Finding Detection
    • Status: Closed
    • Resolution: Won't Fix
    • Labels:
    • Message:
      Create a type that holds an element from "a", "b", and "c", and merge these parallel collections indexed by the same value.
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Targeted languages:
      C#, C++, Flex, Java, Objective-C, Swift
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      30min

      Description

      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.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ann.campbell.2 Ann Campbell
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: