Skip to Main Content
  • Questions
  • Which is better? Overloading vs default parameter

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Evan.

Asked: January 11, 2016 - 11:38 pm UTC

Last updated: January 12, 2016 - 1:37 am UTC

Version: 11

Viewed 1000+ times

You Asked

Hi,

I've run into an interesting implementation problem. It's something I've encountered before, not just in Oracle but in other programming languages too. This time I've decided to post it on a forum to see what other people think.

I'm implementing a procedure and I have a choice between creating one procedure that has a parameter with a default value or overloading a procedure with two implementations, one with no parameters and one with one parameter.

Option 1 - One procedure with a default value
    PROCEDURE some_procedure (
        some_parameter          varchar2        default null
    ) IS
    BEGIN
        IF some_parameter is null THEN
            --Run Code Block 1
        ELSE
            --Run Code Block 2
        END IF;
    END some_procedure;


Option 2 - Overloaded procedure
 
    PROCEDURE some_procedure
    IS
    BEGIN
        --Run Code Block 1
    END some_procedure;

    PROCEDURE some_procedure (
        some_parameter          varchar2
    ) IS
    BEGIN
        --May need to check to make sure parameter is not null

        --Run Code Block 2
    END some_procedure;

Is there a particular option that is better than the other? Or does it depend on the situation? Or is it really just a matter of personal preference?

Any thoughts, comments, insights, etc. would be appreciated.

Thanks in advance.

and Connor said...

I think either are useful.

For example, consider two valid usages of a proc

proc A(parm1,parm2,parm3);

proc A(parm1,parm2,parm3,parm4,parm5,parm6);

I'd overload them, because if I went down the 'default route, I then need to add code to ensure people dont call the proc with 4 or 5 parameters, because I want to limit it to only 3 or 6.

As for a "rule", my preference is similar to my logic above, in that, I try to come up with *usage* rules that make sense to the application. I'll explain with another example:

proc A(p1,p2) already exists. My intention is to change it to be proc A(p1,p2,p3). It still does the same basic function, its just been enhanced due to application requirements. I'd probably go with

proc A(p1,p2,p3 default null)

so that new apps can use the '3' parameters, and as yet unchanged apps can still call the old one. As they are upgraded, eventually the proc would just become: proc A(p1,p2,p3). I didnt overload because in reality I was still dealing with a single function...I just had a transient need due to the way I roll out application changes.

If I was building a radically changed function, that only some apps will use, then I'd probably go overload, ie proc A(p1,p1,p2,p4) because whilst the function ("A") is the same, the implementation is so different that it warrants a separate code base.

Finally..sometimes downtime imposes the need. I can add a new procedure to a package spec without invalidating any dependencies, so sometimes I'll go with an overload simply so I can roll out a fix online without no outage.

Hope this helps.

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library