提问者:小点点

简化if else条件以降低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);
    }
}

共3个答案

匿名用户

我不知道您确切的用例,您必须自己改进一下,但是像这样的东西可能会起作用。

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

匿名用户

使用一个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;

}

匿名用户

这里有一种可能性。 如果我要这样做,我将建立一个映射来获取适当的枚举或常量。

public void pushDocument(ESDocumentType esDocumentType,
        Object data, String documentId, long userId,
        long organizationId) {
    boolean proceed = false;
    
    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 is true then some logging must be done.
    if (proceed) {
        // so if either of the logging is enabled, log it.
        if (Constants.SQS_LOGGING_ENABLED
                || Constants.ELASTIC_LOGGING_ENABLE) {
            
            LogThread logThread = new LogThread();
            logThread.pushDocument(esDocumentType, data,
                    documentId, userId, organizationId);
        }
    }
}