简化其他条件以降低Java的认知复杂度

时间:2020-06-23 13:50:09

标签: java sonarqube

有没有一种方法可以简化此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);
    }
}

5 个答案:

答案 0 :(得分:4)

我不知道您的确切用例,您需要自己进行一些改进,但类似的方法可能会起作用。

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);
    }
}

答案 1 :(得分:3)

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

switch(ESDocumentType)
{
   case ESDocumentType.XMLACTIVITY:
        proceed = Constants.ELASTIC_LOGGING_ENABLED && 
        Constants.ELASTIC_XMLACTIVITY_ENABLED || Constants.SQS_LOGGING_ENABLED;
   break;
   
   [ .... add the other cases here]


   default:
     //we do not need to set proceed to false manually, but here would be the case for that
   break;

}

答案 2 :(得分:1)

这是一种可能性。如果这样做,我将设置一个地图以获取适当的布尔值。

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);
    }
}

这是我提到的替代方法。唯一的区别是proceed的确定方式。

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);
    }
}

答案 3 :(得分:0)

public void pushDocument(ESDocumentType esDocumentType, Object data, String documentId, long userId, long organizationId) {

    boolean proceed = Constants.ELASTIC_LOGGING_ENABLED;

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

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

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

    else if (esDocumentType.equals(ESDocumentType.SINGIN)) {
        proceed &&= Constants.ELASTIC_SIGNIN_ENABLED;
    }

    else if (esDocumentType.equals(ESDocumentType.GOOGLESEARCH)) {
        proceed &&= Constants.ELASTIC_GOOGLESEARCH_ENABLED;
    }

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

答案 4 :(得分:0)

对于Java8,请使用以下代码,这将有助于避免认知和循环复杂性

<ul>
   <li v-for="(conversation,index) in conversation" :key="conversation.id">
      <div>
          <p>Message: {{conversation.text}}</p>
          <p>User: {{conversation.user.name}}</p>
          <p>Created at: {{conversation.createdAt}}</p>
      </div>
   </li>
</ul>