OdeToCode IC Logo

Deconstructing A Function From Microsoft Word 1.1a

Tuesday, April 15, 2014

When Microsoft released the source code to MS-DOS and Word, I had to take a look. One of the first functions I came across was ReplacePropsCa from the srchfmt.c file.

/* %%Function:ReplacePropsCa %%Owner:rosiep */
ReplacePropsCa(prpp, pca)
struct RPP *prpp;
struct CA *pca;
{
    struct CA caInval;

    if (prpp->cbgrpprlChp)
        {
        ExpandCaSprm(pca, &caInval, prpp->grpprlChp);
        ApplyGrpprlCa(prpp->grpprlChp, prpp->cbgrpprlChp, pca);
        if (!vfNoInval)
            {
            InvalCp(pca);
            InvalText(pca, fFalse /* fEdit */);
            }
        }

    if (prpp->cbgrpprlPap)
        {
        int fStc;
        struct CHP chp;
        struct PAP pap;

        if (fStc = (*prpp->grpprlPap == sprmPStc))
            {
            CachePara(pca->doc, pca->cpFirst);
            pap = vpapFetch;
            }
        ExpandCaSprm(pca, &caInval, prpp->grpprlPap);
        ApplyGrpprlCa(prpp->grpprlPap, prpp->cbgrpprlPap, pca);
        if (fStc)
            {
            GetMajorityChp(pca, &chp);
            EmitSprmCMajCa(pca, &chp);
            if (!FMatchAbs(pca->doc, &pap, &vpapFetch))
                InvalPageView(pca->doc);
            }
        if (!vfNoInval)
            {
            InvalCp(&caInval);
            InvalText (pca, fFalse /* fEdit */);
            DirtyOutline(pca->doc);
            }
        }
}

Thought #1: Every Function Has An Owner. Although I see the occasional project where each file has a comment indicating the owner, I don’t remember ever seeing ownership declared on individual functions. I think the concept of collective ownership is a healthier approach to building software, both for the software and the developers. Today’s tools also make it easier to jump around in code.

Thought #2: The Flow Control Is All Wrong. Oh, wait, the flow control seems ok, it’s just the funny indentation of curly braces setting off alarm bells. Joel Spolsky has a post from 2005 titled Making Wrong Code Look Wrong in which he says:

This is the real art: making robust code by literally inventing conventions that make errors stand out on the screen.

After many years in 3 different languages using { and }, my eyes are accustomed to looking for a closing curly brace in the same column as the if. Not seeing the curly means code might accidently execute outside the conditional check. This function hides the closing curly and is full of evil.

Thought #3: The Notation Is Hilarious. Call it Hungarian Notation, or Anti-Hungarian Notation, or something not Hungarian at all but a custom DSL designed in C. In any case the idea of checking to see if a prpp->grpplPap is equal to a sprmPStc is just one brick in a wall of gibberish that reminds me of a Lewis Carroll poem.

`Twas brillig, and the slithy toves
  Did gyre and gimble in the wabe:
All mimsy were the borogoves,
  And the mome raths outgrabe.

Both the function and the poem include gibberish, but at least the Lewis Carroll poem rhymes.