Home

Unused local variables should be removed

Description

    The rule "Unused local variables should be removed" states that any local variables declared in a PL/SQL code block that are not used should be removed. This is to ensure that the code is as efficient as possible, and that any unnecessary code is not taking up space. Unused variables can also lead to confusion and errors, so it is important to remove them in order to keep the code clean and organized.

Key Benefits

  • Reduced complexity: Removing unused local variables reduces the complexity of the code, making it easier to read and understand.
  • Improved performance: Unused local variables can cause the code to run slower, so removing them can improve performance.
  • Reduced memory usage: Unused local variables take up memory, so removing them can reduce memory usage.

 

Non-compliant Code Example

DECLARE
limit PLS_INTEGER := 100;               --Non compliant code (Unused local variable)
CURSOR cur_dept (departmentId INTEGER) RETURN departments%ROWTYPE IS 
    SELECT * FROM departments          
    WHERE department_id = departmentId;	
	
BEGIN
  OPEN cur_dept;
  FETCH cur_dept 
  BULK COLLECT INTO temp1;
  IF NOT cur_dept%FOUND THEN  
    DBMS_OUTPUT.PUT_LINE('Not Found !!');
  ELSE
    DBMS_OUTPUT.PUT_LINE('Department Found: ' || temp1);
  END IF;
  CLOSE cur_dept;
END;

Compliant Code Example

CURSOR cur_dept (departmentId INTEGER) RETURN departments%ROWTYPE IS     --Compliant code (Removed unused local variable)
    SELECT * FROM departments          
    WHERE department_id = departmentId;	
	
BEGIN
  OPEN cur_dept;
  FETCH cur_dept 
  BULK COLLECT INTO temp1;
  IF NOT cur_dept%FOUND THEN  
    DBMS_OUTPUT.PUT_LINE('Not Found !!');
  ELSE
    DBMS_OUTPUT.PUT_LINE('Department Found: ' || temp1);
  END IF;
  CLOSE cur_dept;
END;
Visual Expert 2024
 VEPLSQLRULE124