9

Assuming you have functions which take an increasing number of optional parameters (and a pseudo language which does not allow optional parameters and an arbitrary number of parameters) like this:

function doStuff(Param a)
function doStuff(Param a, Param b)
function doStuff(Param a, Param b, Param c)

What is a good way to unify them to a single call? I can imagine two designs:

Fall through everything:

function doStuff(Param a)
    doStuff(a, defaultB)

function doStuff(Param a, Param b)
    doStuff(a, b, defaultC)

function doStuff(Param a, Param b, Param c)
    // magic goes here
  • The Upside: Not much code duplication, easy changing of a single default parameter.
  • The Downside: Many function calls, might render Stacktraces harder to read or useless.

Or directly call the 'last' function:

function doStuff(Param a)
    doStuff(a, defaultB, defaultC)

function doStuff(Param a, Param b)
    doStuff(a, b, defaultC)

function doStuff(Param a, Param b, Param c)
    // magic goes here
  • The Upside: Not many function calls, not worries if a new function is added into the chain.
  • The Downside: Much code duplication, especially the default values.

What did I not think of?

2
  • 1
    "new function is added" (meaning "new param"?) seems no harder in first example. What IS slightly harder in the first example is dropping an option (like so that user must specify both a and b or neither), but the first still wins for minimizing bug duplication. BTW, typo: Middle overload implementation should be identical between the examples.
    – Paul Martel
    Jan 13, 2012 at 13:22
  • CodeReview is for reviewing actual code, since you are presenting psuedocode it isn't on-topic there. Jan 13, 2012 at 16:15

3 Answers 3

6

I prefer the first method. The possibility of duplicated default values all over the place can severely reduce maintainability. I've encountered that specific issue in the wild, and it was a nightmare.

2

The first is just two extra lines in a stacktrace which isn't so much. In Java the I've never found stacktraces hard to read because of similar method names.

Anyway, using too much parameters could be a bad code smell: How many parameters are too many?

1
  • Ahrm, assume an arbitrary number of methods/parameters.
    – Bobby
    Jan 13, 2012 at 11:29
0

I would opt for the second paradigm (directly calling the "last" function.). This is fine, and it does not constitute real code duplication, because it does not involve manipulation of any kind of state.

It also helps to realize that what you are doing by providing multiple overloads is simply a trick just so as to provide the illusion of optional parameters. Once you see it this way, it becomes obvious that the "last" and only the "last" method is the one which is supposed to do any real work, so any delegation among the non-last methods is just pointless.

Personally, I do not have to worry about this problem anymore, because I am using C#, which supports optional parameters, thus sparing me from having to do tricks like these in order to emulate them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.