我有笨拙的方法,包含多个丑陋的条件语句。例如:
public String getKafkaTopicName(EvntMsg evntMsg) {
Table table = fillerTableSection(evntMsg);
String[] msgDetails = table.getMessageString().split("\\^");
String topicName;
if (("QUEUE_R".equals(table.getDeliveryChannel()) && "A".equals(msgDetails[0])) ||
("M".equals(table.getDeliveryChannel()) && "F".equals(msgDetails[0])) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "S".equals(msgDetails[0])) ||
("QUEUE_W".equals(table.getDeliveryChannel()) && "S".equals(msgDetails[0])) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "C".equals(msgDetails[0]) && "DD_SRC_MESS".equals(table.getCode())) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "C".equals(msgDetails[0]) && "DD_TGT_MESS".equals(table.getCode()))) {
topicName = paramsHelper.getKafkaTopicNameBalance();
} else if (("SMS3DS_CHECK".equals(table.getDeliveryChannel())) ||
("SMS3DS".equals(table.getDeliveryChannel()))) {
topicName = paramsHelper.getKafkaTopicNameOtp();
} else if (("SBRF_I".equals(table.getDeliveryChannel()) && "I".equals(msgDetails[0])) ||
("SBRF_B".equals(table.getDeliveryChannel()) && "B".equals(msgDetails[0])) ) {
topicName = paramsHelper.getKafkaTopicNameIssuing();
} else if (("QUEUE_Q".equals(table.getDeliveryChannel()) && "C".equals(msgDetails[0]) && "C0".equals(table.getCode().substring(0, 2))) ||
("QUEUE_R".equals(table.getDeliveryChannel()) && "AP".equals(msgDetails[0])) ||
("QUEUE_D".equals(table.getDeliveryChannel()) && "AP".equals(msgDetails[0]))) {
topicName = paramsHelper.getKafkaTopicNameNotify();
} else {
topicName = paramsHelper.getKafkaTopicNameUndefined();
}
return topicName;
}
如何对其进行修改以提高可读性?
答案 0 :(得分:1)
您可以做的是隔离返回布尔值的方法
boolean test0(Table table, String[] msgDetails){
return "QUEUE_R".equals(table.getDeliveryChannel()) && "A".equals(msgDetails[0])) ||
("M".equals(table.getDeliveryChannel()) && "F".equals(msgDetails[0])) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "S".equals(msgDetails[0])) ||
("QUEUE_W".equals(table.getDeliveryChannel()) && "S".equals(msgDetails[0])) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "C".equals(msgDetails[0]) && "DD_SRC_MESS".equals(table.getCode())) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "C".equals(msgDetails[0]) && "DD_TGT_MESS".equals(table.getCode()))
}