Remove redundant if

I just ran into a situation in which I found I could easily refactor a
method to remove a variable along with various if statements. Near the last
step, I was left with a few lines of code in an if(value == 0) block and I
wanted to remove the if statement since it is now redundant; e.g.

private int Blah(string value)
{
int temp = 0;
...
if(temp == 0)
{
...
return blah;
}

return temp;
}

Since there are no further assignments to temp (other than initialization),
I thought ReSharper would have picked up the dead temp variable along with
the dead if statement. I suspect ReSharper didn't because it can't tell at
this stage that the return statement is redundant because the if statement
is in the way. At least this seems a logical conclusion.

Anyway, I wanted ReSharper to help remove the if statement so I changed it
to if(true) to guide it along. Unfortunately, the only option available was
to invert the if statement, which I did:

if(false)
{
}
else
{
...
}

It then prompted to remove the redundant else and finally to remove the
redundant and empty if(false) statement.

It works, but it seems to be overly complicated.


1

Please sign in to leave a comment.