Home

Collapsible if statements should be merged

Description

    The "Collapsible if statements should be merged" rule states that when two or more if statements can be logically combined into one, they should be merged into a single statement. This helps to reduce the complexity of the code and improve readability. By merging multiple if statements, the code becomes more concise and easier to understand. Additionally, it can help to reduce the number of lines of code and make debugging and maintenance easier.

Key Benefits

  • Easier to Read and Maintain: It allows for merged code, making it easier to read and maintain.
  • Reduced Complexity: By merging if statements, the complexity of the code is reduced, making it easier to debug and troubleshoot.
  • Efficient Execution: Merging if statements can lead to more efficient execution, as the code is more concise and easier to process.

 

Non-compliant Code Example

BEGIN
 IF score > 35 THEN   --Non compliant code (Multiple collapsible if statements)
     IF score > 40 THEN 
		IF score > 50 THEN 
			IF score > 60 THEN 
				IF score > 70 THEN 
					DBMS_OUTPUT.PUT('Grade A');
				END IF;
			END IF;
			DBMS_OUTPUT.PUT('Grade B');
		END IF;
	END IF;
	DBMS_OUTPUT.PUT('Grade C');
 END IF;
END;

Compliant Code Example

BEGIN
IF score > 70 THEN  
	DBMS_OUTPUT.PUT('Grade A');
ELSIF score > 50 AND score < 70 THEN  --Compliant code 
	DBMS_OUTPUT.PUT('Grade B');
ELSIF score > 35 AND score < 50 THEN  --Compliant code 
	DBMS_OUTPUT.PUT('Grade C');
ELSE 
	DBMS_OUTPUT.PUT('Grade D');
END IF;					
END;
  
Visual Expert 2024
 VEPLSQLRULE106