Suppose we are writing a GUI toolkit in C++ (though this question may also apply to other languages). We have a button class with a member function hide, which hides the button. This member function takes a Boolean parameter animated to control if the button should be hidden with an animation.
class Button {
public:
// Rule of three, etc…
void hide(bool animated);
};
When invoking this member function, it may not be clear what it does.
Button button;
button.hide(false); // well, does it hide the button or not?
// what does "false" even mean here?!
We could rewrite this using a Boolean enum.
class Button {
public:
// Rule of three, etc…
enum Animated : bool {
Animate = true,
DoNotAnimate = false,
};
void hide(Animated animated);
};
Now if we call it, everything becomes more clear.
Button button;
button.hide(Button::DoNotAnimate);
Is this a good thing to do? Does it improve clarity of the code, or is it just overkill and should we use separate documentation (Doxygen-like) for this instead?
button.hide(animated: false);, for example. Otherwise it may be a good idea to try to use more general-purpose enums if possible, rather than overly-specific ones, but I would definitely prefer it to thehide(false)version. Another possibility is to use two separate named methods, maybehideWithAnimationandhideNoAnimationor something. \$\endgroup\$