[英]unify code when getting highest value and lowest value
我的 IDE 給了我重復代碼警告,現在我試圖找出有什么方法可以統一或找到這兩種方法的共同抽象,它們的結構非常相似,但是做相反的事情嗎? 我自己想不通。 謝謝
public double getLowestCurrencyRateInTheCurrentMonth(String baseCurrency, String targetCurrency) {
Map<String, Map<String, Double>> rates = getAllRatesInCurrentMonth(baseCurrency);
double lowestRate = Double.MAX_VALUE;
for(Map<String, Double> ratesByDate : rates.values()){
double baseCurrecyRateToTargetCurrency = ratesByDate.get(targetCurrency);
if(baseCurrecyRateToTargetCurrency < lowestRate){
lowestRate = baseCurrecyRateToTargetCurrency;
}
}
return lowestRate;
}
public double getHighestCurrencyRateInTheCurrentMonth(String baseCurrency, String targetCurrency) {
Map<String, Map<String, Double>> rates = getAllRatesInCurrentMonth(baseCurrency);
double highestRate = Double.MIN_VALUE;
for(Map<String, Double> ratesByDate : rates.values()){
double baseCurrecyRateToTargetCurrency = ratesByDate.get(targetCurrency);
if(baseCurrecyRateToTargetCurrency > highestRate){
highestRate = baseCurrecyRateToTargetCurrency;
}
}
return highestRate;
}
在我看來,那里有很多不必要的代碼。 您可以使用 Streams 以更好的方式實現相同的目標。 它看起來像這樣:
public double getHighestCurrencyRateInTheCurrentMonth(String baseCurrency, String targetCurrency, String variant) {
Map<String, Map<String, Double>> rates = getAllRatesInCurrentMonth(baseCurrency);
DoubleStream doubleStream = rates.values().stream()
.mapToDouble(entry -> entry.get(targetCurrency));
OptionalDouble result = variant.equals("max") ? doubleStream.max() : doubleStream.min();
return result.orElse(0);
}
我同意前面的幾個答案,並相信它們可以很好地使用。
就個人而言,我會創建一個統一的方法來處理公共邏輯流,然后有兩個描述性方法調用這個“公共邏輯方法”
因此,我不必將布爾標志作為參數處理(在這種情況下是雙標志),我有機會表達哪個名稱最適合該方法,最后我將所有邏輯流集中在一個方法中。
例子:
public double getLowestCurrencyRateInTheCurrentMonth(String baseCurrency, String targetCurrency) {
return commonLogic(targetCurrency, getAllRatesInCurrentMonth(baseCurrency), Double.MAX_VALUE);
}
public double getHighestCurrencyRateInTheCurrentMonth(String baseCurrency, String targetCurrency) {
return commonLogic(targetCurrency, getAllRatesInCurrentMonth(baseCurrency), Double.MIN_VALUE);
}
private double commonLogic(String targetCurrency, Map<String, Map<String, Double>> rates, double rate) {
for(Map<String, Double> ratesByDate : rates.values()){
double baseCurrecyRateToTargetCurrency = ratesByDate.get(targetCurrency);
if(rate == Double.MIN_VALUE ? baseCurrecyRateToTargetCurrency > rate : baseCurrecyRateToTargetCurrency < rate){
rate = baseCurrecyRateToTargetCurrency;
}
}
return rate;
}
一個簡單的解決方案如下:你給函數一個速率,它要么是double.MIN_VALUE
要么是double.MAX_VALUE
並稱之為rate
。 在函數中,您可以檢查 rate 是double.MIN_VALUE
還是double.MAX_VALUE
。 或者只是檢查rate
是更小還是更大baseCurrecyRateToTargetCurrency
。 我不太確定它是否是一個常數。 這將是一個例子:
public double getCurrencyRateExtremaInTheCurrentMonth(String baseCurrency, String targetCurrency, double rate) {
Map<String, Map<String, Double>> rates = getAllRatesInCurrentMonth(baseCurrency);
double resultRate = rate
for(String key : rates.keySet()){
Map<String, Double> ratesByDate = rates.get(key);
double baseCurrecyRateToTargetCurrency = ratesByDate.get(targetCurrency);
if((rate == double.MIN_VALUE && baseCurrecyRateToTargetCurrency > rate) || (rate == double.MAX_VALUE && baseCurrecyRateToTargetCurrency < rate)){
resultRate = baseCurrecyRateToTargetCurrency;
}
}
return resultRate;
}
我假設baseCurrecyRateToTargetCurrency
具有相同的值,因為它在函數中以相同的方式檢索。 那正確嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.