如何降低许多 if 的复杂性来检查相同的值,我正在尝试清理我的代码,但在这种情况下我面临着非常高的复杂性!!
PS它不是if...else case它只是很多ifs抛出异常!
void function(String text){
if(text==null)
throw new exception();
if(text.isEmpty())
throw new Exception();
if(text=="test")
throw new Exception();
..... }
由于每个条件都做同样的事情,你可以将它们放在同一个 if 语句中:
if(test==null || text.isEmpty() || test="test" == 0)
{
throw new Exception();
}
您无法删除这些案例,因为它们是不同的。但是,由于我不知道您使用的语言,因此您可以使用:“C# 中的 isNullOrEmpty()”来简化 null 和 isempty 检查,此外,您应该对 test="test" 使用字符串比较。
除非必要,否则最好不要抛出异常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句