简化if-else条件以降低Java中的认知复杂性

简化if-else条件以降低Java中的认知复杂性,java,sonarqube,Java,Sonarqube,有没有办法简化这个java函数 为了代码的可维护性,需要进行简化 public void pushDocument(ESDocumentType esDocumentType, Object data, String documentId, long userId, long organizationId) { boolean proceed = false; if (esDocumentType.equals(ESDocumentType.XMLACTIVITY)) {

有没有办法简化这个java函数

为了代码的可维护性,需要进行简化

public void pushDocument(ESDocumentType esDocumentType, Object data, String documentId, long userId, long organizationId) {
    boolean proceed = false;

    if (esDocumentType.equals(ESDocumentType.XMLACTIVITY)) {
        proceed = Constants.ELASTIC_LOGGING_ENABLED && Constants.ELASTIC_XMLACTIVITY_ENABLED || Constants.SQS_LOGGING_ENABLED;
    }

    else if (esDocumentType.equals(ESDocumentType.XMLREQRES)) {
        proceed = Constants.ELASTIC_LOGGING_ENABLED && Constants.ELASTIC_XMLREQRES_ENABLED || Constants.SQS_LOGGING_ENABLED;
    }

    else if (esDocumentType.equals(ESDocumentType.ORDERHISTORY)) {
        proceed = Constants.ELASTIC_LOGGING_ENABLED && Constants.ELASTIC_ORDERHISTORY_ENABLED || Constants.SQS_LOGGING_ENABLED;
    }

    else if (esDocumentType.equals(ESDocumentType.SINGIN)) {
        proceed = Constants.ELASTIC_LOGGING_ENABLED && Constants.ELASTIC_SIGNIN_ENABLED || Constants.SQS_LOGGING_ENABLED;
    } else if (esDocumentType.equals(ESDocumentType.GOOGLESEARCH)) {
        proceed = Constants.ELASTIC_LOGGING_ENABLED && Constants.ELASTIC_GOOGLESEARCH_ENABLED || Constants.SQS_LOGGING_ENABLED;
    }

    if (proceed) {
        LogThread logThread = new LogThread();
        logThread.pushDocument(esDocumentType, data, documentId, userId, organizationId);
    }
}

使用switch语句,我认为它应该是这样工作的(未经测试):


使用switch语句,我认为它应该是这样工作的(未经测试):


我不知道您的具体用例,您必须自己对其进行改进,但类似的东西可能会起作用

List<ESDocumentType> enabled; // fill this based on your "Constant.ELASTIC_<BLAH>_ENABLED" constants in the constructor

public void pushDocument(ESDocumentType type, other parameters) {
    boolean proceed = (Constants.ELASTIC_LOGGING_ENABLED && enabled.contains(type)) || Constants.SQS_LOGGING_ENABLED;
    
    if (proceed) {
        LogThread logThread = new LogThread();
        logThread.pushDocument(esDocumentType, data, documentId, userId, organizationId);
    }
}
列表已启用;//根据构造函数中的“常量.弹性启用”常量填写此字段
公共文档(ESDocumentType,其他参数){
布尔继续=(Constants.ELASTIC_LOGGING_ENABLED&&ENABLED.contains(type))| | Constants.SQS_LOGGING_ENABLED;
如果(继续){
LogThread LogThread=新的LogThread();
logThread.pushDocument(esDocumentType、数据、documentId、userId、organizationId);
}
}

我不知道您的确切用例,您必须自己对其进行改进,但类似的方法可能会奏效

List<ESDocumentType> enabled; // fill this based on your "Constant.ELASTIC_<BLAH>_ENABLED" constants in the constructor

public void pushDocument(ESDocumentType type, other parameters) {
    boolean proceed = (Constants.ELASTIC_LOGGING_ENABLED && enabled.contains(type)) || Constants.SQS_LOGGING_ENABLED;
    
    if (proceed) {
        LogThread logThread = new LogThread();
        logThread.pushDocument(esDocumentType, data, documentId, userId, organizationId);
    }
}
列表已启用;//根据构造函数中的“常量.弹性启用”常量填写此字段
公共文档(ESDocumentType,其他参数){
布尔继续=(Constants.ELASTIC_LOGGING_ENABLED&&ENABLED.contains(type))| | Constants.SQS_LOGGING_ENABLED;
如果(继续){
LogThread LogThread=新的LogThread();
logThread.pushDocument(esDocumentType、数据、documentId、userId、organizationId);
}
}

这里有一种可能性。如果我这样做,我会设置一个映射来获取相应的布尔值

public void pushDocument(ESDocumentType esDocumentType,
        Object data, String documentId, long userId,
        long organizationId) {
    
    
    boolean proceed = esDocumentType.equals(ESDocumentType.XMLACTIVITY);
    proceed = proceed || esDocumentType.equals(ESDocumentType.XMLREQRES);
    proceed = proceed || esDocumentType.equals(ESDocumentType.ORDERHISTORY);
    proceed = proceed || esDocumentType.equals(ESDocumentType.SINGIN);
    proceed = proceed ||  esDocumentType.equals(ESDocumentType.GOOGLESEARCH);
    
    
    // if any logging is to be done, proceed and one of the others must be true.
    if (proceed && (Constants.SQS_LOGGING_ENABLED
            || Constants.ELASTIC_LOGGING_ENABLE)) {
            LogThread logThread = new LogThread();
            logThread.pushDocument(esDocumentType, data,
                    documentId, userId, organizationId);
    }
}

这是我提到的另一种选择。唯一的区别是如何确定
继续

Map<ESDocumentType, Boolean> docType = Map.of(
        ESDocumentType.EMLACTIVITY, Constants.ELASTIC_XMLACTIVITY_ENABLED,
        ESDocumentType.XMLREQRES, Constants.ELASTIC_XMLREQRES_ENABLED,
        ESDocumentType.ORDERHISTORY, Constants.ELASTIC_ORDERHISTORY_ENABLED,
        ESDocumentType.SINGIN, Constants.ELASTIC_SINGIN_ENABLED,
        ESDocumentType.GOOGLESEARCH, Constants.ELASTIC_GOOGLESEARCH_ENABLED);
    
public void pushDocument(ESDocumentType esDocumentType,
        Object data, String documentId, long userId,
        long organizationId) {
    
    
    boolean proceed = docType.getOrDefault(esDocumentType, false);
    
    // if any logging is to be done, proceed and one of the others must be true.
    if (proceed && (Constants.SQS_LOGGING_ENABLED
            || Constants.ELASTIC_LOGGING_ENABLE)) { 
                 LogThread logThread = new LogThread();
                 logThread.pushDocument(esDocumentType, data,
                      documentId, userId, organizationId);
    }
}
Map docType=Map.of(
ESDocumentType.EMLACTIVITY,常数.ELASTIC\u XMLACTIVITY\u已启用,
ESDocumentType.XMLREQRES,已启用常数.ELASTIC\u XMLREQRES\u,
ESDocumentType.ORDERHISTORY,已启用常数.ELASTIC\u ORDERHISTORY\u,
已启用ESDocumentType.SINGIN、Constants.ELASTIC_SINGIN_、,
ESDocumentType.GOOGLESEARCH,常量。弹性搜索(已启用GOOGLESEARCH);
公共文档(ESDocumentType ESDocumentType,
对象数据、字符串documentId、长用户ID、,
长组织ID){
布尔继续=docType.getOrDefault(esDocumentType,false);
//如果要进行任何日志记录,请继续,其他记录之一必须为true。
如果(继续)&(Constants.SQS_日志记录已启用)
||常量。弹性(日志记录(启用)){
LogThread LogThread=新的LogThread();
logThread.pushDocument(esDocumentType、数据、,
文档ID、用户ID、组织ID);
}
}

这里有一种可能性。如果我这样做,我会设置一个映射来获取相应的布尔值

public void pushDocument(ESDocumentType esDocumentType,
        Object data, String documentId, long userId,
        long organizationId) {
    
    
    boolean proceed = esDocumentType.equals(ESDocumentType.XMLACTIVITY);
    proceed = proceed || esDocumentType.equals(ESDocumentType.XMLREQRES);
    proceed = proceed || esDocumentType.equals(ESDocumentType.ORDERHISTORY);
    proceed = proceed || esDocumentType.equals(ESDocumentType.SINGIN);
    proceed = proceed ||  esDocumentType.equals(ESDocumentType.GOOGLESEARCH);
    
    
    // if any logging is to be done, proceed and one of the others must be true.
    if (proceed && (Constants.SQS_LOGGING_ENABLED
            || Constants.ELASTIC_LOGGING_ENABLE)) {
            LogThread logThread = new LogThread();
            logThread.pushDocument(esDocumentType, data,
                    documentId, userId, organizationId);
    }
}

这是我提到的另一种选择。唯一的区别是如何确定
继续

Map<ESDocumentType, Boolean> docType = Map.of(
        ESDocumentType.EMLACTIVITY, Constants.ELASTIC_XMLACTIVITY_ENABLED,
        ESDocumentType.XMLREQRES, Constants.ELASTIC_XMLREQRES_ENABLED,
        ESDocumentType.ORDERHISTORY, Constants.ELASTIC_ORDERHISTORY_ENABLED,
        ESDocumentType.SINGIN, Constants.ELASTIC_SINGIN_ENABLED,
        ESDocumentType.GOOGLESEARCH, Constants.ELASTIC_GOOGLESEARCH_ENABLED);
    
public void pushDocument(ESDocumentType esDocumentType,
        Object data, String documentId, long userId,
        long organizationId) {
    
    
    boolean proceed = docType.getOrDefault(esDocumentType, false);
    
    // if any logging is to be done, proceed and one of the others must be true.
    if (proceed && (Constants.SQS_LOGGING_ENABLED
            || Constants.ELASTIC_LOGGING_ENABLE)) { 
                 LogThread logThread = new LogThread();
                 logThread.pushDocument(esDocumentType, data,
                      documentId, userId, organizationId);
    }
}
Map docType=Map.of(
ESDocumentType.EMLACTIVITY,常数.ELASTIC\u XMLACTIVITY\u已启用,
ESDocumentType.XMLREQRES,已启用常数.ELASTIC\u XMLREQRES\u,
ESDocumentType.ORDERHISTORY,已启用常数.ELASTIC\u ORDERHISTORY\u,
已启用ESDocumentType.SINGIN、Constants.ELASTIC_SINGIN_、,
ESDocumentType.GOOGLESEARCH,常量。弹性搜索(已启用GOOGLESEARCH);
公共文档(ESDocumentType ESDocumentType,
对象数据、字符串documentId、长用户ID、,
长组织ID){
布尔继续=docType.getOrDefault(esDocumentType,false);
//如果要进行任何日志记录,请继续,其他记录之一必须为true。
如果(继续)&(Constants.SQS_日志记录已启用)
||常量。弹性(日志记录(启用)){
LogThread LogThread=新的LogThread();
logThread.pushDocument(esDocumentType、数据、,
文档ID、用户ID、组织ID);
}
}

对于Java8,使用下面的代码将有助于避免认知和圈复杂度

public void pushDocument(ESDocumentType esDocumentType, Object data, String documentId, long userId, long organizationId) {
    EnumMap<ESDocumentType, Boolean> docType = new EnumMap<>(ESDocumentType.class);
    docType.put(ESDocumentType.XMLACTIVITY, Constants.ELASTIC_XMLACTIVITY_ENABLED);
    docType.put(ESDocumentType.XMLREQRES, Constants.ELASTIC_XMLREQRES_ENABLED);
    docType.put(ESDocumentType.ORDERHISTORY, Constants.ELASTIC_ORDERHISTORY_ENABLED);
    docType.put(ESDocumentType.SINGIN, Constants.ELASTIC_SIGNIN_ENABLED);
    docType.put(ESDocumentType.GOOGLESEARCH, Constants.ELASTIC_GOOGLESEARCH_ENABLED);
    boolean proceed = docType.getOrDefault(esDocumentType, false);
    if (proceed && Constants.ELASTIC_LOGGING_ENABLED || Constants.SQS_LOGGING_ENABLED) {
        LogThread logThread = new LogThread();
        logThread.pushDocument(esDocumentType, data, documentId, userId, organizationId);
    }
}
public void pushDocument(ESDocumentType ESDocumentType,对象数据,字符串documentId,长用户ID,长组织ID){
EnumMap docType=新的EnumMap(ESDocumentType.class);
put(ESDocumentType.XMLACTIVITY,常数.ELASTIC\u XMLACTIVITY\u已启用);
put(ESDocumentType.XMLREQRES,常量.ELASTIC_XMLREQRES_已启用);
docType.put(ESDocumentType.ORDERHISTORY,常量.ELASTIC\u ORDERHISTORY\u已启用);
docType.put(已启用ESDocumentType.SINGIN、常量、弹性签名);
docType.put(ESDocumentType.GOOGLESEARCH,Constants.ELASTIC\u GOOGLESEARCH\u已启用);
布尔继续=docType.getOrDefault(esDocumentType,false);
如果(继续&&Constants.ELASTIC|u LOGGING|u ENABLED||Constants.SQS|u LOGGING_ENABLED){
LogThread LogThread=新的LogThread();
logThread.pushDocument(esDocumentType、数据、documentId、userId、organizationId);
}
}

对于Java8,使用下面的代码将有助于避免认知和圈复杂度

public void pushDocument(ESDocumentType esDocumentType, Object data, String documentId, long userId, long organizationId) {
    EnumMap<ESDocumentType, Boolean> docType = new EnumMap<>(ESDocumentType.class);
    docType.put(ESDocumentType.XMLACTIVITY, Constants.ELASTIC_XMLACTIVITY_ENABLED);
    docType.put(ESDocumentType.XMLREQRES, Constants.ELASTIC_XMLREQRES_ENABLED);
    docType.put(ESDocumentType.ORDERHISTORY, Constants.ELASTIC_ORDERHISTORY_ENABLED);
    docType.put(ESDocumentType.SINGIN, Constants.ELASTIC_SIGNIN_ENABLED);
    docType.put(ESDocumentType.GOOGLESEARCH, Constants.ELASTIC_GOOGLESEARCH_ENABLED);
    boolean proceed = docType.getOrDefault(esDocumentType, false);
    if (proceed && Constants.ELASTIC_LOGGING_ENABLED || Constants.SQS_LOGGING_ENABLED) {
        LogThread logThread = new LogThread();
        logThread.pushDocument(esDocumentType, data, documentId, userId, organizationId);
    }
}
public void pushDocument(ESDocumentType ESDocumentType,对象数据,字符串documentId,长用户ID,长组织ID){
EnumMap docType=新的EnumMap(ESDocumentType.class);
put(ESDocumentType.XMLACTIVITY,常数.ELASTIC\u XMLACTIVITY\u已启用);
put(ESDocumentType.XMLREQRES,常量.ELASTIC_XMLREQRES_已启用);
docType.put(ESDocumentType.ORDERHISTORY,常量.ELASTIC\u ORDERHISTORY\u已启用);
docType.put(已启用ESDocumentType.SINGIN、常量、弹性签名);
docType.put(ESDocumentType.GOOGLESEARCH,Constants.ELASTIC\u GOOGLESEARCH\u已启用);
布尔继续=docType.getOrDefault(esDocumentType,false);
如果(继续&&Constants.ELASTIC|u LOGGING|u ENABLED||Constants.SQS|u LOGGING_ENABLED){
LogThread LogThread=新的LogThread();
logThread.pushDocument(esDocumentType、数据、documentId、userId、organizationId);
}
}

所有表达式似乎都以
Constants.ELAS开头