I’ve spent the last two days in Damian Conway’s “Advanced Module Implementation” course. In it he steps through, line by line, the core code of his Attribute::Handlers, NEXT, Class::Delegation, Inline::Files, Filter::Simple, Perl6::Variables, Perl6::Currying, Hook::Lexwrap, Regexp::Common, and, yes, Acme::Bleach modules to show lots of different ways in which to build modules. And twist your brain in lots of painful ways along the way.
A key refrain along the way is that of Perl Aikido – having enough mastery of your craft to know how to get lots done, not by writing lots of code, but by doing just the right thing at the right time. Most of these modules are pretty much implemented in less than one screenful of code. And whilst some of it is insanely devious (such as the infamous
goto , "$imposter" } }; line), much of it is just piecing together straightforward things in an unusual way (the much more subtle deviousness of Regexp::Common that doesn’t look at all scary until you try to follow just what is actually going on).
I’ve recently been noticing a glitch in my theory of extraneous code. Almost everyone knows that a beginner in any programming language writes far too much code. They don’t yet know all the idioms of the language, and try to replicate the approaches they know from other languages, even if this language has a built in manner for doing this. As you get better at writing the language you learn the better approaches, and your code gets shorter. However, I no longer think there’s a simple progression from this point to the ‘master’ level where modules like the above ones can be written in one screenful of code. I think that ‘expert’ programmers, still on their way towards ‘master’, actually revert to writing much too much code again. When faced with a difficult problem, they know enough of the nasty things they can do to attempt to stomp through the problem. Ironically someone with less knowledge would probably not attempt this, and try to find a way around the problem instead. An expert may have lots more tools in his toolbox, but it takes a master to know exactly which one is best for every job.