Details

    • Type: Language-Specification
    • Status: Active
    • Resolution: Unresolved
    • Labels:
      None
    • Impact:
      Unknown 'null' severity
    • Likelihood:
      Unknown 'null' severity
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended

      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.

      This rule raises an exception when two methods implemented inside the class definition share the same implementation.

      Noncompliant Code Example

      class Point {
        int x;
        int y;
      
      // .....
      
      public:
        void setX(int v) {
          if (v >= 0 && v < MAX_X) {
            x = v;
            return;
          }
          error();
        }
      
        void setY(int v) {  // Noncompliant
          if (v >= 0 && v < MAX_X) {
            x = v;
            return;
          }
          error();
        }
      };
      

      Compliant Solution

      class Point {
        int x;
        int y;
      
      // .....
      
      public:
       void setX(int v) {
          if (v >= 0 && v < MAX_X) {
            x = v;
            return;
          }
          error();
        }
      
        void setY(int v) {
          if (v >= 0 && v < MAX_X) {
            y = v;
            return;
          }
          error();
        }
      };
      

      Exceptions

      Empty methods, methods with the same name (overload) and methods with only one statement are ignored.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            massimo.paladin Massimo PALADIN
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: