UP | HOME

Interfaces

While abstraction (meaning abstracting out irrelevant details, not abstract philosophical or artistic bullshit) is the main principle of all programming (and math), what we call Interfaces is the most important concept in programming, period.

The Modularity principle, just naturally follows from what interfaces are and how they being used.

Domain Specific Languages or DSLs, which is, perhaps, the second most important concept in all programming, are just specially designed sets of functions (or, more precisely - function type-signatures). But a set function type-signatures is what an interface is.

Just remember, that a function is the smallest building block of abstraction, and corresponds to a single named expression, parameterized (tables) or not (constants). In the old times they used to call it a procedure abstraction.

Perhaps the best current metaphor for interfaces is from the golden age of Personal Computers, when on could buy, lets say a video card, and just plug it into some PCI (nowadays - PCI-Express) slot and it will just work. Then one might change not just a vendor, but a whole architercure of a video card (let’s say from Nvidia to AMD), plug it into the motherboard, and everything will magically work.

This is exactly what we want - the ability to swap not just implementation, without breaking anything, but to switch between “storages” or “network protocols”, having them as plug-ins (which is just a module that implements some standard interface).

Duck-typing

To be an X is to exhibit X’s /behavior or just act exactly as X does. What walks like a duck and quacks like duck, they say. And, indeed, what can be a junior Javascript coder? What writes code like a javascript coder and quacks like a javascript coder. Anyway.

Biology

This is actually a universal principle. You can “plug” or “use” any molecule, lets say a glucose, which has a particular shape (and resulting electro-chemical characteristics).

A shape serves as its own type and all the individual molecules of the same shape are indistinguishable and interexchageable. This, by the way, is how any molecular biology or life itself is possible. Mother nature does not have the concept of “identity”.

There could be molecules which has exactly the same “parts” or “sides”, and these parts can be used as if the molecules are exactly the same. Biology uses this trick all the time.

That particular “shape” or arrangement which triggers some enzymes or binds to some receptors is, operationally, an interface. It is that fundamental.

The essence

The essence of programming is to reduce everything to the few universal principles and universal building blocks which found in biology (after enumerable trials-and-errors). Smart people call them “stable intermediate building blocks”.

It turns out that hierarchical (or layered) DSLs is a universal architecture. Just like the language of the Set Theory has been embedded into mathematics, an Abstract Data Type that implements the Set abstraction - the module - extends a pure functional language like Haskell (or the pure subset of Ocaml).

Mathematicians never consider representation or even existence (leave alone implementation) of their “objects”, and the actual technique (and a whole methodology) of using Abstract Data Types, which create an abstraction barrier and hide implementation details behind (which could even be swapped - one module for another) is what Barbara Liskov got her Turing Award for.

DSLs are just specialized abstract interfaces and a corresponding module which abstracts out the actual implementation behind its abstract public interface. Exactly like a module which implements Sets.

Just make them pure-functional, like the imaginary core of molecular biology (enzymes are pure functions, and molecular structures are, at least operationally, immutable and persistent, if you don’t know. Yes, DNA and RNA occasionally breaks, and DNA some times can be repaired, but they are not “supposed to” ).

Even better - is to use the mind of an external observer to generalize and derive useful abstractions (like a Number or a linear sequence, a tree or a lookup table) and use the minimal possible set of these abstractions to represent and define your problem domain. This is what the Set Theory and the First Order Logic do in math (which happen to be DSLs embedded in it).

And the essence of the essence is to use a minimal arrangement that is stable, just does one thing and does it well. And build whole hierarchies out of these arrangements. Yes, you have probably heard this somewhere before.

Author: Ln Gnmn <lngnmn1@gmail.com>

Email: lngnmn2@yahoo.com

Created: 2023-08-08 Tue 18:40

Emacs 29.1.50 (Org mode 9.7-pre)