Block call

When duplicated codes are contiguous blocks, they are easy to detect and factor. Simply subcode will do -- with parameters if necessary. However, I often find another class of code duplication very annoying:

open In, $file or die "I can't open $file!\n";
    # process the file line by line
    # potentially very complexed but interesting each time.
close In

Before MyDef, I must have repeated such code thousands of times. Albeit simple, every time it takes almost a minute -- note that I have to put down the closing bracket and the "close" statement as well -- before I get to the interesting part in the middle, and by that time, my working memory has already been flushed and I need pause to remember what kind of interesting stuff I was thinking ...

What I wished was write code like this:

&call open_r, $file
    # do my interesting stuff ...

MyDef allows just like that! To achieve that magic, first we have to change from '$call' to '&call' -- we have to tell MyDef what to expect; MyDef never pretends to be as intelligent as we are. Next we have to define the subcode a bit unusual:

subcode: open_r(name)
    open In, "$(name)" or die "I can't open $(name)!\n";
    close In;

You certainly noticed what special we have done -- a special place holder, BLOCK. And that is all.

It is easy to be carried away. One time I was programming a 3-dimension array, and I have:

subcode: loop_x

subcode: loop_y

subcode: loop_z

subcode: loop_xyz
    &call loop_z
        &call loop_y
            &call loop_x

page: t
    module: c

    &call loop_xyz
        # manipulate array[i]

I'll leave to you to judge whether that is too obssessive.

results matching ""

    No results matching ""