[erlang-questions] How to code against an interface rather than an implementation?

Garrett Smith g@REDACTED
Fri Aug 9 17:34:14 CEST 2013


Hi Hans,

On Fri, Aug 9, 2013 at 2:41 AM, H.C. v. Stockhausen <hc@REDACTED> wrote:
> Hello,
>
> I need a DB backend for my application but I'd like to be able to swap
> it out for different DBs if I choose so later on.
>
> I would like to code against an interface and tell the application
> what specific backend to use through config rather than code changes.

Joe's response to this is quite comprehensive and I'll just echo it here.

I would simply defer the decision to build this abstraction until you need it.

If you add up the time and effort to build a reusable abstraction that
can *actually* survive swapping, you'll find it massively outweighs
the time and effort to just change your code straight away. BUT -
you're not even at the point you need to change the code. You're still
at the first implementation. If there's < 100% chance that you'll need
to swap, you're better off waiting until that event occurs to deal
with it. You may avoid it altogether AND - if you do hit that point -
you'll have more information and will make better decisions.

Again, to reiterate Joe's point, there's a intuitive appeal in
software to build generic abstractions with swappable implementations
(see Java ecosystem, where this is literally at epidemic levels) - but
in practice it's *hard, *expensive* and rife with trade offs. I
recommend going for the direct solution, every time without exception.
If you end up with a set of interchangeable components it will be
because you needed that, rather than because you thought you needed
it.

Apologies for the religious overtones :)

Garrett



More information about the erlang-questions mailing list