您将如何重构具有多个 bool 字段的代码?
How would you refactor code with many bool fields?
例如,我有几种可能的逻辑组合,我需要确定哪些是正确的并根据结果执行操作。
public bool VerificationConnection(Transport objectFirst, Transport objectSecond)
{
bool isCarFirst = objectFirst is Car;
bool isMotorcycleFirst = objectFirst is Motorcycle;
bool isCarSecond = objectSecond is Car;
bool isMotocycleSecond = objectSecond is Motorcycle;
if(isCarFirst && isCarSecond)
{
//do something
}
if(isCarFirst && isMotocycleSecond)
{
//do something
}
if(isMotocycleFirst && isCarSecond)
{
//do something
}
return true;
}
您可以使用派遣 table。对于具有两个布尔字段的用例,委托字典或类似的东西可能就足够了。
Dictionary<(bool, bool), Delegate> dispatch = new Dictionary<(bool, bool), Delegate>();
dispatch[(true, true)] = new Action(() => Console.WriteLine("Hello"));
dispatch[(true, false)] = new Action<string>(s => Console.WriteLine(s));
dispatch[(true, true)].DynamicInvoke();
dispatch[(true, false)].DynamicInvoke("World");
您可以在 switch 语句中使用方便的 pattern matching (C# 7.0 - C# 10.0)。这将两种类型模式组合成一个元组模式:
switch ((objectFirst, objectSecond)) {
case (Car, Car):
DoSomething();
break;
case (Car, Motorcycle):
DoSomething();
break;
case (Motorcycle, Car):
DoSomething();
break;
case (Motorcycle, Motorcycle):
DoSomething();
break;
}
如果你需要强类型的值,你可以使用声明模式;但是,这也可能是一个提示,您的代码没有最佳地使用面向对象的原则。
switch ((objectFirst, objectSecond)) {
case (Car c1, Car c2):
DoSomething(c1, c2);
break;
case (Car c, Motorcycle m):
DoSomething(c, m);
break;
case (Motorcycle m, Car c):
DoSomething(m, c);
break;
case (Motorcycle m1, Motorcycle m2):
DoSomething(m1, m2);
break;
}
如果 switch 语句 returns 一个值,则使用 switch expression 代替。
var value = (objectFirst, objectSecond) switch {
(Car c1, Car c2) => GetValue(c1, c2),
(Car c, Motorcycle m) => GetValue(c, m),
(Motorcycle m, Car c) => GetValue(m, c),
(Motorcycle m1, Motorcycle m2) => GetValue(m1, m2),
_ => null
};
例如,我有几种可能的逻辑组合,我需要确定哪些是正确的并根据结果执行操作。
public bool VerificationConnection(Transport objectFirst, Transport objectSecond)
{
bool isCarFirst = objectFirst is Car;
bool isMotorcycleFirst = objectFirst is Motorcycle;
bool isCarSecond = objectSecond is Car;
bool isMotocycleSecond = objectSecond is Motorcycle;
if(isCarFirst && isCarSecond)
{
//do something
}
if(isCarFirst && isMotocycleSecond)
{
//do something
}
if(isMotocycleFirst && isCarSecond)
{
//do something
}
return true;
}
您可以使用派遣 table。对于具有两个布尔字段的用例,委托字典或类似的东西可能就足够了。
Dictionary<(bool, bool), Delegate> dispatch = new Dictionary<(bool, bool), Delegate>();
dispatch[(true, true)] = new Action(() => Console.WriteLine("Hello"));
dispatch[(true, false)] = new Action<string>(s => Console.WriteLine(s));
dispatch[(true, true)].DynamicInvoke();
dispatch[(true, false)].DynamicInvoke("World");
您可以在 switch 语句中使用方便的 pattern matching (C# 7.0 - C# 10.0)。这将两种类型模式组合成一个元组模式:
switch ((objectFirst, objectSecond)) {
case (Car, Car):
DoSomething();
break;
case (Car, Motorcycle):
DoSomething();
break;
case (Motorcycle, Car):
DoSomething();
break;
case (Motorcycle, Motorcycle):
DoSomething();
break;
}
如果你需要强类型的值,你可以使用声明模式;但是,这也可能是一个提示,您的代码没有最佳地使用面向对象的原则。
switch ((objectFirst, objectSecond)) {
case (Car c1, Car c2):
DoSomething(c1, c2);
break;
case (Car c, Motorcycle m):
DoSomething(c, m);
break;
case (Motorcycle m, Car c):
DoSomething(m, c);
break;
case (Motorcycle m1, Motorcycle m2):
DoSomething(m1, m2);
break;
}
如果 switch 语句 returns 一个值,则使用 switch expression 代替。
var value = (objectFirst, objectSecond) switch {
(Car c1, Car c2) => GetValue(c1, c2),
(Car c, Motorcycle m) => GetValue(c, m),
(Motorcycle m, Car c) => GetValue(m, c),
(Motorcycle m1, Motorcycle m2) => GetValue(m1, m2),
_ => null
};