user3629533
user3629533

Reputation: 417

Is it worth replacing argument in a switch with a constant?

In my code I have

switch (cd->op)
{
...
}

and I'm wondering whether I should do

CalcWizConsts::eqOps thisOp = cd->op;
switch (thisOp)
{
...
}

Upvotes: 1

Views: 94

Answers (1)

Medo42
Medo42

Reputation: 3821

The argument to switch will only be evaluated once, so there is no need to store it in a temporary first. There is no performance difference, and you don't have to worry about changing the value in one of the case clauses either. The only reason I can think of for assigning to a variable first is to make the code more readable, if the expression is lengthy.

Upvotes: 8

Related Questions