class Thing {
private boolean _visible;
boolean isVisible() {
return _visible ? true : false;
}
}
This code is unnecessarily using the ternary conditional operator when it's not needed.
Occasionally developers get so captivated by the expressiveness of this operator that they find it tempting to use it when it's not really needed. In this particular example, it almost looks like the original writer forgot that the type of the _visible field was boolean anyway and could just be returned directly:
class Thing {
private boolean _visible;
boolean isVisible() {
return _visible;
}
}

2 comments: