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

Nested code blocks should not be used

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Extract this nested code block into a separate method.
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      JavaScript, PHP, TypeScript
    • Covered Languages:
      C#, C, C++, Java, Objective-C
    • Irrelevant for Languages:
      VB.Net
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Scope:
      Main Sources, Test Sources
    • Checkstyle:
      AvoidNestedBlocks

      Description

      Nested code blocks can be used to create a new scope and restrict the visibility of the variables defined inside it. Using this feature in a method typically indicates that the method has too many responsibilities, and should be refactored into smaller methods.

      Noncompliant Code Example

      public void evaluate(int operator) {
        switch (operator) {
          /* ... */
          case ADD: {                                // Noncompliant - nested code block '{' ... '}'
              int a = stack.pop();
              int b = stack.pop();
              int result = a + b;
              stack.push(result);
              break;
            }
          /* ... */
        }
      }
      

      Compliant Solution

      public void evaluate(int operator) {
        switch (operator) {
          /* ... */
          case ADD:                                  // Compliant
            evaluateAdd();
            break;
          /* ... */
        }
      }
      
      private void evaluateAdd() {
        int a = stack.pop();
        int b = stack.pop();
        int result = a + b;
        stack.push(result);
      }
      

        Attachments

          Issue Links

          1.
          C# RSPEC-5192 Language-Specification Active Unassigned
          2.
          C-Family RSPEC-5521 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                freddy.mallet Freddy Mallet (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated: