Suppose multiple comparations are required at once:
enum usermode
{
active,
standingby,
inactive,
dead,
// many other modes....
};
class A
{
public:
usermode mode;
};
function inherited pointer to class A (ptr points to A)
Method A:
if( ptr->mode == active || ptr->mode == standingby || ptr->mode == inactive || ptr->mode == dead ...//etc )
{
//do something
}
Method B:
usermode cmpmode = ptr->mode;
if( cmpmode == active || cmpmode == standingby || cmpmode == inactive || cmpmode == dead ...//etc )
{
//do something
}
Is it a good practice to do so?
In this case it simply cuts down on the amount of characters in the
ifstatement expression, so whatever works and is most readable.However, in the case of checking the return value of a function, I would stuff it into a variable and check that, i.e.,
Taking your
ifstatement at face value, I would do this:If this is a common check, you may want to make a special case for it, i.e.,