From a040c2487e00403e7785ae3f622bd2cd48587d37 Mon Sep 17 00:00:00 2001 From: David Jones Date: Mon, 4 Jun 2007 15:42:02 +0100 Subject: [PATCH] Mps: updated coding style. Copied from Perforce Change: 162482 ServerID: perforce.ravenbrook.com --- mps/design/cstyle/index.html | 113 ++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 48 deletions(-) diff --git a/mps/design/cstyle/index.html b/mps/design/cstyle/index.html index 0b7556f2262..65a3b389356 100644 --- a/mps/design/cstyle/index.html +++ b/mps/design/cstyle/index.html @@ -54,8 +54,11 @@

2. General Formatting Conventions

Line Width

-.width: Lines should be no wider than 80 characters. [This rule will -change to 72 -- DRJ 2007-06-04] +.width: Lines should be no wider than 72 characters. .width.why: Many +people use 80 column terminal windows so that multiple windows can be +placed side by side. Restricting lines to 72 characters allows line +numbering to be used (in vi for example) and also allows diffs to be +displayed without overflowing the terminal.

@@ -63,7 +66,8 @@

White Space

.space.notab: No tab characters should appear in the source files. Ordinary -spaces should be used to indent and format the sources. Tab characters are +spaces should be used to indent and format the sources. +.space.notab.why: Tab characters are displayed differently on different platforms, and sometimes translated back and forth, destroying layout information.

@@ -83,8 +87,9 @@

White Space

foo = x + y*z; -[to be announced: rule about having no white space after if/for/while -- -DRJ 2007-06-04] +

+.space.control.not: No space between the keywords if, +for, while and the following paren.

Sections and Paragraphs

@@ -179,24 +184,28 @@

Indentation

-.indent.elseif: As a special exception to the indentation rule above, an else -may be immediately followed by an if, in which case the if body is at the same -level as the previous if body. For example: +.indent.elseif: The usual style for else if chains is +a special exception to the indentation rule above: an else +may be immediately followed by an if, in which case the +if body is at the same +level as the previous if body. For example:

   if(j == block->base) {
-    if(j+step == block->limit)
+    if(j+step == block->limit) {
       putc('@', stream);
-    else
+    } else {
       putc('[', stream);
+    }
   } else if(j+step == block->limit) {
     putc(']', stream);
     pop_bracket();
   } else if(j > block->base && j < block->limit)
     putc('=', stream);
-  else
+  } else {
     putc('.', stream);
+  }
 

@@ -251,8 +260,7 @@

Indentation

.indent.expr: Note that when breaking an expression it is clearer to place the operator at -the start of the continuation line: [Is it? Opinions vary -- DRJ -2007-06-04] +the start of the continuation line:

@@ -261,6 +269,16 @@ 

Indentation

<= AddrAdd(chunk->base, chunk->ullageSize));
+

+This is particularly useful in long conditional expressions that use && +and ||. For example: +

+ +
+  } while(trace->state != TraceFINISHED
+          && (trace->emergency || traceWorkClock(trace) < pollEnd));
+
+

Positioning of Braces

@@ -294,44 +312,11 @@

Positioning of Braces

-The same applies to struct, enum, union, and function definitions. +The same applies to struct, enum, union.

-.brace.otb.split: There is a further division of the OTB style. -

- -

-.brace.otb.most: The "At Most One True Brace" allows one to have an if -statement with a block statement in one tail and an unblocked statement in the -other. Like this: -

- -
-  if(isBad) {
-    FooBar();
-    BarFoo();
-  } else
-    Zing();
-
- -

-.brace.otb.exactly: The "Exactly One True Brace" does not admit the mixture of -braces and not-braces in "if"s. The above example in EOTB would be: -

- -
-  if(isBad) {
-    FooBar();
-    BarFoo();
-  } else {
-    Zing();
-  }
-
- -

-.brace.otb.more: There are further refinements according to whether you admit -any non-braced statements in "if"s, "while"s, and so on. +.brace.otb.function.not: OTB is never used for function definitions.

@@ -351,6 +336,24 @@

Positioning of Braces

} +

+.brace.always: Braces are always required for if, for, +while, and so on. +.brace.always.except: Except that an if with no else +is allowed to drop its braces when its body is a single simple statement. +Typically this will be a goto or an assignment. For example: +

+ +
+  if(res != ResOK)
+    goto failStart;
+
+ +

+Note in particular that an if with an else must have +braces on both paths. +

+

.brace.case: If you need to put braces around the "contents" of a case statement (inside a switch) then do it like this: @@ -449,6 +452,11 @@

Formatting Comments

if(isBase) { +

+.comment.para.precede: Paragraph comments, even one-liners, precede the +code to which they apply. +

+

.comment.column: Columnar comments appear in a column to the right of the code. They should be used sparingly, since they clutter the code and make it @@ -484,6 +492,15 @@

B. History

and edited. + +2007-06-04 +DRJ +Changed .width from 80 to 72. Banned space between "if" and "(". +Required braces on almost everything. Clarified that paragraph comments +precede the code. + + +

C. Copyright and License