Use reduce instead of manual accumulation
Clean code
What is it?
This practice is triggered when code manually accumulates results in loops or recursion, instead of using the reduce
function provided by languages like JavaScript, Python, or Ruby.
Why apply it?
Using reduce
promotes code clarity and conciseness by expressing the accumulation logic in a declarative manner, thereby reducing potential errors and enhancing maintainability.
How to fix it?
Refactor the code to use a reduce
function, passing a reducing callback and an initial accumulator value, to replace manual accumulation logic.
Read more:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
Examples
Example 1:
Positive
Correct implementation following the practice.
package org.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public final class Util {
public static Map<String, Integer> nameCount(List<String> names) {
return names.stream()
.collect(Collectors.toMap(name -> name, name -> 1, Integer::sum));
}
public static void printNames(List<String> names) {
names.forEach(System.out::println);
}
public static Integer sum(List<Integer> numbers) {
return numbers.stream()
.reduce(0, Integer::sum);
}
}
Negative
Incorrect implementation that violates the practice.
package org.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public final class Util {
public static Map<String, Integer> nameCount(List<String> names) {
return names.stream()
.collect(Collectors.toMap(name -> name, name -> 1, Integer::sum));
}
public static void printNames(List<String> names) {
names.forEach(System.out::println);
}
public static Integer sum(List<Integer> numbers) {
int sum = 0;
for (int number : numbers) {
sum += number;
}
return sum;
}
}