Home
Magic numbers should not be used
Description
The "Magic numbers should not be used" rule in PL/SQL code states that hard-coded numeric values should not be used in code. Instead, constants should be used to represent the numeric values, so that the code is more readable and maintainable. This makes it easier to understand the code and to modify it if needed.
Key Benefits
- Security: Using Magic numbers makes code vulnerable to attack as they are easily guessed.
- Maintainability: Using Magic numbers makes code difficult to maintain as it is not clear what the numbers represent.
- Readability: Using Magic numbers makes code hard to read and understand.
Non-compliant Code Example
BEGIN FORALL j IN CUSTOMER_TABLE.FIRST..CUSTOMER_TABLE.LAST SAVE EXCEPTIONS UPDATE CUSTOMERS SET minimunExpense = 1800 --Non compliant code (Magic numbers is used) WHERE 100 < CUSTOMER_TABLE(j); --Non compliant code (Magic numbers is used) EXCEPTION WHEN OTHERS THEN error_message := SQLERRORMSG; DBMS_OUTPUT.PUT_LINE (error_message); RAISE; END;
Compliant Code Example
DECLARE minExpenseValue INTEGER := 1800; counter INTEGER := 100; BEGIN FORALL j IN CUSTOMER_TABLE.FIRST..CUSTOMER_TABLE.LAST SAVE EXCEPTIONS UPDATE CUSTOMERS SET minimunExpense = minExpenseValue --Compliant code WHERE counter < CUSTOMER_TABLE(j); --Compliant code EXCEPTION WHEN OTHERS THEN error_message := SQLERRORMSG; DBMS_OUTPUT.PUT_LINE (error_message); RAISE; END;