如何简化复杂的 if then 条件?
How to simplify complicated if then conditions?
我有一种情况,我必须在 if 语句中排除不断增长的条件列表,这些条件列表开始时相当简单
我考虑过循环 list/array 并尽可能使用 RegEx。
我只想知道其他人做了什么来尝试和简化这些情况。
这是一个简单的例子:
if (!DataField.Contains(strText) & (strOtherText.IndexOf(strFindThis) < 0) || Regex.isMatch(strWithLetters, @"[A-Z]"))
{
//Do things;
}
else
{
//Do Other Things;
}
仍在学习 RegEx,但我正在努力
到目前为止,它是由几个单词或某些字符组成的字符串。
例如:
- "#、&、^、@ 等.."
- "Form 9999-99 invalid client name"
- 不能以 "https" 或 "http"
开头
- 在某些情况下,如果它包含像 "Error Code" 这样的字符串,那么它将被保存在备用文件中,而不是简单地被忽略以进行处理
到目前为止我有 23 个
这实际上取决于您的条件表达式,但您可以使用类似的东西:
List<Func<bool>> expressions = new List<Func<bool>>();
expressions.Add(()=>anyConditionalExpression());
if(expressions.All(x=>x.Invoke())){
//do something;
}
else{
//do something else;
}
是的,没有看到你的条件很难给出具体的答案,但总的来说:
每个条件另起一行。
如果您有一组条件执行相同的操作但输入不同,您可以将它们抽象为一个函数,该函数可同时检查多个条件。
例如,
if(str1 != null && str1.Length > 5 && str2 != null && str2.Length > 5)
{...}
进入
bool isNice(str)
{
return str != null && str.Length > 5;
}
if(isNice(str1) &&
isNice(str2))
{...}
我会创建一个接口来实现您需要支持的各种验证标准。完成后,可以将实施对象放入一个集合中,该集合可以传递给验证方法。
public interface IValidator
{
bool IsValid(String currentLine);
}
将实施具体 类 以支持您的规则:ContainsStringValidator
和 RegexMatcherValidator
从您上面的示例中浮现在脑海中。
List<IValidator> rules = new ArrayList<IValidator>();
rules.Add(new ContainsStringValidator("StringToLookFor"));
rules.Add(new RegexMatcherValidator(@"[A-Z]"));
现在规则已收集到一个可管理的单元中,您可以编写代码来解析文件。
public void CheckLine(String line, List<IValidator> rules)
{
foreach (IValidator v in rules)
{
if (v.IsValid(line))
{
// it works
}
else
{
// do something when bad
}
}
}
我有一种情况,我必须在 if 语句中排除不断增长的条件列表,这些条件列表开始时相当简单
我考虑过循环 list/array 并尽可能使用 RegEx。
我只想知道其他人做了什么来尝试和简化这些情况。
这是一个简单的例子:
if (!DataField.Contains(strText) & (strOtherText.IndexOf(strFindThis) < 0) || Regex.isMatch(strWithLetters, @"[A-Z]"))
{
//Do things;
}
else
{
//Do Other Things;
}
仍在学习 RegEx,但我正在努力
到目前为止,它是由几个单词或某些字符组成的字符串。
例如:
- "#、&、^、@ 等.."
- "Form 9999-99 invalid client name"
- 不能以 "https" 或 "http" 开头
- 在某些情况下,如果它包含像 "Error Code" 这样的字符串,那么它将被保存在备用文件中,而不是简单地被忽略以进行处理
到目前为止我有 23 个
这实际上取决于您的条件表达式,但您可以使用类似的东西:
List<Func<bool>> expressions = new List<Func<bool>>();
expressions.Add(()=>anyConditionalExpression());
if(expressions.All(x=>x.Invoke())){
//do something;
}
else{
//do something else;
}
是的,没有看到你的条件很难给出具体的答案,但总的来说:
每个条件另起一行。
如果您有一组条件执行相同的操作但输入不同,您可以将它们抽象为一个函数,该函数可同时检查多个条件。
例如,
if(str1 != null && str1.Length > 5 && str2 != null && str2.Length > 5)
{...}
进入
bool isNice(str)
{
return str != null && str.Length > 5;
}
if(isNice(str1) &&
isNice(str2))
{...}
我会创建一个接口来实现您需要支持的各种验证标准。完成后,可以将实施对象放入一个集合中,该集合可以传递给验证方法。
public interface IValidator
{
bool IsValid(String currentLine);
}
将实施具体 类 以支持您的规则:ContainsStringValidator
和 RegexMatcherValidator
从您上面的示例中浮现在脑海中。
List<IValidator> rules = new ArrayList<IValidator>();
rules.Add(new ContainsStringValidator("StringToLookFor"));
rules.Add(new RegexMatcherValidator(@"[A-Z]"));
现在规则已收集到一个可管理的单元中,您可以编写代码来解析文件。
public void CheckLine(String line, List<IValidator> rules)
{
foreach (IValidator v in rules)
{
if (v.IsValid(line))
{
// it works
}
else
{
// do something when bad
}
}
}