What does style look like, part 3

When we last left the code we were looking at, it looked like:

#include "list.h"main(C cArg, SZ rgszArg[]) {    I iNode, cNodes = atoi(rgszArg[1]), cNodesToSkip = atoi(rgszArg[2]);    PNODE pnodeT, pnodeCur;     InitNodes(cNodes);    for (iNode = 2, pnodeCur = PnodeNew(1); iNode <= cNodes ; iNode++) {        pnodeT = PnodeNew(i);         InsertNext(pnodeCur, pnodeT);         pnodeCur = pnodeT;     }    while (pnodeCur != PnodeNext(x)) {        for (iNode = 1; iNode < cNodesToSkip ; iNode++)             pnodeCur = PnodeNext(pnodeCur);        FreeNode(PnodeDeleteNext(pnodeCur));    }    printf("%d\n", Item(nodeCur));}

This time, let's consider a different indentation style:  Braces after the expression, every optional brace included (modified BSD)

#include "list.h"main(C cArg, SZ rgszArg[]) {    I iNode, cNodes = atoi(rgszArg[1]), cNodesToSkip = atoi(rgszArg[2]);    PNODE pnodeT, pnodeCur;     InitNodes(cNodes);    for (iNode = 2, pnodeCur = PnodeNew(1); iNode <= cNodes ; iNode++)     {        pnodeT = PnodeNew(i);         InsertNext(pnodeCur, pnodeT);         pnodeCur = pnodeT;     }    while (pnodeCur != PnodeNext(x))     {        for (iNode = 1; iNode < cNodesToSkip ; iNode++)         {            pnodeCur = PnodeNext(pnodeCur);        }        FreeNode(PnodeDeleteNext(pnodeCur));    }    printf("%d\n", Item(nodeCur));}

That's a fascinating difference.  Moving the braces down to the next line opens the code up quite a bit, making it easier to see how the code is structured.  Next, what happens with the declarations - the declaration of iNode,cNodes, and cNodesToSkip is smushed together, lets split them out into separate lines.

#include "list.h"main(C cArg, SZ rgszArg[]) {    I iNode;    I cNodes = atoi(rgszArg[1]);    I cNodesToSkip = atoi(rgszArg[2]);    PNODE pnodeT;    PNODE pnodeCur;     InitNodes(cNodes);    for (iNode = 2, pnodeCur = PnodeNew(1); iNode <= cNodes ; iNode++)     {        pnodeT = PnodeNew(i);         InsertNext(pnodeCur, pnodeT);         pnodeCur = pnodeT;     }    while (pnodeCur != PnodeNext(x))     {        for (iNode = 1; iNode < cNodesToSkip ; iNode++)         {            pnodeCur = PnodeNext(pnodeCur);        }        FreeNode(PnodeDeleteNext(pnodeCur));    }    printf("%d\n", Item(nodeCur));}

Again, this helps a bit, but not a lot.  What about other bracing styles?

#include "list.h"main(C cArg, SZ rgszArg[]) {        I iNode;        I cNodes = atoi(rgszArg[1]);        I cNodesToSkip = atoi(rgszArg[2]);        PNODE pnodeT;        PNODE pnodeCur;         InitNodes(cNodes);        for (iNode = 2, pnodeCur = PnodeNew(1); iNode <= cNodes ; iNode++)         {                pnodeT = PnodeNew(i);                 InsertNext(pnodeCur, pnodeT);                 pnodeCur = pnodeT;         }        while (pnodeCur != PnodeNext(x))         {                for (iNode = 1; iNode < cNodesToSkip ; iNode++)                 {                        pnodeCur = PnodeNext(pnodeCur);                }                FreeNode(PnodeDeleteNext(pnodeCur));        }        printf("%d\n", Item(nodeCur));}

8 character indents stretch the code out a bunch, but they don't effect the overall effect.  How about a variant of 8 char indents?

#include "list.h"main(C cArg, SZ rgszArg[])     {        I iNode;        I cNodes = atoi(rgszArg[1]);        I cNodesToSkip = atoi(rgszArg[2]);        PNODE pnodeT;        PNODE pnodeCur;         InitNodes(cNodes);        for (iNode = 2, pnodeCur = PnodeNew(1); iNode <= cNodes ; iNode++)             {                pnodeT = PnodeNew(i);                 InsertNext(pnodeCur, pnodeT);                 pnodeCur = pnodeT;             }        while (pnodeCur != PnodeNext(x))             {                for (iNode = 1; iNode < cNodesToSkip ; iNode++)                     {                        pnodeCur = PnodeNext(pnodeCur);                    }                FreeNode(PnodeDeleteNext(pnodeCur));            }        printf("%d\n", Item(nodeCur));    }

This one's kinda interesting.  The braces are now more clearly laid out.  On the other hand, it's a bit disconcerting to have the "printf" hanging out on its own.  Personally, I'm not too happy with this one.

How about the "braces appear on the same line as the code they wrap"?

#include "list.h"main(C cArg, SZ rgszArg[]) {    I iNode;    I cNodes = atoi(rgszArg[1]);    I cNodesToSkip = atoi(rgszArg[2]);    PNODE pnodeT;    PNODE pnodeCur;     InitNodes(cNodes);    for (iNode = 2, pnodeCur = PnodeNew(1); iNode <= cNodes ; iNode++)         {        pnodeT = PnodeNew(i);         InsertNext(pnodeCur, pnodeT);         pnodeCur = pnodeT;         }    while (pnodeCur != PnodeNext(x))         {        for (iNode = 1; iNode < cNodesToSkip ; iNode++)             {            pnodeCur = PnodeNext(pnodeCur);            }        FreeNode(PnodeDeleteNext(pnodeCur));        }    printf("%d\n", Item(nodeCur));}

An interesting variant.  I don't know if it's an improvement, but...

Just for grins, the style of a senior developer within Microsoft:

#include "list.h"main(C cArg, SZ rgszArg[]) {    I iNode;    I cNodes = atoi(rgszArg[1]);    I cNodesToSkip = atoi(rgszArg[2]);    PNODE pnodeT;    PNODE pnodeCur;     InitNodes(cNodes);    for (iNode = 2, pnodeCur = PnodeNew(1); iNode <= cNodes ; iNode++) {        pnodeT = PnodeNew(i);         InsertNext(pnodeCur, pnodeT);         pnodeCur = pnodeT;         }    while (pnodeCur != PnodeNext(x)) {        for (iNode = 1; iNode < cNodesToSkip ; iNode++) {            pnodeCur = PnodeNext(pnodeCur);            }        FreeNode(PnodeDeleteNext(pnodeCur));        }    printf("%d\n", Item(nodeCur));    }

In this style, the opening brace goes with the if/while/for loop, but the closing brace goes with the code at that level of indentation.  Again, it's an "interesting" choice.

Tomorrow, we add comments.  That'll help a bundle towards helping this out.