HorseloverFat
HorseloverFat

Reputation: 3336

return enum or pass enum by reference?

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?

Upvotes: 1

Views: 1650

Answers (4)

Tuxdude
Tuxdude

Reputation: 49513

Enums are just integer values when they're stored in memory, and compilers enforce check on the range of values used for the enum. Having said that integer data types are efficient and safe to return by value.

Unless you function really returns more than one value, the caller need not pass the enum as a reference and get back the result.

YourEnum result
result = MyFunc()

PS: The rule I usually follow for deciding whether a value should be returned, or make the caller pass a pointer/reference and return the value is if the data returned is a simple or complex data type. ;)

Upvotes: 0

user541686
user541686

Reputation: 210525

I feel like passing an enum by reference is better because it avoids having a 'function with side effects'

Quite the opposite, actually... generally, if you modify your arguments, that's a side effect.

There's no reason to avoid returning anything.

Upvotes: 2

betabandido
betabandido

Reputation: 19694

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 insert method from unordered_map in the C++ standard library:

template <class P> 
std::pair<iterator, bool> insert( P&& value );

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.

Upvotes: 3

Andrew
Andrew

Reputation: 24846

I think it's better tu return the enum as the result of the function since it's more readable and simpler to write:

MyEnum res = myfunc();

Then

MyEnum res;
myfunc(res);

Upvotes: 2

Related Questions