Skip to content

Using ReSharper, I’ve Always Wondered Why They Had the “Invert if” refactor

Updated: at 02:12 PM

Well, now I know.  Here is an example of some code I just wrote:

if (doDeficitCalc)
{
    throw 
        new ApplicationException("Need to implement deficit weight rating");
}
else
{
    // Linear interpolation from top of range to bottom for speed
    double x1 = rateBreakList[0];
    double x2 = rateBreakList[rateBreakList.Count - 1];
 
    double y1 = rateList[0];
    double y2 = rateList[rateList.Count - 1];
 
 
    double frac = weight/(x2 - x1);
    double yResult = y1 + (frac*(y2 - y1));
    retWeight = yResult;
}

ReSharper correctly warns me that the else statement is redundant as shown below:

image

So, if I hover over the “if” statement, I get the chance to invert the if clause

image

When I accept the invert if, I now get:

if (!doDeficitCalc)
{
    // Linear interpolation from top of range to bottom for speed
    double x1 = rateBreakList[0];
    double x2 = rateBreakList[rateBreakList.Count - 1];
 
    double y1 = rateList[0];
    double y2 = rateList[rateList.Count - 1];
 
 
    double frac = weight/(x2 - x1);
    double yResult = y1 + (frac*(y2 - y1));
    retWeight = yResult;
}
else
{
    throw
        new ApplicationException("Need to implement deficit weight rating");
}

Which has no warning!!!  Just what I wanted with less key strokes.

Thank you again ReSharper!

Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects