Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

migrated from codereview.stackexchange.com Jan 13 '12 at 16:13

This question came from our site for peer programmer code reviews.

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 '12 at 13:22
    
CodeReview is for reviewing actual code, since you are presenting psuedocode it isn't on-topic there. –  Winston Ewert Jan 13 '12 at 16:15
add comment

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

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?

share|improve this answer
    
Ahrm, assume an arbitrary number of methods/parameters. –  Bobby Jan 13 '12 at 11:29
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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