开发者

Multiple equal ifs should return different values from different methods (C#)?

开发者 https://www.devze.com 2023-03-07 05:08 出处:网络
I have some methods which use if conditions, like this: string method1() { bool x= Convert.ToBoolean(...);

I have some methods which use if conditions, like this:

string method1()
{
    bool x= Convert.ToBoolean(...);
    bool y= Convert.ToBoolean(...);

     if (x && y)
     {
         return " ";
     }

     if (!x && y)
     {
         return " ";
      }

      if (!y && x)
      {
          return " ";
      }

      if (!x && !y)
      {
          return "X";
      }

      return " ";
  }

Thats my first method, now I have a similiar one, which has the开发者_开发百科 same checks and the same boolean values, but returns other strings (not space or X). Whats an elegeant approach to solve this?

Thanks


According to your code you only return "X" if both x and y are false. In that case every other combination should return " ". This could be shortened but I left it as is for readability.

method1("X", " ");
method1("OtherValue", " ");

string method1(string matchValue, string nonMatchValue)
{
    bool x= Convert.ToBoolean(...);
    bool y= Convert.ToBoolean(...);

      if (!x && !y)
      {
          return matchValue;
      }

      return nonMatchValue;
  }


The elegant approach is to refactor your code so that the boolean checks are in their own method that each of the other two methods can access, and then each method can return a string based on the result of the boolean check.

EXAMPLE

private int BinaryTruthTest(bool x, bool y)
{
   if(x && y)
   {
      return 3;
   }
   if(!x && y)
   {
      return 1;
   }
   if(!y && x)
   {
      return 2;
   }
   if(!x && !y)
   {
      return 0;
   }
}

string method1()
{
   bool x = Convert.ToBoolean(...);
   bool y = Convert.ToBoolean(...);

   int testResult = BinaryTruthTest(x, y);

   if(testResult==0)
   {
      return "X";
   }
   else
   {
      return " ";
   }
}


Why not pass the desired return values as parameters to the method?

string method1(string returnVal1, string returnVal2)
{
    bool x= Convert.ToBoolean(...);
    bool y= Convert.ToBoolean(...);

    if (x && y)
    {
        return returnVal1;
    }

    if (!x && y)
    {
        return returnVal1;
    }

    if (!y && x)
    {
        return returnVal1;
    }

    if (!x && !y)
    {
        return returnVal2;
    }

    return returnVal1;
}


Not sure what you're trying to do, but if you want a generic version of that method where only the results change, you could pass the result as a 2-dimension array....

string method1(string[][] result) {
   bool x= Convert.ToBoolean(...);
   bool y= Convert.ToBoolean(...);
   int u = (x ? 1 : 0);
   int v = (y ? 1 : 0);
   return result[u][v];
}


IF you want method two to return four different values for the four different cases then you could do:

string method1(string[] values)
{
if (x && y)
 {
     return values[0];
 }

 if (!x && y)
 {
     return values[1];
  }

  if (!y && x)
  {
      return values[2];
  }

  if (!x && !y)
  {
      return values[3];
  }

}

However that is not very pretty. What is the use of this method in your application?


It depends on the real-world problem, but I suspect you need a new class that can be reused anywhere:

class DecisionMaker
{
    string _both;
    string _notX;
    string _notY;
    string _neither;
    string _default;

    public DecisionMaker(string both, string notX, string notY, string neither, string defaultVal)
    {
        _both = both;
        _notX = notX;
        _notY = notY;
        _neither = neither;
        _default = defaultVal;
    }

    public string Method1(bool x, bool y)
    {
        if (x && y)
            return _both;

        if (!x && y)
            return _notX;

        if (!y && x)
            return _notY;

        if (!x && !y)
            return _neither;

        return _default;
    }
}

Or optionally, you can have the Convert.ToBoolean(...) stuff inside Method1 and make Method1 have no parameters. Or you could create a DecisionMakerBase abstract class with protected abstract methods that allow you to only change what functionality you need to change. It really depends on your scenario.

0

精彩评论

暂无评论...
验证码 换一张
取 消