Barr J
Barr J

Reputation: 10929

C# switch clause on boolean variable

I have a piece of code, that is checking if a boolean variable is true and based on the condition, I perform operations accordingly:

bool result = true;
bool isTrue = false;
CheckIsTrue(ref isTrue);

if (result)
{
   if (isTrue)
   //Perform action
}

I have a need to perform another operation, if the variable is set to false:

 if (result)
 {
    if (isTrue)
    {
      //Perform action
    } 
    else if(actionType == 6)
    {
      //Perform action, isTrue = false.
    }
 }

For readability and maintainability reason, I decided to change the above to:

 if (result)
 {
     switch (isTrue)
     {
       case true:
               //perform action on isTrue
           break;
              default:
                   switch (actionType)
                   {
                      case 6:
                         //Perform action on 6
                       break;
                          default:
                        //Perform default action        
                       break;
                   }
            break;
     } 
}

My question is: is it smart to use swicth.. case... on boolean variables? This is the best way I have considered to simplify my code, however I am not sure on how correct this truly is.

Upvotes: 1

Views: 10851

Answers (7)

Stephen Quan
Stephen Quan

Reputation: 25966

In C# 8 you can match tuples:

switch (result, isTrue, actionType)
{
    case (true, false, 6):
        // Perform action on 6
        // Perform default action
        break;
    case (true, true, _):
        // Perform action
        break;
    default:
        break;
}

Upvotes: 1

Barr J
Barr J

Reputation: 10929

Thank you all for your help! What I ended up doing, for number of reasons was:

 if (result)
 {
    if (isTrue)
    {
      //Perform action
    } 
    else 
    {
       switch (actionType)
       {
          case 6:
              //Perform action on 6
                break;
                   default:
                      //no condition mathed - write to log     
                break;
       }
    }
 }

I did prefer this approach and the reasons are:

First - If the type of the actionType variable changes, all I need to do is a small change in the case, instead of dealing with it inside else-if clause.

Second, if another condition is added I just need to add another case, instead of building a big nested else-if clause over the years.

Third - It is easier to maintain log record if no case is matched, inside the default case.

I considered all of the answers during the weekend and accepted @Sergey Berezovskiy answer, as the answer and link he posted helped me come to my final conclusion.

Upvotes: 0

Michał Turczyn
Michał Turczyn

Reputation: 37377

I think switch statement isn't good choice for boolean variables. Just compare these codes:

if(boolVariable)
{
  //...
}
else
{
  //...
}

and it's equivalent

switch(boolVariable)
{
  case true:
    //...
    break;
  case false: // or default:
    //...
    break;
}

IMO if statement is more cleaner and readable and maintainnable :)

Upvotes: 2

Ricardo Alves
Ricardo Alves

Reputation: 1121

On my opinion, the first code was more readable than that monster. If you're using C# 7 or higher, you can write the code like this:

switch (result)
{
    case true when isTrue:
        //Here is the code when both result and isTrue are true
    break;
    case true when actionType == 6:
        //Here is the code when both result and actionType is 6
    break;
    default:
        //Here defaultaction
    break;
}

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

With only one level of indentation and proper variable names:

if (!actionShouldBePerformed) // instead of result
   return;

if (defaultActionShouldBePerformed) // insted of isTrue
{
   //Perform action
   return;
}

if (actionType == ActionType.NameOfType) // instead of magic number 6
   // Perform another action   

Further reading: Replace Nested Conditional with Guard Clauses

Upvotes: 3

Buda Gavril
Buda Gavril

Reputation: 21657

I would not use switch clause for bool values. A more readable form of your code would be:

        if (!result)
            return;

        if (isTrue)
        {
            // do action
        }
        else if (actionType == 6)
        {
            // do something
        }
        else
        {
            // do the default action
        }

But this is not a good example of OOP code, I would suggest you to read about SOLID principles.

Upvotes: 1

Teun van der Wijst
Teun van der Wijst

Reputation: 1019

Its not really wrong, but I wouldn't consider the last code block to be more readable. Personally, I would stick with if ... else, like this:

if (result) {
  if (isTrue) {
    // perform action
    return;
  } else if (actionType == 6) {
    isTrue = false;
    // perform action
    return;
  }
  // perform default action
}

Upvotes: 1

Related Questions