[gnome-devel-docs] programming-guidelines: Section on the switch statement



commit 458e0c592ce833576a2823e8c3b8e50ad6910160
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Aug 6 17:45:49 2013 +0200

    programming-guidelines: Section on the switch statement

 programming-guidelines/C/c-coding-style.page |  108 ++++++++++++++++++++++++++
 1 files changed, 108 insertions(+), 0 deletions(-)
---
diff --git a/programming-guidelines/C/c-coding-style.page b/programming-guidelines/C/c-coding-style.page
index 003d6e3..c0b2a0f 100644
--- a/programming-guidelines/C/c-coding-style.page
+++ b/programming-guidelines/C/c-coding-style.page
@@ -487,6 +487,114 @@ if ( condition )
     </code>
   </section>
 
+  <section id="switch">
+    <title>The switch statement</title>
+
+    <p>
+      A <code>switch ()</code> should open a block on a new
+      indentation level, and each <code>case</code> should start on
+      the same indentation level as the curly braces, with the case
+      block on a new indentation level:
+    </p>
+
+    <code>
+/* valid Linux kernel style */
+switch (condition) {
+case FOO:
+        do_foo ();
+        break;
+
+case BAR:
+        do_bar ();
+        break;
+}
+
+/* valid GNU style */
+switch (condition)
+  {
+  case FOO:
+    do_foo ();
+    break;
+
+  case BAR:
+    do_bar ();
+    break;
+  }
+
+/* invalid */
+switch (condition) {
+  case FOO: do_foo (); break;
+  case BAR: do_bar (); break;
+}
+
+/* invalid */
+switch (condition)
+  {
+  case FOO: do_foo ();
+    break;
+  case BAR: do_bar ();
+    break;
+  }
+
+/* invalid */
+switch (condition)
+  {
+    case FOO:
+    do_foo ();
+    break;
+    case BAR:
+    do_bar ();
+    break;
+  }
+    </code>
+
+    <p>
+      It is preferable, though not mandatory, to separate the various
+      cases with a newline:
+    </p>
+
+    <code>
+switch (condition) {
+case FOO:
+        do_foo ();
+        break;
+
+case BAR:
+        do_bar ();
+        break;
+
+default:
+        do_default ();
+}
+    </code>
+
+    <p>
+      The <code>break</code> statement for the default: case is not
+      mandatory.
+    </p>
+
+    <p>
+      If a <code>case</code> block needs to declare new variables, the same rules as the
+      inner blocks apply (see above); the <code>break</code> statement should be placed
+      outside of the inner block:
+    </p>
+
+    <code>
+/* valid GNU style */
+switch (condition)
+  {
+  case FOO:
+    {
+      int foo;
+
+      foo = do_foo ();
+    }
+    break;
+
+  ...
+  }
+    </code>
+  </section>
 
 
 </page>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]