June 2018
Intermediate to advanced
280 pages
7h 46m
English
The code mentioned in the previous example contains duplicated code (code smell). We'll apply the execute around pattern to simplify the code and make it easier to read. A possible refactoring can make use of lambda, as we can see:
publicstaticvoid measurePerformance(Runnable runnable) { long start = System.currentTimeMillis(); runnable.run(); System.out.println("It took " + (System.currentTimeMillis() - start) + " ms");}publicstaticvoid main(String[] args) { Main main = new Main(); List<Integer> array = Arrays.asList(500_000, 499_999); measurePerformance(() -> array.stream().map(BigInteger::valueOf) .forEach(x -> main.fib(x))); measurePerformance(() -> main.memoization(array));}
Read now
Unlock full access