When writing recursive functions, it sometimes happens that something should happen only on the first pass of the recursive algorithm. When this is true, I have two options,
- Have an optional parameter called “first run” which is set to true by default but when called recursively, the argument is false
- Have two functions
Which option is preferable? If it is the latter, what should I name these functions? (e.g. if its a flood fill algorithm would I choose FloodFill and FloodFillRecursive?)
Thanks in advance, ell.
I might use two functions, and I would say that the function that will be called should be named
FloodFill: the user doesn’t need to know how that function is implemented, so it should not be namedFloodFillRecursive.Actually,
FloodFillRecursivecould be the name of the inner function : the one that contains the implementation, the on that is called by the one called by the user — as it is that second function that is recursive.Ideally, that function should not be visible from the users : it should be kind of hidden in your library (be it trully hidden, or using some naming-convention that informs users they should not call it directly).
And, this way, if you change implementation, you will not have your users call a
FloodFillRecursivefunction that might no be recursive anymore.