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

Methods should not have identical implementations

    Details

    • Message:
      Update this method so that its implementation is not identical to "xxx".
    • Highlighting:
      • primary: method signature
      • secondary: original method
        • message: original implementation
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Flex, Rust, Solidity, VB6
    • Covered Languages:
      C#, C, C++, Go, Java, JavaScript, Kotlin, Objective-C, PHP, Python, Ruby, Scala, Swift, TypeScript, VB.Net
    • Irrelevant for Languages:
      ABAP, Cobol, CSS, HTML, PL/I, PL/SQL, RPG, T-SQL, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      15min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • ESLint-SonarJS:
      no-identical-functions

      Description

      When two methods have the same implementation, either it was a mistake - something else was intended - or the duplication was intentional, but may be confusing to maintainers. In the latter case, one implementation should invoke the other. Numerical and string literals are not taken into account.

      Noncompliant Code Example

      private final static String CODE = "bounteous";
      
      public String calculateCode() {
        doTheThing();
        return CODE;
      }
      
      public String getName() {  // Noncompliant
        doTheThing();
        return CODE;
      }
      

      Compliant Solution

      private final static String CODE = "bounteous";
      
      public String getCode() {
        doTheThing();
        return CODE;
      }
      
      public String getName() {
        return getCode();
      }
      

      Exceptions

      Methods that are not accessors (getters and setters), with fewer than 2 statements are ignored.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated: