Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSharper: if/else if vs switch in C#

So I was discussing with a colleague the benefits of ReSharper (currently v8.1) in terms of refactoring if/else if statements to switch statements. I had not given this much thought but my colleague came up with the example below. The thing is that when the code is run you can see that it actually gets to "else" statement in the DoElseIf but not in the DoSwitch. Even so ReSharper suggests that I refactor my if/else if to a switch statement when it's obvious that the compiled code does not behave the same way.

Can anyone with more knowledge of ReSharper than me tell me if I'm looking at this the wrong way or if I should be careful refactoring an if/else if to a switch statement?

Here's the code:

namespace ConsoleApplication1
{
   class Program
   {
          static bool MyValue { get; set; }
          static void Main(string[] args)
          {
                 Task t = new Task(ChangeIt);
                 t.Start();

                 for (var i = 0; i < 1000000; i++)
                 {
                       DoElseIf();
                 }
                 for (var i = 0; i < 1000000; i++)
                 {
                       DoSwitch();
                 }

                 Console.WriteLine("Work's done!");
                 Console.ReadLine();
          }

          static void DoSwitch()
          {
                 switch (MyValue)
                 {
                       case true:
                              //Console.WriteLine("true");
                              break;
                       case false:
                              //Console.WriteLine("false");
                              break;
                       default:
                              Console.WriteLine("WTF (Switch)!");
                              break;
                 }
          }

          static void DoElseIf()
          {
                 if (MyValue == true)
                 {
                       //Console.WriteLine("true");
                 }
                 else if (MyValue == false)
                 {
                       //Console.WriteLine("false");
                 }
                 else
                 {
                       Console.WriteLine("WTF (Else-if)!");
                 }
          }

          public static void ChangeIt()
          {
                 MyValue = !MyValue;
                 Task.Factory.StartNew(ChangeIt);
          }
   }
}

Thanks in advance and happy holidays to all :-)

like image 306
CJe Avatar asked Dec 23 '13 10:12

CJe


People also ask

Can you convert a switch statement to an equivalent if statement or vice versa?

You can always convert a switch statement to an equivalent if statement. You can always convert an if statement to a switch statement. The break keyword must be used in a switch statement; otherwise, a syntax error occurs. The default case must be specified in a switch statement.

Is switch better than if-else C#?

The . NET framework and the C# language provide two methods for conditional processing where multiple discrete values can be selected from. The switch statement is less flexible than the if-else-if ladder but is generally considered to be more efficient.

How do I change if-else to switch?

Place your cursor in the if keyword. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select from the following two options: Select Convert to 'switch' statement.

Is a switch statement faster than if?

A switch statement is significantly faster than an if-else ladder if there are many nested if-else's involved. This is due to the creation of a jump table for switch during compilation. As a result, instead of checking which case is satisfied throughout execution, it just decides which case must be completed.


2 Answers

Your example is a very specific case where you are running an if statement within a not thread safe loop, meaning you expect another task to change the value of a variable while evaluating it.

Usually you want to prevent such cases because this can lead to very bad issues.

And regarding your question, how should ReSharper know about this fact? Usually, if you do an if/elseif, we can expect that the statement gets evaluated correctly right?

In addition, the switch even helps you prevent this kind of mess, so in my opinion, R# is right ;)

But of course, you can not trust R# blindly without double checking the suggestions. Often some suggestions are crap and you also have to configure R# to match with your code style guidelines etc... It's just a tool ;)

like image 88
MichaC Avatar answered Oct 07 '22 14:10

MichaC


R# is a very helpful tool that helps you to write better code. Does it suggestions never break working code? No, that's also the very reason why there is no "yes to all proposed refactorings" button.

To answer your question a bit more generic: You should always understand what R# changes about your code. You are the programmer.

PS: When you find a scenario where R# breaks working code, have another look at that particular piece of code, it might be of poor quality already.

PPS: I use R# for a very long time already, and never ran into an example where R# proposed to implement a bug :)

like image 44
bas Avatar answered Oct 07 '22 14:10

bas