如何重构这种嵌套的if语句?

dfty9e19  于 2021-07-11  发布在  Java
关注(0)|答案(5)|浏览(455)

我在函数的签名中有多个布尔值,因此有多个变量组合,例如:

boolean deleteFile(String path, String searchName, boolean wholeWord, boolean caseSensitive) {

    ...

    if (caseSensitive) {
        if (wholeWord) {
            if (matchesCaseSensitively)
                return file.delete();
        } else if (containsCaseSensitively)
            return file.delete();
    } else {
        if (wholeWord) {
            if (matchesCaseInsensitively)
                return file.delete();
        } else if (containsCaseInsensitively)
            return file.delete();
    }

    return false;    
}

我怎样才能摆脱嵌套 if 语句(可能使用布尔逻辑)?
编辑:既然有很多人在争论可读性,问题是如何以更可读和简洁的方式重构它?

toiithl6

toiithl61#

绕过嵌套条件的最佳实践之一是将它们展开为单独的检查,每个检查一个定义良好的条件(如果条件变得复杂,可以将其抽象为检查函数)。这种方法称为网关式。
因此,您的代码可能类似于:

if ( caseSensitive &&  wholeWord && matchesCaseSensitively) return file.delete()
if ( caseSensitive && !wholeWord && containsCaseSensitively) return file.delete()
if (!caseSensitive &&  wholeWord && matchesCaseSensitively) return file.delete()
if (!caseSensitive && !wholeWord && containsCaseInsensitively) return file.delete()

同样,当条件变得太复杂时,您可能希望用检查函数替换它:

if (checkingFun1()) return file.delete()

更多关于网关样式和气泡样式的信息,请点击这里:[链接]

pdkcd3nj

pdkcd3nj2#

那就不那么可读了,所以这不是一个好主意,但是下面的例子展示了如何对条件进行分组
您可以将条件分组

boolean deleteFile(String path, String searchName, boolean wholeWord, boolean caseSensitive) {
    if (caseSensitive && ((wholeWord && matchesCaseSensitively) || containsCaseSensitively)) {
        return file.delete();
    } else if ((wholeWord && matchesCaseInsensitively) || containsCaseInsensitively) {
        return file.delete();
    }
    return false;
}

甚至在一个

boolean deleteFile(String path, String searchName, boolean wholeWord, boolean caseSensitive) {
    if ((caseSensitive && ((wholeWord && matchesCaseSensitively) || containsCaseSensitively)) ||
        ((wholeWord && matchesCaseInsensitively) || containsCaseInsensitively)) {
        return file.delete();
    }
    return false;
}
ruarlubt

ruarlubt3#

这相当于:

if (wholeWord && (caseSensitive && matchesCaseSensitively || !caseSensitive && matchesCaseInsensitively) ||
    !wholeWord && (caseSensitive && containsCaseSensitively || !caseSensitive && containsCaseInsensitively)) {
    return file.delete();
}
return false;

虽然这在技术上比较简单(即圈复杂度较低),但可能不容易阅读。

jjjwad0x

jjjwad0x4#

你可以这样写:

boolean deleteFile(String path, String searchName, boolean wholeWord, boolean caseSensitive) {

    ...

    if ((caseSensitive && wholeWord && matchesCaseSensitively) || (caseSensitive && !wholeWord && containsCaseSensitively) || (!caseSensitive && wholeWord && matchesCaseInsensitively) || (!caseSensitive && !wholeWord && containsCaseInsensitively)
        return file.delete();       
    }

    return false;    
}
fzsnzjdm

fzsnzjdm5#

另一种方法是简化主流程,并有自己的功能来完成区分大小写和不区分大小写的工作:

...
    if (caseSensitive) {
        return deleteCaseSensitive(file, wholeWord);
    } else {
        return deleteCaseInsensitive(file, wholeWord);
    }
    ...

private boolean deleteCaseSensitive(file, wholeWord) {
    if ((wholeWord && matchesCaseSensitively) 
       || (!wholeWord && containsCaseSensitively)) {
        return file.delete();
    }

    return false;
}

private boolean deleteCaseInsensitive(file, wholeWord) {
   if ((wholeWord && matchesCaseInsensitively) 
      || (!wholeWord && containsCaseInsensitively)) {
        return file.delete();
    }

   return false;    
}

相关问题