Home
Two branches in a conditional structure should not have exactly the same implementation
Description
The PL/SQL code rule "Two branches in a conditional structure should not have exactly the same implementation" states that when using a conditional structure such as an IF/ELSE statement, the two branches should not contain the same code. This means that the code should differ in some way, either in the code itself or in the outcome. This rule helps ensure that the code is more robust and that any unexpected behavior can be easily identified and corrected.
Key Benefits
- Reduces complexity: By having two branches in a conditional structure with different implementations, the complexity of the code is reduced as it can be easier to read and understand.
- Makes code more maintainable: By having two branches in a conditional structure with different implementations, it makes the code more maintainable as it can be easier to identify and modify any parts of the code that need to be changed.
- Improves performance: By having two branches in a conditional structure with different implementations, it can improve the performance of the code as it can be optimized for different scenarios.
Non-compliant Code Example
BEGIN
IF counter > 0 THEN
cust_order := 1;
message := 'Number is positive';
ELSIF counter > 100 THEN --Non compliant code (ElSEIF branch is having exactly the same implementation of IF branch)
cust_order := 1;
message := 'Number is positive';
ELSE
cust_order := 0;
message := 'Number is negative';
END IF;
END;
Compliant Code Example
BEGIN
IF counter > 0 THEN
cust_order := 1;
message := 'Number is positive';
ELSIF counter > 100 THEN --Compliant code (ElSEIF branch is having exactly the same implementation of IF branch)
cust_order := 2;
message := 'Number is positive';
ELSE
cust_order := 0;
message := 'Number is negative';
END IF;
END;