Sub topic 16
Design, Naming, and Testing Guidelines
Assume you know basic SQL and anonymous PL/SQL blocks. This chapter focuses on making stored procedures and functions clear, safe, and testable.
Design subprograms around one responsibility
A subprogram should do one related job. A procedure might update an employee's salary. A function might calculate a tax rate. Avoid a single procedure that updates data, sends email, formats reports, and writes audit records.
Use a procedure when the main purpose is an action. Use a function when the main purpose is calculating or returning one value.
Choose names that describe the business action or result:
raise_salarydescribes an action.get_full_namedescribes a returned value.calculate_order_totaldescribes a calculation.p_employee_ididentifies a parameter.v_totalidentifies a local variable.
Use consistent prefixes if your team follows that convention. For example, p_ can identify parameters and v_ can identify local variables. Do not rely on prefixes to replace clear names.
Keep interfaces stable
The parameter list is the subprogram's interface, or the part callers depend on. Prefer anchored types such as %TYPE when a parameter represents a table column.
CREATE OR REPLACE PROCEDURE raise_salary (
p_employee_id IN employees.employee_id%TYPE,
p_percent IN NUMBER DEFAULT 10
) AS
BEGIN
UPDATE employees
SET salary = salary * (1 + p_percent / 100)
WHERE employee_id = p_employee_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001,
'Employee not found: ' || p_employee_id);
END IF;
END raise_salary;
/A default value can preserve compatibility when a new optional behavior is added. Avoid changing parameter meaning after callers already depend on it. If the interface must change substantially, create a new subprogram or package version.
Validate boundaries and handle errors
Validate values at the boundary of the subprogram. For example, reject a negative percentage before changing data.
Raise errors that explain what failed. Do not catch every exception and silently return a success value. Preserve useful Oracle errors unless you can add meaningful context.
IF p_percent < 0 THEN
RAISE_APPLICATION_ERROR(-20002, 'Percentage cannot be negative');
END IF;Keep transaction control deliberate. A reusable procedure should usually not commit or roll back its caller's work unless that behavior is part of its documented contract.
Test normal and failure paths
Test more than the expected input. Check:
- A valid existing identifier.
- A missing identifier.
- Boundary values such as zero.
- Invalid values such as negative percentages.
- Repeated calls and calls inside a larger transaction.
NULLvalues where they are allowed.
Use a test block that records the observable result. Roll back changes made by the test when possible.
BEGIN
raise_salary(p_employee_id => 100, p_percent => 5);
DBMS_OUTPUT.PUT_LINE('Success');
ROLLBACK;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
ROLLBACK;
RAISE;
END;
/A good test checks both the result and the error behavior. Automated tests should run against controlled data and should not depend on the current contents of a shared production schema.
Exercise
Why should a reusable procedure usually avoid committing its caller's transaction?
Show answer
The caller may need to combine the procedure's changes with other work and either commit or roll back everything as one unit.