Splitting code into the *smallest* pieces possible pieces that have
"independent meaning" has two, no three advantages:
1. (Mainly aesthetic) The code that uses the small "helpers" looks
elegant.
2. (Important) It is much easier to generate comprehensive unit tests
for small methods that do only one (simple) thing.
3. (Very important) It is often possible to test small pieces even
when it is difficult or even impossible to test the larger pieces.
For example, gui code and code that uses separate processes may be
difficult to test as an overall unit. But helpers may have nothing to
do with the gui or with separate processes.
For example, yesterday I wrote a unit test of the Inkscape code that
bypassed the interactive invocation of Inkscape. The test doesn't
test everything, but it does test a lot. Many other unit tests could
be written that tests bits and pieces of the code.
As another example, here is the run method in @button edit-screenshot,
the "post production" slideshow script::
def run (self):
'''Compose a call to c.inkscapeCommands.run
based on @callout and @marker nodes.'''
c = self.c ; p = self.p
if not self.inSlideShow(p): return
fn = self.getFn(p)
if not fn: return
callouts = self.getCallouts(p)
markers = self.getMarkers(p)
c.inkscapeCommands.run(fn,
callouts=callouts,
markers=markers,
edit_flag=callouts or markers,
png_fn=self.getPngFn(fn),
svg_fn=self.getSvgFn(fn),
template_fn=self.template_fn)
This example illustrates all three points above:
1. It is elegant: the helpers hide the details so it is very clear
what the code is doing.
2. Unit testing each helper will be as simple as possible.
3. Unit tests for all the helpers are practical even though a unit
test for the run method itself would not be practical. (If edit_flag
is True the unit test would invoke Inkscape interactively.)
In summary, the title of this post is a little joke. This Aha is a
significant step forward in my understanding--anything that makes unit
testing simpler and more applicable has fundamental importance.
Edward
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/leo-editor?hl=en.