I have a procedure which performs some task, but performs it in two slightly different ways depending on its input. Specifically it extends a suffix in a suffix tree; if the suffix ends at a node the case is simple, but when the suffix ends within a leaf edge some additional work is required. I mention this detail to explain why I have included these two behaviours in a single function (it mirrors “Rule 2” of Dan Gusfield’s description of Ukkonen’s algorithm for suffix tree construction: http://www.stanford.edu/~mjkay/gusfield.pdf).
Anyway, once the function completes this work, the caller needs to know which of the two cases were followed. I thought an enum would be a good way to share this information, as it makes the cases explicit (as opposed to arbitrarily mapping the cases to bools or ints).
TLDR: To share information with the caller, should I pass an enum by reference to this procedure or return an enum? I feel like passing an enum by reference is better because it avoids having a ‘function with side effects’ but would like to know if there is generally ‘right’ way of doing this. Or, alternatively, does this really suggest that I should be replacing my single procedure with two separate procedures?
In my opinion it makes more sense to return the enum, since it is the result from calling the function. Using a reference would make much more sense if you were passing a value to the function, and the function was supposed to modify that value.
You can see a similar example in
insertmethod fromunordered_mapin the C++ standard library:That method returns a boolean (within a pair) stating whether the value was inserted in the map or not. This actually a very similar case as the one you are dealing with.