Patchwork CODING_STYLE: don't allow non-indented statements after if/else blocks

login
register
mail settings
Submitter Aurelien Jarno
Date Oct. 26, 2009, 6:26 a.m.
Message ID <20091026062637.GA21562@volta.aurel32.net>
Download mbox | patch
Permalink /patch/36883/
State New
Headers show

Comments

Aurelien Jarno - Oct. 26, 2009, 6:26 a.m.
Rationale: The following code is difficult to read, but allowed by the
current coding style.

    if (a == 5) printf("a was 5.\n");
    else if (a == 6) printf("a was 6.\n");
    else printf("a was something else entirely.\n");

Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
---
 CODING_STYLE |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
Blue Swirl - Oct. 26, 2009, 4:02 p.m.
On Mon, Oct 26, 2009 at 8:26 AM, Aurelien Jarno <aurelien@aurel32.net> wrote:
> Rationale: The following code is difficult to read, but allowed by the
> current coding style.

Fully agree.

> +Every control flow statement is followed by a new indented and braced
> +block; even if the block contains just one statement.  The opening brace
> +is on the line that contains the control flow statement that introduces
> +the new block; the closing brace is on the same line as the else keyword,
> +or on a line by itself if there is no else keyword.  Example:

I think an exception should be granted for "else if" case, otherwise
the style would require braces around "if", like:
    if (a == 5) {
        printf("a was 5.\n");
    } else {
        if (a == 6) {
            printf("a was 6.\n");
        }
    } else {
        printf("a was something else entirely.\n");
    }

Picking nits: "while" is a control flow statement, even in "do {}
while" statement and then it would illegal to require a braced block
after the "while" statement.

Patch

diff --git a/CODING_STYLE b/CODING_STYLE
index a579cb1..3ffbc3d 100644
--- a/CODING_STYLE
+++ b/CODING_STYLE
@@ -51,11 +51,11 @@  QEMU coding style.
 
 4. Block structure
 
-Every indented statement is braced; even if the block contains just one
-statement.  The opening brace is on the line that contains the control
-flow statement that introduces the new block; the closing brace is on the
-same line as the else keyword, or on a line by itself if there is no else
-keyword.  Example:
+Every control flow statement is followed by a new indented and braced
+block; even if the block contains just one statement.  The opening brace
+is on the line that contains the control flow statement that introduces
+the new block; the closing brace is on the same line as the else keyword,
+or on a line by itself if there is no else keyword.  Example:
 
     if (a == 5) {
         printf("a was 5.\n");