【问题标题】:Would like some suggestions to clean up a series of if statements想要一些建议来清理一系列 if 语句
【发布时间】:2021-04-19 14:45:29
【问题描述】:

我有一种方法可以进行大量验证,并且正在失控。我将不胜感激有关如何最好地清理此方法的任何建议。我正在使用 Java 11,此方法是 Spring Boot 微服务的一部分。

public void validateRequest(DepositRequest depositRequest, String transferId, String userId) {
    if (!Arrays.asList("REALTIME_PAYMENT", "ACCOUNT_PAYMENT").contains(depositRequest.creditTransfer()
            .getTransferInformation().getValue())) {
        logError(depositRequest, participantUserId, etransferId, INVALID_ACCOUNT_NUMBER);
        throw new ServerValidationException(INVALID_ACCOUNT_NUMBER, PAYMENT);
    }

    if (depositRequest.creditTransfer().getGroupHeader().getSettlementInformation().getClearingSystem() == null) {
        logSchemaValidationError(depositRequest, etransferId, participantUserId, "proprietary");
        throw new ServerValidationException(SCHEMA_VALIDATION_ERROR, PAYMENT);
    }

    if (depositRequest.creditTransfer().getGroupHeader().getInstructing()
            .getInstitutionIdentification().getMemberIdentification() == null) {
        logSchemaValidationError(depositRequest, etransferId, participantUserId, "member_identification");
        throw new ServerValidationException(SCHEMA_VALIDATION_ERROR, PAYMENT);
    }

    if (depositRequest.creditTransfer().getGroupHeader().getInstructed()
            cialInstitutionIdentification().getMemberIdentification() == null) {
        logSchemaValidationError(depositRequest, etransferId, participantUserId, "member_identification");
        throw new ServerValidationException(SCHEMA_VALIDATION_ERROR, PAYMENT);
    }

    if (depositRequest.creditTransfer().getTransferInformation().getCreditor().getName() == null) {
        logSchemaValidationError(depositRequest, etransferId, participantUserId, "creditor.name");
        throw new ServerValidationException(SCHEMA_VALIDATION_ERROR, PAYMENT);
    }

    if (depositRequest.creditTransfer().getTransferInformation().getDebtor().getName() == null) {
        logSchemaValidationError(depositRequest, etransferId, participantUserId, "debtor.name");
        throw new ServerValidationException(SCHEMA_VALIDATION_ERROR, PAYMENT);
    }

    if (depositRequest.authorization() != null) {
        if (depositRequest.authorization().getToken() == null ||
                authorization().getToken().length() < 1 ||
                authorization().getToken().length() > 35) {
        logSchemaValidationError(depositRequest, etransferId, participantUserId, "participant_authorization_token");
        throw new ServerValidationException(SCHEMA_VALIDATION_ERROR, PAYMENT);
        }
    }

    if (!isCreditorAccountIdentificationValid(depositRequest.creditTransfer().getTransferInformation().getIdentification())) {
        logSchemaValidationError(depositRequest, etransferId, participantUserId, "identification");
        throw new ServerValidationException(INVALID_ACCOUNT_INFO, PAYMENT);
    }

    if (!depositRequest.creditTransfer().getTransferInformation().getSettlementDate().equals(LocalDate.now())) {
        logSchemaValidationError(depositRequest, etransferId, participantUserId, "settlement_date");
        throw new ServerValidationException(SCHEMA_VALIDATION_ERROR, PAYMENT);
    }
}

【问题讨论】:

    标签: java refactoring


    【解决方案1】:

    简单的代码就是好的代码。您当前的代码易于理解和维护。所以我不一定会急于改变它。但如果你愿意,你可以:

    1. 在单独的私有方法中进行每个验证,并在主验证方法中调用每个方法

      public void validateRequest(DepositRequest depositRequest, String transferId, String userId) {
       validateAccountNumber(depositRequest,transferId, userId);
          ...
      }
      
      private void validateAccountNumber(DepositRequest depositRequest, String transferId, String userId) {
          ...
      }
      
    1. 你可以看中并创建一个验证器接口

      public interface Validator {
           void validate(DepositRequest depositRequest, String transferId, String userId);
      }
      
      public class AccountIDValidator implements Validator {
           public void validate(DepositRequest depositRequest, String transferId, String userId){
                ...
            }
       }
      

    然后你为每个验证创建这个接口的实例,然后你把它们放在一个列表中,这样你的验证就变成了

       List<Validator> validators=Arrays.asList(new AccountIDValidator(),...);
    
       public void validateRequest(DepositRequest depositRequest, String transferId, String userId) {
           for (Validator v:validators){
              v.validate();
           }
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 2016-11-20
      相关资源
      最近更新 更多