November 26, 20268 minutes
LocalDate has no next business day, so five services copy the same loop and one forgets Sunday. Learn to centralise missing library behaviour with a Foreign Method or a Local Extension, and when to leave it.
A delivery app needs one small thing from its date class: the next business day. Skip Saturday, skip
Sunday, and give me Monday. Java’s LocalDate has plusDays and getDayOfWeek, but no next
business day. So one developer writes the loop. Then another developer copies it. Soon the loop lives
in five services, and one copy forgets Sunday, so an order on Friday is promised for Sunday. That’s a
smell called Incomplete Library Class: the library lacks something, and the missing piece gets
copied everywhere.
Prefer to watch? The narrated video covers the same lesson (about 8 minutes). The article follows it section by section, with diagrams and a little extra depth.
Here is the app as a picture. The library has no nextBusinessDay, so every service carries its own
copy of the loop.
Picture a rented flat. You’d love a shelf in the hallway, but the lease says no drilling into the walls. You could try to change the wall, but it isn’t yours. Or you could hang a hook wherever you need one, a different hook each time.
The smarter move is a free-standing shelf. It stands next to the wall, holds everything, and leaves the wall alone. The library class is the wall. It belongs to someone else, and it stays as it is. The shelf is a class that you own. And when the landlord repaints the wall, which is a library upgrade, your shelf still stands.
Here are two of the five services. The quote service takes the order date, adds a day, and keeps adding days while it lands on a weekend. The invoice service needs the same rule, so someone copies the loop. But this copy checks only for Saturday.
class QuoteService {
LocalDate deliveryDate(LocalDate ordered) {
LocalDate d = ordered.plusDays(1);
while (d.getDayOfWeek() == DayOfWeek.SATURDAY
|| d.getDayOfWeek() == DayOfWeek.SUNDAY) {
d = d.plusDays(1);
}
return d;
}
}
class InvoiceService {
LocalDate dueDate(LocalDate issued) {
LocalDate d = issued.plusDays(1);
while (d.getDayOfWeek() == DayOfWeek.SATURDAY) {
d = d.plusDays(1);
}
return d;
}
}Honestly, the first developer did the sensible thing. The library had no such method, and there was
a deadline. Each copy is short, which is why nobody stops to ask where the rule should live.
Meanwhile, the rest of the app uses LocalDate happily. Only this one rule is missing.
Now it’s Friday. A customer places an order. The quote service skips the weekend and says Monday. The invoice service skips only Saturday, and says Sunday. Same question, two answers, and nothing in the code explains why.
Then the business adds public holidays. Now you must find every copy, and edit each one the same way. Copies drift apart, and a search for one won’t find the others, because each was tweaked a little. And every copy needs its own tests. The loop isn’t the real problem. The real problem is that the rule has no home.
One of these may be a coincidence. Several together mean a missing piece.
Before you write anything, check whether the library already has it. Java’s time package has
temporal adjusters, and one of them is called next.
LocalDate monday = date.with(
TemporalAdjusters.next(DayOfWeek.MONDAY));next takes a day of the week and finds the first such day after your date. It’s handy for the next
Monday, but none of the built-in adjusters skip weekends or know about business days. So the library
can’t help here. It does let you plug in your own adjuster (TemporalAdjusters.ofDateAdjuster, used
with with), but the rule still has to live somewhere you own. Check the documentation, and newer
versions too. Someone may have added it already.
If a library class lacks something and you can’t change it, put the missing behavior in one place that you own. Like the shelf, it stands next to the wall. Callers use the shelf, and the wall stays as it is. That gives the rule a name, a home, and one set of tests.
Can’t change the library? Wrap it once, in one place.
The first tool is a Foreign Method: one method that calls the library, adds the missing piece, and
returns the result. The course page uses that name. The refactoring.guru site calls it Introduce
Foreign Method. Here it’s a static method in a small class called BusinessDays. It takes a
LocalDate and returns the next business day.
final class BusinessDays {
private BusinessDays() {}
static LocalDate next(LocalDate date) {
LocalDate d = date.plusDays(1);
while (d.getDayOfWeek() == DayOfWeek.SATURDAY
|| d.getDayOfWeek() == DayOfWeek.SUNDAY) {
d = d.plusDays(1);
}
return d;
}
}Every service now calls that one method. The copies are gone, and the Sunday bug disappears with them.
class QuoteService {
LocalDate deliveryDate(LocalDate ordered) {
return BusinessDays.next(ordered);
}
}
class InvoiceService {
LocalDate dueDate(LocalDate issued) {
return BusinessDays.next(issued);
}
}Fowler’s original recipe puts a foreign method in the client class, with a comment that it belongs in the library class. With five clients, one shared helper is the natural home (refactoring.guru makes the same point: when several classes could use the method, a wrapper or shared home is better). This simple rule ignores public holidays. If the rule grows, it grows in one place.
Fowler’s second edition dropped both names from its online catalogue, so you’ll mostly meet them on the course page and in older books.
When you need several methods, not one, use Introduce Local Extension. Make a class of your own
that carries the new methods. The obvious way is a subclass. But LocalDate is final, so the
compiler refuses. (The Java documentation declares public final class LocalDate.)
// error: cannot inherit from final LocalDate
class BusinessDate extends LocalDate {
}So we wrap instead. A BusinessDate holds a LocalDate and adds the methods we want: next, and
plusBusinessDays.
record BusinessDate(LocalDate date) {
BusinessDate next() {
return new BusinessDate(BusinessDays.next(date));
}
BusinessDate plusBusinessDays(int n) {
BusinessDate result = this;
for (int i = 0; i < n; i++) {
result = result.next();
}
return result;
}
}Callers write: take this date, add three business days. The loop stays in one place, and the wrapper delegates to the helper.
LocalDate promised = new BusinessDate(ordered)
.plusBusinessDays(3)
.date();The price of a wrapper is forwarding. Any LocalDate method you want on it, you must pass through
yourself. So wrap only when you need the extra methods. Name the wrapper after the idea in your
domain, like business date, not after the library.
A wrapper looks like two patterns you may know. An
Adapter wraps a
class to change its shape. Here, we add behavior instead. A
Decorator wraps a class to add behavior, but keeps the same
interface, so callers can’t tell the difference. A BusinessDate is a new type, so it isn’t one.
And the smell next door is Duplicate Code. The five copies were duplicate code, and the missing method is why they appeared.
“If the library lacks a method, fork it, or patch it.” Patching third-party code works until the next upgrade. Then you must re-apply your patch, and carry it forever. Wrapping or extending in your own code leaves the library alone, so upgrades stay boring.
So when should you leave it?
Incomplete Library Class means the library lacks something, and the missing piece gets copied
everywhere. Look for an existing method first. Then add the behavior in one place: a Foreign Method
for one helper, a Local Extension for several. Use a wrapper, because LocalDate is final. And skip
all this for a one-off, or send the change upstream.
Checklist:
Can’t change the library? Wrap it once, in one place.