首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简化其他条件以降低Java中的认知复杂性

简化其他条件以降低Java中的认知复杂性
EN

Stack Overflow用户
提问于 2020-06-23 13:50:09
回答 5查看 4K关注 0票数 1

有办法简化这个java函数吗?

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

代码语言:javascript
复制
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);
    }
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2020-06-23 15:02:39

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-06-23 14:15:48

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

代码语言:javascript
复制
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);
    }
}
票数 5
EN

Stack Overflow用户

发布于 2020-06-23 14:01:34

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

代码语言:javascript
复制
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;

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62536317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档