Video in progress

The team proposes adding a functional library like Vavr (Either, Try, immutable collections) to the project. How do you evaluate this decision?

The upside: errors and optionality are expressed in types, and composition is convenient. The cost: learning time for new developers, integration with Spring/JPA/Jackson, two kinds of collection worlds, and the library's long-term support. I usually propose starting with a small, bounded area and checking whether standard Java's sealed/record features are enough.

📖 Answer

I evaluate a decision like this not by "pretty code" but by the whole system and the team.

The benefit side:

  • Try, Either, Validation: errors as values, and collecting them without stopping at the first one is easy;
  • persistent collections: modification returns a new version, no defensive copy needed;
  • a richer API than Stream (zip, sliding, groupBy), sequences you can traverse many times.

The cost side:

  • Two worlds: io.vavr.collection.List and java.util.List, with conversion at the boundaries all the time. JPA, Jackson, Spring Data and MapStruct expect standard collections. Jackson needs a separate module.
  • Learning: a new developer struggles reading a service full of flatMap, fold, Either.right. Arguments in reviews multiply.
  • The library's fate: projects maintained by a small team can slow down or stop. Tying the core domain code to it is a long-term risk. Before deciding, look at the latest releases and the maintainers' activity.
  • Java itself got closer: in Java 17–21, with sealed interfaces, records and pattern matching switch, you write your own Result in 20 lines and everyone understands it.
sealed interface Outcome<T> permits Outcome.Ok, Outcome.Fail {
    record Ok<T>(T value) implements Outcome<T> {}
    record Fail<T>(String code, String message) implements Outcome<T> {}
}

String text = switch (outcome) {
    case Outcome.Ok<Payment>(var p) -> "to'landi: " + p.id();
    case Outcome.Fail<Payment>(var code, var msg) -> code + ": " + msg;
};

How I decide:

  1. We write down the concrete problem: "validation errors are getting lost", "control flow through exceptions is unreadable".
  2. We show a solution with standard Java in a sample.
  3. If Vavr is clearly better anyway: in one module, with clear boundaries (Vavr types don't leak into the public API), and a 2–3 month trial and a retrospective.
  4. An exit plan in the ADR: how much code would have to be migrated if the library stops.

In practice many teams end up with standard Java plus a few small types of their own, and that's not a bad outcome.