Thread overview
Re: [Tidbit] making your D code more modular & unittestable
Mar 08, 2017
H. S. Teoh
Mar 09, 2017
H. S. Teoh
March 08, 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.
March 08, 2017
On Thu, Mar 09, 2017 at 12:21:48AM +0000, Adam D. Ruppe via Digitalmars-d wrote:
> On Wednesday, 8 March 2017 at 21:34:19 UTC, H. S. Teoh wrote:
> > So I came up with an idea to abstract file contents as a random-access range of ubyte with lazy loading, so that I can rewrite file parsing code in the nice
> 
> Sounds like what the kernel does when you memory-map a file...

I know... I probably should've just used std.mmfile.

But it was fun to write my own RA range with slicing.  I did learn a thing or two about what std.range considers to satisfy hasSlicing... it wasn't quite as obvious as I had thought.


T

-- 
"I suspect the best way to deal with procrastination is to put off the procrastination itself until later. I've been meaning to try this, but haven't gotten around to it yet. " -- swr
March 09, 2017
On 03/08/2017 07:31 PM, H. S. Teoh via Digitalmars-d wrote:
>
> I did learn a
> thing or two about what std.range considers to satisfy hasSlicing... it
> wasn't quite as obvious as I had thought.

Do tell!