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)
            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))
        if (!vfNoInval)
            InvalText (pca, fFalse /* fEdit */);

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.

Gravatar Alex J Tuesday, April 15, 2014
Having the curlies indented under an if can be considered more consistent, if you consider that an if statement should begin an indentation level. If you define the if statement as: if (condition) statement-or-block Then this is consistent: if (a == b) foo(); // A statement if (a == b) { // A block foo1(); foo2(); } They are after all block delimiters, a way to group several statements so they can be seen as one, and not extensions of the if statement, which is what putting the curly on the same line as the IF seems to imply. It's not a style I personally use, but I can see why would someone use it.
Gravatar Alex J Tuesday, April 15, 2014
Comment whitespace is mangled, see: https://gist.github.com/anonymous/b92d124ac1264dd07bd6
Gravatar Jesse C. Slicer Tuesday, April 15, 2014
I literally cannot understand a single line of this function. What I mean, is, that I can describe the technical bits that occur with compiler-level accuracy, but knowing what any bit of it is actually designed to accomplish is like trying to decode the cryptonomicon.
Gravatar Dave Davis Wednesday, April 16, 2014
putting ownership on each method may have something to do with the source control system of the time. Now a days you can track line by line changes made and who made those changes.
Gravatar gun Thursday, April 17, 2014
Easy to criticize now :-) This is a 30-years piece of code! At that time, many of us programmers were using assembler or similar. Compilers where itself limited in many ways (like identifiers name or length).
Comments are closed.