[Tidbit] making your D code more modular & unittestable

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 8 15:33:21 PST 2017


On Wed, Mar 08, 2017 at 10:03:28PM +0000, XavierAP via Digitalmars-d wrote:
> On Wednesday, 8 March 2017 at 21:34:19 UTC, H. S. Teoh wrote:
> > While writing a tool for dissecting various file formats, I found a
> > useful coding pattern that helps your D code be cleaner, more
> > modular, and more easily unittestable.
> 
> https://en.wikipedia.org/wiki/Dependency_inversion_principle
> 
> "Abstractions should not depend on details. Details should depend on
> abstractions."

Yes.

There's also the pattern of unification + generalization.  I.e., I
started with having code that depends on the API details of
std.stdio.File, then moved to a RA range API (that is, unification with
array slicing syntax), which allowed the generalization of the code to
apply to things that don't have a File interface.

Seen another way, initially multiple modules depended on the details of
the File API, but after unification with the RA range API and
refactoring, only 1 module needs to deal with the specifics of the File
API; everyone else uses general array slicing syntax which has a larger
scope of applicability.


T

-- 
Today's society is one of specialization: as you grow, you learn more and more about less and less. Eventually, you know everything about nothing.


More information about the Digitalmars-d mailing list