I have a boolean constant that I use to determine whether I do an action. I'm quite fond of ternary operators because they look clean in most scenarios.
In my case, the action should be done if the constant is true and nothing should happen if the constant is false.
To me, I would handle it like this
BOOLEAN_CONSTANT ? doAction() : false;
but I'm unsure about whether this is "correct".
Is there a documented and/or recommended way to use the ternary operator with 1 argument?
If there isn't, i'll just use an if statement, but they take up more lines (unless you use a single line if which isn't part of the coding paradigm I follow).
It seems as if this question provokes opinionated responses down to the preferred coding style of a person. I follow a certain style that suits what I do, if you follow a different one that's completely okay.
I considered closing this thread, but I'll leave it to a moderator since I can't judge whether its existence would be classified as useful by SO guidelines.
You can use &&
to evaluate the second expression only if the first expression is truthy:
BOOLEAN_CONSTANT && doAction();