Home
WHEN clauses should not have too many lines
Description
The rule "WHEN clauses should not have too many lines" states that when writing PL/SQL code, the WHEN clauses should be kept as concise as possible. This means that the code should be written in such a way that the WHEN clauses are not too long or complex, as this can make the code difficult to read and understand. It is recommended that the WHEN clauses should be kept to a maximum of three lines, and that any complex logic should be broken up into smaller, more manageable chunks. This will help to ensure that the code is easier to read and understand, and that any potential errors can be quickly identified and resolved.
Key Benefits
- Reduced complexity: By limiting the number of lines in a WHEN clause, the complexity of the code is reduced, making it easier to read and maintain.
- Improved performance: Fewer lines of code can lead to improved performance as the database engine has to process fewer instructions.
- Increased clarity: Limiting the number of lines in a WHEN clause makes the code more concise and easier to understand.
Non-compliant Code Example
DECLARE
price NUMBER:=29500;
BEGIN
GetCarDetails(:price, car_record);
CASE (price)
WHEN price > 0 AND price < 500000 THEN --Non compliant code (WHEN clauses is having lines more then default defined 6 limit)
DBMS_OUTPUT.PUT_LINE('Low Model');
DBMS_OUTPUT.PUT_LINE('Model Number:'|| car_record.modelnumber);
DBMS_OUTPUT.PUT_LINE('Power Generation :'|| car_record.power);
DBMS_OUTPUT.PUT_LINE('Engine :'|| car_record.engine_size);
DBMS_OUTPUT.PUT_LINE('Size (LxWxH) :'|| car_record.car_size);
DBMS_OUTPUT.PUT_LINE('Year Of Model:'|| car_record.yearmodel);
DBMS_OUTPUT.PUT_LINE('Ex Showroom Price:'|| car_record.price);
WHEN price >= 500000 AND price < 1200000
THEN 'Hatchback Model'
WHEN price >= 1200000 AND price < 1500000
THEN 'Sedan Model'
END;
END;
Compliant Code Example
DECLARE
price NUMBER:=29500;
BEGIN
GetCarDetails(:price, car_record);
CASE (price)
WHEN price > 0 AND price < 500000 THEN --Compliant code
PrintDetails(:price, car_record);--PrintDetails contains multiple statements (DBMS_OUTPUT.PUT_LINE statements in this case)
WHEN price >= 500000 AND price < 1200000
THEN 'Hatchback Model'
WHEN price >= 1200000 AND price < 1500000
THEN 'Sedan Model'
END;
END;