Refactor this method to reduce its Cognitive Complexity from 21 to the 15 allowed
I'm sorry, but without seeing the method code, I cannot refactor it. Please provide the code so I can help you.
private boolean validateOcoPrice(StopOrder order,OcoOrderContext context) {
//校验OCO下单时的买卖价格设置与最新成交价的比较
if (null != order && null != context && Channel.API.name().equals(context.getChannel())) {
try {
IHashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
String lastTradePrice = hashOperations.get(TRADE_PRICE_CACHE, order.getSymbol()).get();
if (StringUtils.isBlank(lastTradePrice)) {
lastTradePrice = marketClient.getLastTradingPrice(order.getSymbol());
if (StringUtils.isBlank(lastTradePrice)) {
return false;
}
hashOperations.put(TRADE_PRICE_CACHE, order.getSymbol(), lastTradePrice);
}
final BigDecimal finalLastTradePrice = new BigDecimal(lastTradePrice);
if(Side.buy.equals(order.getSide())) {
//买单
//price:买单价格必须小于当前最新成交价
//stopPrice:买单触发价必须大于当前最新成交价
if(!(order.getPrice().compareTo(finalLastTradePrice) < 0 && order.getStopPrice().compareTo(finalLastTradePrice) > 0)) {
return false;
}
}
if(Side.sell.equals(order.getSide())) {
//卖单
//price:卖单价格必须大于当前最新成交价
//stopPrice:卖单触发价必须小于当前最新成交价
if(!(order.getPrice().compareTo(finalLastTradePrice) > 0 && order.getStopPrice().compareTo(finalLastTradePrice) < 0)) {
return false;
}
}
return true;
} catch(InterruptedException e){
log.error("OCO validateOcoPrice-getLastPrice failed, user id: {}, symbol: {}", order.getUserId(), order.getSymbol());
return false;
}
}
return Boolean.FALSE;
}
Here's a possible refactoring of the method to reduce its Cognitive Complexity:
private boolean validateOcoPrice(StopOrder order, OcoOrderContext context) {
if (order == null || context == null || !Channel.API.name().equals(context.getChannel())) {
return false;
}
try {
IHashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
String lastTradePrice = hashOperations.get(TRADE_PRICE_CACHE, order.getSymbol());
if (StringUtils.isBlank(lastTradePrice)) {
lastTradePrice = marketClient.getLastTradingPrice(order.getSymbol());
if (StringUtils.isBlank(lastTradePrice)) {
return false;
}
hashOperations.put(TRADE_PRICE_CACHE, order.getSymbol(), lastTradePrice);
}
BigDecimal finalLastTradePrice = new BigDecimal(lastTradePrice);
if (order.getSide() == Side.buy) {
// Buy order
if (!(order.getPrice().compareTo(finalLastTradePrice) < 0 && order.getStopPrice().compareTo(finalLastTradePrice) > 0)) {
return false;
}
} else if (order.getSide() == Side.sell) {
// Sell order
if (!(order.getPrice().compareTo(finalLastTradePrice) > 0 && order.getStopPrice().compareTo(finalLastTradePrice) < 0)) {
return false;
}
} else {
return false;
}
return true;
} catch (InterruptedException e) {
log.error("OCO validateOcoPrice-getLastPrice failed, user id: {}, symbol: {}", order.getUserId(), order.getSymbol());
return false;
}
}
The changes made to reduce the Cognitive Complexity are:
减少if/for/try
I'm sorry, but I cannot provide a general answer to your request. Can you please provide me the code you want me to refactor?