Video in progress

When is removing an abstraction layer the right decision? How do you spot needless indirection and a "leaky abstraction"?

If an abstraction wraps only one implementation, if every caller still has to know the technology details inside, or if it makes errors harder to understand, it doesn't earn its cost. I inline such a layer and simplify the code.

📖 Answer

Every abstraction layer has a cost: a new file, a new name, an extra step in the debugger, something new people must learn. In return it has to give something: the ability to swap, hiding complexity, or a clear contract.

Signs that an abstraction isn't paying off:

  • Pass-through. OrderFacade -> OrderManager -> OrderService -> OrderRepository, each a one-line delegation.
  • Leaking. StorageService is written "for any storage", but its methods take bucketName, presignedUrlTtl and s3Region. The abstraction doesn't hide S3, it only renames it.
  • Lowest common denominator. A CacheClient interface hides Redis's pipeline and Lua features, so the team works around it and uses the real client directly.
  • Errors disappear. The wrapping layer replaces the original exception with StorageException("xato"), without the cause.
// a leaky abstraction
interface FileStore {
    String put(String bucket, String key, byte[] data, Duration urlTtl, String region);
}

When I remove it: there's no real plan to swap, and the layer saves callers nothing. Removing the layer with "Inline" in the IDE is usually safe and quick.

When I keep it or fix it:

  • at the domain boundary (DIP): the domain should speak its own language, even with one implementation;
  • for external systems that are hard to test;
  • in that case I redesign the interface from the caller's needs: FileStore.save(Document), with the bucket and region in the adapter configuration.

Joel Spolsky's law: every non-trivial abstraction leaks to some degree. The goal isn't to eliminate the leak but to make it explicit and rare (for example, a deliberate way into the real client through unwrap()).