I have this piece of code:
if (filter != RECENT &&
filter != TODAY &&
filter != WEEK &&
filter != MONTH &&
filter != ALLTIME)
{
filter = RECENT;
}
Note that filter
is a string
and it's compared against const string
types. Is there any way to do this inline and have it be more readable? Doing it with the ternary operator doesn't make it much better since I still have to repeat filter != XXXXX
filter = filter != RECENT &&
filter != TODAY &&
filter != WEEK &&
filter != MONTH &&
filter != ALLTIME ? RECENT : filter;
and this clearly doesn't work
filter = filter != RECENT && TODAY && WEEK && MONTH && ALLTIME ? RECENT : filter;
Is there any prettier way (prettier == all of the logic must be in a single line of code) to do this comparison? More specifically to prevent filter != XXXXX
repetition.
Note that performance is not my primary concern for this question.