[rust-dev] Half-indent for case blocks looks terrible

Brendan Eich brendan at mozilla.org
Fri Jul 22 07:52:10 PDT 2011


On Jul 22, 2011, at 6:46 AM, Marijn Haverbeke wrote:

> Having some braces four spaces apart and others only two makes for a
> very jarring visual effect. We could probably get used to it, but if
> somebody has a better idea, I think we should consider it.

I'm probably to blame for this style meme (although I got it from Kipp Hickman, my old SGI / Netscape colleague), I do it i in C code for labels (goto and switch-case). But C does not require braces around case statements, of course, so you don't get the half-indented closing braces, as in:

    switch (foo) {
      case BAR1: {
        ...
      }
      case BAR2: {
        ... // don't forget to break!
      }
      default: {
         ...
      }
    }

which I agree are a bit disconcerting and unaesthetic.

The motivation is that labels are not statements. They should not indent four spaces, and then their labeled statements indent four more. That tends to over-indent too much, and the labels have no control flow nesting property that needs any more indentation than their labeled statements.

K&R and old Unix ken&dmr style simply does not indent labels, so one can always fall back on that.

A lot of Mozilla code, C++ and JS, uses two-space c-basic-offset and does indent case labels a full two spaces, then two more for the labeled statements (I don't think goto labels get the same treatment, but this is C++ code so gotos are less common; JS break/continue-to-label is also rare).

Two-space c-basic-offset works a bit better with multiline if conditions:

  if (very_long_conjunct_goes_here() &&
      another_whopper_here()) {
    consequent_at_distinct_indentation_level();
    ...
  }

With four-space indentation units, the condition's overflow line and the consequent block kids start at the same indentation, and are that much harder to distinguish at a glance. This has led to a SpiderMonkey style variation where the { in such a situation moves to its own line, in the same column as the }.

Style, yay.

/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20110722/1eb8610c/attachment.html>


More information about the Rust-dev mailing list