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

Objects whose internal states are updated should be used subsequently

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this object; its creation has no effect on the overall program execution.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Targeted languages:
      C#, C++, Flex, Java, JavaScript, Objective-C, PHP, Python, Swift
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      20min
    • FindBugs:
      UC_USELESS_OBJECT, UC_USELESS_OBJECT_STACK

      Description

      There's no point in creating objects that don't effect program outcome. The creation of such objects is likely to indicate an error. Perhaps an assignment was forgotten or a method left uncalled?

      This rule raises an issue when an object is created and its state is updated, but it neither leaves the method where it was created nor affects the enclosing object.

      Noncompliant Code Example

      public void addPerson(String name){
        Person p = new Person();  // Noncompliant
        p.gender = Person.Gender.MALE;
        p.setName(name);
      }
      

      Compliant Solution

      public void addPerson(){
        Person p = new Person();
        p.gender = Person.Gender.MALE;
        p.setName("Sam");
      
        people.add(p);
      }
      

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated: