Invert "if" statement to reduce nesting braces behavior

I have fairly recently started using ReSharper and I LOVE it.  However there is one thing that confuses me a little bit.  When a get the suggestion to 'Invert if statement to reduce nesting' it usually works just as I would expect.  However once it a while it inverts it and leaves braces around the remaining code which if I'm not mistaken are unnecessary.  What makes the difference on what it does? 

For example I have this piece of code:

 

if (goodIds.Count > 0)
{
    List<Element> elems = goodIds.Select(x => d.GetElement(new ElementId(x))).ToList();
    var t = new Transaction(d, "Update Key Values");

    if (t.Start() == TransactionStatus.Started)
    {
        foreach (Element e in elems)
        {
            if (e.get_Parameter(BuiltInParameter.KEYNOTE_PARAM) != null)
            { e.get_Parameter(BuiltInParameter.KEYNOTE_PARAM).Set(k.NewKey); }

            if (e.get_Parameter(BuiltInParameter.KEY_VALUE) != null)
            { e.get_Parameter(BuiltInParameter.KEY_VALUE).Set(k.NewKey); }
        }

        t.Commit();
    }
    else
    { Utility.TelerikAlert("Failed", "Failed to update key values; transaction failed to start."); }

}

 

ReSharper rightly suggests inverting the if statement, but when I do I get this:

if (goodIds.Count <= 0) continue;
{
    List<Element> elems = goodIds.Select(x => d.GetElement(new ElementId(x))).ToList();
    var t = new Transaction(d, "Update Key Values");

    if (t.Start() == TransactionStatus.Started)
    {
        foreach (Element e in elems)
        {
            if (e.get_Parameter(BuiltInParameter.KEYNOTE_PARAM) != null)
            { e.get_Parameter(BuiltInParameter.KEYNOTE_PARAM).Set(k.NewKey); }

            if (e.get_Parameter(BuiltInParameter.KEY_VALUE) != null)
            { e.get_Parameter(BuiltInParameter.KEY_VALUE).Set(k.NewKey); }
        }

        t.Commit();
    }
    else
    { Utility.TelerikAlert("Failed", "Failed to update key values; transaction failed to start."); }

}

 

Shouldn't it be this?

if (goodIds.Count <= 0) continue;

List<Element> elems = goodIds.Select(x => d.GetElement(new ElementId(x))).ToList();
var t = new Transaction(d, "Update Key Values");

if (t.Start() == TransactionStatus.Started)
{
    foreach (Element e in elems)
    {
        if (e.get_Parameter(BuiltInParameter.KEYNOTE_PARAM) != null)
        { e.get_Parameter(BuiltInParameter.KEYNOTE_PARAM).Set(k.NewKey); }

        if (e.get_Parameter(BuiltInParameter.KEY_VALUE) != null)
        { e.get_Parameter(BuiltInParameter.KEY_VALUE).Set(k.NewKey); }
    }

    t.Commit();
}
else
{ Utility.TelerikAlert("Failed", "Failed to update key values; transaction failed to start."); }

 

Often on similar code I do get the second option but once in a while it leaves the braces like that.  Is there something I'm missing as to why this happens?

 

0
3 comments

Hello Stephen!

 

Thank you for contacting us.

Could you please specify R# version you are using?

Thank you.

0

Sure, I'm currently using 2017.2, but I feel like I've noticed it since I started using R# which was probably roughly a year ago...

0

Thank you for the reply.

 

Strangely I cannot reproduce the problem on the latest R# versions.

Could you please provide some sample solution introducing the issue?

Thank you.

0

Please sign in to leave a comment.