Skip to contentSkip to conversation

clarification of coding style regarding conditional statements with two branches

PATCH In review Oliver Neukum 4 participants 6 messages May 4, 2007 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clarification of coding style regarding conditional statements with two branches

Hi,

I suggest that the coding style should state that if either branch of an 'if' statement needs braces, both branches should use them.

Regards
Oliver

----

Documentation/CodingStyle +15 -0
@@ -160,6 +160,21 @@
 25-line terminal screens here), you have more empty lines to put
 comments on.
 
+Do not unnecessarily use braces where a single statement will do.
+
+if (condition)
+	action();
+
+This does not apply if one branch of a conditional statement is a single
+statement. Use braces in both branches.
+
+if (condition) {
+	do_this();
+	do_that();
+} else {
+	otherwise();
+}
+
 		3.1:  Spaces
 
 Linux kernel style for use of spaces depends (mostly) on
#1 lore raw

Oliver Neukum napsal(a):

Quoted 13 lines

Hi,

I suggest that the coding style should state that if either branch of an 'if' statement needs braces, both branches should use them.

Regards
Oliver

----

Documentation/CodingStyle line 169

Quoted 10 lines
Documentation/CodingStyle +7 -0
@@ -160,6 +160,21 @@
 25-line terminal screens here), you have more empty lines to put
 comments on.
 
+Do not unnecessarily use braces where a single statement will do.
+
+if (condition)
+	action();
+
+This does not apply if one branch of a conditional statement is a single
+statement. Use braces in both branches.

Why, what's wrong with

if (condition) {
	do_this();
	do_that();
} else
	otherwise();

? It's more readable/nicer in my eyes than

Documentation/CodingStyle line 177

Quoted 7 lines
Documentation/CodingStyle +7 -0
+if (condition) {
+	do_this();
+	do_that();
+} else {
+	otherwise();
+}
+

and not only in mine according to fast grep.

regards,

Signature · 4 lines
http://www.fi.muni.cz/~xslaby/            Jiri Slaby
faculty of informatics, masaryk university, brno, cz
e-mail: jirislaby gmail com, gpg pubkey fingerprint:
B674 9967 0407 CE62 ACC8  22A0 32CC 55C3 39D4 7A7E
#2 lore raw

Am Freitag, 4. Mai 2007 16:48 schrieb Jiri Slaby:

Quoted 8 lines

Why, what's wrong with
if (condition) {
        do_this();
        do_that();
} else
        otherwise();

? It's more readable/nicer in my eyes than

Than I am afraid we need to agree to disagree. But then I would insist that neither side give the other a hard to time over this issue.

Regards
Oliver

#3 lore raw

Oliver Neukum napsal(a):

Quoted 8 lines

Am Freitag, 4. Mai 2007 16:48 schrieb Jiri Slaby:

Quoted 7 lines

Why, what's wrong with

if (condition) {
        do_this();
        do_that();
} else
        otherwise();
Quoted 4 lines
Quoted 1 line

? It's more readable/nicer in my eyes than

Than I am afraid we need to agree to disagree. But then I would insist that neither side give the other a hard to time over this issue.

Then I would just let it undefined to let subsys maintainers (or coders themselves) to decide what they want in their subtree like ton of other things...

regards,

Signature · 4 lines
http://www.fi.muni.cz/~xslaby/            Jiri Slaby
faculty of informatics, masaryk university, brno, cz
e-mail: jirislaby gmail com, gpg pubkey fingerprint:
B674 9967 0407 CE62 ACC8  22A0 32CC 55C3 39D4 7A7E
#4 lore raw
In reply to Jiri Slaby
Quoted 8 lines · Jiri Slaby

Why, what's wrong with

if (condition) {
	do_this();
	do_that();
} else
	otherwise();

? It's more readable/nicer in my eyes than

I think so. And it means less lines #.

Quoted 6 lines

Documentation/CodingStyle line 176

Quoted 6 lines
Documentation/CodingStyle +6 -0
+if (condition) {
+	do_this();
+	do_that();
+} else {
+	otherwise();
+}

Exception:

if (x) {
	if (y)
		foo1();
	else
		foo2();
} else
	bar();

The braces after if(x) are needed so nobody thinks it's:

if (x)

	if (y)
		foo1();
else
	foo2();
else
	bar();
Signature · 1 line
Krzysztof Halasa
#5 lore raw
In reply to Jiri Slaby
Quoted 10 lines · Jiri Slaby

Documentation/CodingStyle line 169

Quoted 8 lines
Documentation/CodingStyle +7 -0
+Do not unnecessarily use braces where a single statement will do.
+
+if (condition)
+	action();
+
+This does not apply if one branch of a conditional statement is a single
+statement. Use braces in both branches.

Why, what's wrong with

Quoted 7 lines
if (condition) {
	do_this();
	do_that();
} else
	otherwise();

? It's more readable/nicer in my eyes.

I'll just point to an opinion:
http://lkml.org/lkml/2006/9/5/36 , lower half. ("Secondly, whenever...")

Jan