24

Hey all, looking to reduce the code on my c# if statements as there are several repeating factors and was wondering if a trimmer solution is possible.

I currently have 2 if statements that need to carry out an identical statement, however the only variable is an extra condition on an if statement when a checkbox is not checked. Im just wondering if there is a way to make it one statement or make the condition string variable, heres the compressed version of the code:

if (checkbox.checked)
  {
    if (columnname != a && columnname != b && columnname != c)
    {
      "statement 1"
    }
  }
else
  {
    if (columnname != a && columnname != b && columnname != c 
        && columnname != A2)
    {
      "statement 1"
    }
  }

Its like I need to run an if statement within the conditions of an if statement if that makes sense, like this psuedo form:

if (columnname != a 
    && columnname != b 
    && columnname != c 
    && if(checkbox.checked{columnname != A2})
skaffman
  • 398,947
  • 96
  • 818
  • 769
markdigi
  • 1,242
  • 6
  • 15
  • 26

12 Answers12

50

Use the && (and) operator in combination with the || (or) operator in a nested condition as so:

if (columnname != a 
  && columnname != b 
  && columnname != c
  && (checkbox.checked || columnname != A2))
{
   "statement 1"
}

Should do the trick.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
  • 6
    usually when my conditions in an if statements are more than three, I extract them to a private method just to make it clear what's the condition is – Prashant Cholachagudda Dec 13 '13 at 10:46
  • @PrashantCholachagudda I don't see how static values being abstracted to somewhere else helps discern 'what they are' in any way. Do you have an example or an article on what that looks like and what the benefit is? – TylerH Jun 09 '22 at 13:57
12
if (columnname != a && columnname != b && columnname != c 
        && (columnname != A2 || checkbox.checked))
    {
      "statement 1"
    }
David Basarab
  • 72,212
  • 42
  • 129
  • 156
mikefrey
  • 4,653
  • 3
  • 29
  • 40
12

I always try to factor out complex boolean expressions into meaningful variables (you could probably think of better names based on what these columns are used for):

bool notColumnsABC = (columnname != a && columnname != b && columnname != c);
bool notColumnA2OrBoxIsChecked = ( columnname != A2 || checkbox.checked );

if (   notColumnsABC 
    && notColumnA2OrBoxIsChecked )
  {
      "statement 1"
  }
markh44
  • 5,804
  • 5
  • 28
  • 33
8

Check this for a more clustered way of checking conditions:

private bool IsColumn(string col, params string[] names) => names.Any(n => n == col);

usage:

private void CheckColumn()
{
     if(!IsColumn(ColName, "Column A", "Column B", "Column C"))
    {
     //not A B C column
    }

}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Sturm
  • 3,968
  • 10
  • 48
  • 78
3

Isn't this the same:

if ((checkbox.checked || columnname != A2) && 
        columnname != a && columnname != b && columnname != c)
  {
      "statement 1"
  }
JBrooks
  • 9,901
  • 2
  • 28
  • 32
3

You could also do this if you think it's more clear:

if (columnname != a 
  && columnname != b 
  && columnname != c
{
   if (checkbox.checked || columnname != A2)
   {
      "statement 1"
   }
}
Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
2

How about maybe something like this?

    var condCheck1 = new string[]{"a","b","c"};
    var condCheck2 = new string[]{"a","b","c","A2"}

    if(!condCheck1.Contains(columnName) && !checkbox.checked)
        //statement 1
    else if (!condCheck2.Contains(columnName))
        //statment 2
BLong
  • 69
  • 4
0
if (checkbox.checked && columnname != a && columnname != b && columnname != c)
    {
      "statement 1"
    }
else if (columnname != a && columnname != b && columnname != c 
        && columnname != A2)
    {
      "statement 1"
    }

is one way to simplify a little.

gjutras
  • 736
  • 1
  • 4
  • 13
0

I think agileguy has the correct answer, but I would like to add that for more difficult situations there are a couple of strategies I take to solve the problem. The first is to use a truth table. If you Google "truth table" you will run across some examples related directly to programming and computer science.

Another strategy I take is to use an anonymous function to encapsulate common logic between various conditions. Create it right before the if block, then use it where needed. This seems to create code that is more readable and maintainable.

Jason Jackson
  • 17,016
  • 8
  • 49
  • 74
0

It's too late but worth the information

C#9 : you can write

if (columnname is not a or b or c)
shaair
  • 945
  • 12
  • 24
0

Like @sal said, C# 9.0 added improved pattern matching. You can now use is with and and or.

With this, the code:

if (checkbox.checked)
  {
    if (columnname != a && columnname != b && columnname != c)
    {
      "statement 1"
    }
  }
else
  {
    if (columnname != a && columnname != b && columnname != c 
        && columnname != A2)
    {
      "statement 1"
    }
  }

Can be reduced to:

if (checkbox.checked)
  {
    if (columnname is (!= a and != b and != c)
    {
      "statement 1"
    }
  }
else
  {
    if (columnname is (!= a and != b and != c and != A2)
    {
      "statement 1"
    }
  }

This pattern matching also added cleaning up switch statements in a similar way. Definitely worth using.

-1
var test = new char[] {a, b, c}.Contains(columnname));

if(test)
{
  "true statement"
}
else
{
   "false statement"
}
frido
  • 13,065
  • 5
  • 42
  • 56
Dan
  • 1
  • 1