Simulating I/O errors [was: assume, assert, enforce, @safe]

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 1 13:02:30 PDT 2014


On Fri, Aug 01, 2014 at 12:05:00PM -0700, Walter Bright via Digitalmars-d wrote:
> On 8/1/2014 8:18 AM, Assaf Gordon via Digitalmars-d wrote:
> >As Walter said, it's alarming how many programs fail to handle such
> >cases (though D is pretty solid in that regard).
> 
> One of the very cool things about the ranges+algorithms style of
> programming is things like I/O don't get mixed up in code that
> operates on data.
[...]

Not to mention, it lets you *really* unittest your code thoroughly,
because it isolates each separate stage of the processing into its own
self-contained unit, with clearly-defined standard interconnects. Your
unittest can therefore easily hook up with the unit's interfaces, and
inject arbitrary inputs to the unit and run arbitrary tests on its
output -- without needing such hackery as redirecting stdout just so you
can confirm correct computation of a numerical result, for example.

In the traditional imperative programming style, you often have code
that has loops within loops within loops, with complex interactions
between each loop body and its surrounding context. It's generally
impossible (or very hard) to extricate the inner loop code from its
tightly-coupled context, which means your unittest will have a hard time
"reaching into" the innards of the nested loops to verify the
correctness of each piece of code.  Often, the result is that rather
than testing each *unit* of the code, you have to do a holistic test by
running the entire machinery of nested loops inside a sandbox and
capturing its output (via stdout redirection, or instrumenting dependent
subsystems, etc.) -- hardly a *unit* test anymore! -- and still, you
have the problem that there are too many possible code paths that these
nested loops may run through, so it would be hard to have any confidence
that you've covered sufficiently many paths in the test. You're almost
certain to miss important boundary cases.


T

-- 
The irony is that Bill Gates claims to be making a stable operating system and Linus Torvalds claims to be trying to take over the world. -- Anonymous


More information about the Digitalmars-d mailing list