[Swan-dev] readable C style for split control statements

D. Hugh Redelmeier hugh at mimosa.com
Sun Jun 15 21:08:01 EEST 2014


Here's a change that Tuomo just checked in.  This changes the
indentation of the folded part of an if statement:

        if (conn->options_set[KBF_DPDDELAY] &&
            conn->options_set[KBF_DPDTIMEOUT]) {
                msg.dpd_delay = deltatime(conn->options[KBF_DPDDELAY]);
                msg.dpd_timeout = deltatime(conn->options[KBF_DPDTIMEOUT]);
=>
        if (conn->options_set[KBF_DPDDELAY] &&
                conn->options_set[KBF_DPDTIMEOUT]) {
                msg.dpd_delay = deltatime(conn->options[KBF_DPDDELAY]);
                msg.dpd_timeout = deltatime(conn->options[KBF_DPDTIMEOUT]);

I read code from the left.  When I'm scanning the revised version, I
start with the expectation that the second line is a statement within
the then-part of the if.  Only when I get to the end do I find that it
is actually more of the predicate.

When I read the original version, the distinct indentation makes it
clear that the second line is part of the predicate.

We don't want to create
<http://en.wikipedia.org/wiki/Garden_path_sentence>

I propose that each broken control-statement head be indented in such a way
as to make the fragments visually distinct from the body.  This will
require spaces for the last bit of the indentation.

Thus the version before the change is correct and the changed version is 
wrong.


More information about the Swan-dev mailing list