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

Variables should not be declared inside loops

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Move this variable declaration outside of the loop.
    • Highlighting:
      Hide

      Variable declaration

      Show
      Variable declaration
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Targeted languages:
      PL/SQL
    • Irrelevant for Languages:
      ABAP, C#, C, C++, Cobol, CSS, Flex, HTML, Java, JavaScript, Objective-C, PHP, PL/I, Python, RPG, Swift, T-SQL, TypeScript, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      It is a best practice to have variable declarations outside of the loop. Additionally, declaring variables inside a loop is slightly less efficient because memory management is then performed with each iteration of the loop.

      Noncompliant Code Example

      BEGIN
        FOR i IN 1..10 
        LOOP
          DECLARE
            variableX NUMBER:= 10;
          BEGIN
              variableX:= variableX+i;
              dbms_output.put_line(variableX);
          END;
       END LOOP;
      END;
      

      Compliant Solution

      DECLARE
        variableX NUMBER:= 0;
      BEGIN
        FOR i IN 1..10
        LOOP
          variableX:= variableX+1;
          dbms_output.put_line(variableX);
        END LOOP;
      END;
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              jeanchristophe.collet Jean-Christophe Collet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: