Return enum instead of bool from function for clarity ?
- by Moe Sisko
This is similar to :
http://stackoverflow.com/questions/2908876/net-bool-vs-enum-as-a-method-parameter
but concerns returning a bool from a function in some situations.
e.g.
Function which returns bool :
public bool Poll()
{
bool isFinished = false;
// do something, then determine if finished or not.
return isFinished;
}
Used like this :
while (!Poll())
{
// do stuff during wait.
}
Its not obvious from the calling context what the bool returned from Poll() means.
It might be clearer in some ways if the "Poll" function was renamed "IsFinished()", but the method does a bit of work, and (IMO) would not really reflect what the function actually does. Names like "IsFinished" also seem more appropriate for properties. Another option might be to rename it to something like : "PollAndReturnIsFinished" but this doesn't feel right either.
So an option might be to return an enum. e.g :
public enum Status
{
Running,
Finished
}
public Status Poll()
{
Status status = Status.Running;
// do something, then determine if finished or not.
return status;
}
Called like this :
while (Poll() == Status.Running)
{
// do stuff during wait.
}
But this feels like overkill.
Any ideas ?