We create APIs all the time - and I don’t have only libraries and frameworks in mind. Every piece of code that’s intended to be called by another piece of code is an API, in some sense. It’s our job to define an interface, which will be used to achieve whatever is expected.
While discussing various API designs, we often focus on “how it’s gonna look” first. Does it allow fluent calls?
While browsing Twitter a few days ago, I’ve been reminded about a quite controversial topic in the Java community, which (apparently) still is using interface default methods. To be honest, I’ve been a bit skeptical as well when I’ve first seen them coming as a part of Java 8. However, today I’ll write a few words in defense of default methods, as when used carefully, they can turn out to be extremely handy.
Luke, I am Your Father! In his first day at the new job, Bob created the following class:
import java.util.Arrays; import java.util.List; /** * Returns a sum of given numbers. * @author Bob */ public class NumberSumCalculator { public int sum(List<Integer> integers) { int sum = 0; if (integers != null) { for (Integer integer : integers) { sum += integer; } } return sum; } } He was so proud of it, so he put the @author Javadoc tag with his name at the top.