Cliff Woolley
2003-10-02 21:38:12 UTC
So this is NOT intended to be the catalyst for a huge heated debate, it's
just a suggestion, but I propose the following tweaks to the styleguide.
For the most part, it's just clarifying the English. The only real
changes are ones intended to codify two items most of us already do
anyway: (a) if there is a multi-line conditional expression (with
if/for/etc), then you put the { on the next line to separate the code from
the condition; (b) all blocks should have {}'s, even if they are only one
line long.
--Cliff
------------------------------------------------------
eg:
if (foo) {
then();
}
not
if (foo)
then();
and
if (foo && bar && baz &&
quux && crash && burn)
{
then();
}
not
if (foo && bar && baz &&
quux && crash && burn) {
then();
}
------------------------------------------------------
Index: styleguide.xml
===================================================================
RCS file: /home/cvs/httpd-site/xdocs/dev/styleguide.xml,v
retrieving revision 1.2
diff -u -d -r1.2 styleguide.xml
--- styleguide.xml 2 Oct 2002 00:18:06 -0000 1.2
+++ styleguide.xml 2 Oct 2003 18:50:30 -0000
@@ -13,19 +13,18 @@
href="mailto:***@awe.com">***@awe.com</a></strong>. Based on a vote
taken in November, 1996.
<br />
-Further refinements voted upon in July 1997.
+Further refinements voted upon in July 1997 and October 2003.
</section>
<section>
<title>Introduction</title>
-<p>[This bit could state that code should be laid out to be clear to
-someone else familiar with Apache. Functions should be short and
-easily understood. Comments should be provided to explain the
-rationale for code which is not obvious, and to document behavior of
-functions. The guidelines can be broken if necessary to acheive a
-clearer layout]</p>
+<p>Code should be laid out to be clear to someone else familiar with Apache.
+Functions should be short and easily understood. Comments should be provided to
+explain the rationale for code which is not obvious and to document behavior
+of functions. The guidelines can be broken if necessary to achieve a clearer
+layout.</p>
-<p>This style can be generated with the following arguments to GNU indent:</p>
+<p>This style can be approximated with the following arguments to GNU indent:</p>
<p><ul><code>-i4 -npsl -di0 -br -nce -d0 -cli0 -npcs -nfc1 -nut</code></ul></p>
@@ -34,13 +33,18 @@
<section>
<title>The Guidelines</title>
<UL>
- <li>Opening braces are given on the same lines as statements, or on the
- following line at the start of a function definition.
+ <li>Opening braces are given on the same lines as statements, except
+ at the start of a function definition and at the start of a
+ conditional block with a multi-line condition, where they are on their
+ own line.
</li>
- <li>Code inside a block (whether surrounded by braces or not) is
- indented by four space characters. Tab characters are not
- used. Comments are indented to the same level as the surrounding
- code.
+ <li>
+ All blocks (including conditional blocks of only one line) are surrounded
+ by braces.
+ </li>
+ <li>Code inside a block is indented by four space characters. Tab
+ characters are not used. Comments are indented to the same level as
+ the surrounding code.
</li>
<li>Closing braces are always on a separate line from surrounding
code, and are indented to line up with the start of the text on
@@ -48,7 +52,7 @@
</li>
<li>Functions are declared with ANSI-style arguments.
</li>
- <li>There is no space between the function name and the opening bracket
+ <li>There is no space between the function name and the opening parenthesis
of the arguments to the function. There is a single space following
commas in argument lists and the semi-colons in for statements.
</li>
@@ -99,8 +103,10 @@
and the wrapped portion is indented under the first term in the expression
(or the first argument to the function).
Conditional expressions should be wrapped to keep single or
-parenthesized terms as atomic as possible, and place Boolean operators
-at either the start (preferable) or end of the line.
+parenthesized terms as atomic as possible, placing boolean operators
+at either the start or end of the line. The opening brace of the block
+following this kind of multi-line condition should be on its own line
+to help distinguish the code in the block from the condition itself.
</p>
<p>
Example:
@@ -110,8 +116,9 @@
const char *args, void *foo,
int k)
- if (cond1 && (item2 || item3) && (!item4)
- && (item5 || item6) && item7) {
+ if (cond1 && (item2 || item3) && (!item4) &&
+ (item5 || item6) && item7)
+ {
do_a_thing();
}
</pre>
@@ -127,9 +134,13 @@
<p>Example:</p>
<pre>
- code;
/* comment */
code;
+ /* really
+ * long
+ * comment
+ */
+ code;
</pre>
</li>
<li><strong>Function Declaration and Layout</strong>
@@ -146,7 +157,7 @@
<p>The return type is placed on the same line as the function. Arguments
(if any) are given in ANSI style. If no arguments, declare function
as <samp>void</samp>. No space
- between function name and opening bracket, single space after comma
+ between function name and opening parenthesis, single space after comma
separating each argument. The opening brace is placed on the line
after the definition, indented to line up with the start of the
return type text. The code is indented with four spaces, and the closing
@@ -157,7 +168,7 @@
<li><strong>Function Calls</strong>
<p>Space after commas in function calls. No space between function name
- and opening bracket.</p>
+ and opening parenthesis.</p>
<pre>
f(a, b);
</pre>
@@ -169,7 +180,8 @@
<li><strong>Flow-Control Layout</strong>
<p>Flow-control statements (<samp>if</samp>, <samp>while</samp>,
- <samp>for</samp>, <EM>etc.</EM>) are laid out as in this example:</p>
+ <samp>for</samp>, <EM>etc.</EM>) are laid out as in this example
+ (using braces even when "code;" is a single line):</p>
<pre>
if (expr) {
@@ -180,10 +192,11 @@
}
</pre>
-<p>There is a space between the keyword and the opening bracket.
+<p>There is a space between the keyword and the opening parenthesis.
Opening brace placed on same line as the flow keyword. The code
itself is indented by four spaces. The closing brace is indented to
- line up with the opening brace. If an <samp>else</samp> clause is used, the
+ line up with the first character of the keyword. If an
+ <samp>else</samp> clause is used, the
<samp>else</samp> keyword is placed on the line following the closing
brace and is indented to line up with the corresponding
<samp>if</samp>. <strong>Also
@@ -234,7 +247,7 @@
</pre>
</li>
-<li><strong>Capitalisation of Enums</strong>
+<li><strong>Capitalization of Enums</strong>
<p>No rule.</p>
</li>
</ol>
just a suggestion, but I propose the following tweaks to the styleguide.
For the most part, it's just clarifying the English. The only real
changes are ones intended to codify two items most of us already do
anyway: (a) if there is a multi-line conditional expression (with
if/for/etc), then you put the { on the next line to separate the code from
the condition; (b) all blocks should have {}'s, even if they are only one
line long.
--Cliff
------------------------------------------------------
eg:
if (foo) {
then();
}
not
if (foo)
then();
and
if (foo && bar && baz &&
quux && crash && burn)
{
then();
}
not
if (foo && bar && baz &&
quux && crash && burn) {
then();
}
------------------------------------------------------
Index: styleguide.xml
===================================================================
RCS file: /home/cvs/httpd-site/xdocs/dev/styleguide.xml,v
retrieving revision 1.2
diff -u -d -r1.2 styleguide.xml
--- styleguide.xml 2 Oct 2002 00:18:06 -0000 1.2
+++ styleguide.xml 2 Oct 2003 18:50:30 -0000
@@ -13,19 +13,18 @@
href="mailto:***@awe.com">***@awe.com</a></strong>. Based on a vote
taken in November, 1996.
<br />
-Further refinements voted upon in July 1997.
+Further refinements voted upon in July 1997 and October 2003.
</section>
<section>
<title>Introduction</title>
-<p>[This bit could state that code should be laid out to be clear to
-someone else familiar with Apache. Functions should be short and
-easily understood. Comments should be provided to explain the
-rationale for code which is not obvious, and to document behavior of
-functions. The guidelines can be broken if necessary to acheive a
-clearer layout]</p>
+<p>Code should be laid out to be clear to someone else familiar with Apache.
+Functions should be short and easily understood. Comments should be provided to
+explain the rationale for code which is not obvious and to document behavior
+of functions. The guidelines can be broken if necessary to achieve a clearer
+layout.</p>
-<p>This style can be generated with the following arguments to GNU indent:</p>
+<p>This style can be approximated with the following arguments to GNU indent:</p>
<p><ul><code>-i4 -npsl -di0 -br -nce -d0 -cli0 -npcs -nfc1 -nut</code></ul></p>
@@ -34,13 +33,18 @@
<section>
<title>The Guidelines</title>
<UL>
- <li>Opening braces are given on the same lines as statements, or on the
- following line at the start of a function definition.
+ <li>Opening braces are given on the same lines as statements, except
+ at the start of a function definition and at the start of a
+ conditional block with a multi-line condition, where they are on their
+ own line.
</li>
- <li>Code inside a block (whether surrounded by braces or not) is
- indented by four space characters. Tab characters are not
- used. Comments are indented to the same level as the surrounding
- code.
+ <li>
+ All blocks (including conditional blocks of only one line) are surrounded
+ by braces.
+ </li>
+ <li>Code inside a block is indented by four space characters. Tab
+ characters are not used. Comments are indented to the same level as
+ the surrounding code.
</li>
<li>Closing braces are always on a separate line from surrounding
code, and are indented to line up with the start of the text on
@@ -48,7 +52,7 @@
</li>
<li>Functions are declared with ANSI-style arguments.
</li>
- <li>There is no space between the function name and the opening bracket
+ <li>There is no space between the function name and the opening parenthesis
of the arguments to the function. There is a single space following
commas in argument lists and the semi-colons in for statements.
</li>
@@ -99,8 +103,10 @@
and the wrapped portion is indented under the first term in the expression
(or the first argument to the function).
Conditional expressions should be wrapped to keep single or
-parenthesized terms as atomic as possible, and place Boolean operators
-at either the start (preferable) or end of the line.
+parenthesized terms as atomic as possible, placing boolean operators
+at either the start or end of the line. The opening brace of the block
+following this kind of multi-line condition should be on its own line
+to help distinguish the code in the block from the condition itself.
</p>
<p>
Example:
@@ -110,8 +116,9 @@
const char *args, void *foo,
int k)
- if (cond1 && (item2 || item3) && (!item4)
- && (item5 || item6) && item7) {
+ if (cond1 && (item2 || item3) && (!item4) &&
+ (item5 || item6) && item7)
+ {
do_a_thing();
}
</pre>
@@ -127,9 +134,13 @@
<p>Example:</p>
<pre>
- code;
/* comment */
code;
+ /* really
+ * long
+ * comment
+ */
+ code;
</pre>
</li>
<li><strong>Function Declaration and Layout</strong>
@@ -146,7 +157,7 @@
<p>The return type is placed on the same line as the function. Arguments
(if any) are given in ANSI style. If no arguments, declare function
as <samp>void</samp>. No space
- between function name and opening bracket, single space after comma
+ between function name and opening parenthesis, single space after comma
separating each argument. The opening brace is placed on the line
after the definition, indented to line up with the start of the
return type text. The code is indented with four spaces, and the closing
@@ -157,7 +168,7 @@
<li><strong>Function Calls</strong>
<p>Space after commas in function calls. No space between function name
- and opening bracket.</p>
+ and opening parenthesis.</p>
<pre>
f(a, b);
</pre>
@@ -169,7 +180,8 @@
<li><strong>Flow-Control Layout</strong>
<p>Flow-control statements (<samp>if</samp>, <samp>while</samp>,
- <samp>for</samp>, <EM>etc.</EM>) are laid out as in this example:</p>
+ <samp>for</samp>, <EM>etc.</EM>) are laid out as in this example
+ (using braces even when "code;" is a single line):</p>
<pre>
if (expr) {
@@ -180,10 +192,11 @@
}
</pre>
-<p>There is a space between the keyword and the opening bracket.
+<p>There is a space between the keyword and the opening parenthesis.
Opening brace placed on same line as the flow keyword. The code
itself is indented by four spaces. The closing brace is indented to
- line up with the opening brace. If an <samp>else</samp> clause is used, the
+ line up with the first character of the keyword. If an
+ <samp>else</samp> clause is used, the
<samp>else</samp> keyword is placed on the line following the closing
brace and is indented to line up with the corresponding
<samp>if</samp>. <strong>Also
@@ -234,7 +247,7 @@
</pre>
</li>
-<li><strong>Capitalisation of Enums</strong>
+<li><strong>Capitalization of Enums</strong>
<p>No rule.</p>
</li>
</ol>