Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Resharper (or is it Visual Studio - it's a yellow light bulb with a tiny red filament in it) offers to "invert if" on this code:

if (tItems >= 0)
{
    return tItems;
}
SetTotalItemsAndTotalAmt();

...and if I allow it to, it becomes this:

if (tItems < 0)
{
    SetTotalItemsAndTotalAmt();
}
return tItems;

...but then it offers to "invert if" again, and if I gullibly take the bait, it sets it back to its former glory.

If one way is considered better (more grokkable, I guess) than the other, why would it also allow a reverse inversion? And if it's a case of "six of one and half a dozen of the other," why does it even bother to offer a change?

UPDATE

Actually, come to think of it, one of those ways is not even valid, for it says, "WorkFiles.getAmount()': not all code paths return a value" unless it has the return statement outside the if condition.

My guess is this is not Resharper doing this; those Urquell-powered cats wouldn't do such a thing, methinks.

share|improve this question
2  
Be honest: how often did you click on "OK" before you got bored and came here to ask that question? –  itsbruce Aug 29 '13 at 22:53
1  
Only seventy-eleven. –  B. Clay Shannon Aug 29 '13 at 23:05
add comment

1 Answer

It's ReSharper that's offering this, and it offers it because it can. There can be many good reasons to let it do so. I frequently use this when refactoring code like:

if (cond1) {
   if (cond2) {
      if (cond3) {
         DoSomething();
      } else {
         throw "Error 3";
      }
   } else {
      throw "Error 2";
   }
} else {
   throw "Error 1";
}
return;

into:

if (!cond1) throw "Error 1";
if (!cond2) throw "Error 2";
if (!cond3) throw "Error 3";
DoSomething();
return;

It's especially useful when those ifs run on for several screens of code. And yes, I see code like this often.

share|improve this answer
1  
So it's just that sometimes Resharper offers a refactor that doesn't add substantial value. –  Robert Harvey Aug 30 '13 at 0:15
    
Not "doesn't add substantial value", but rather "is not a good or bad thing, but is a thing that might be troublesome to do without a single-button implementation." –  Ross Patterson Aug 30 '13 at 18:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.