Skip to contentSkip to conversation

Minor coding style fix

PATCH In review Aneesh Kumar K.V 4 participants 5 messages Oct 8, 2006 – Oct 9, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Minor coding style fix

Kernel generally follow the style

if (func()) {
/* failed case */
} else {
/* success */
}

-aneesh

kernel/sys.c +1 -1
index 98489d8..55cb77c 100644
@@ -517,7 +517,7 @@ EXPORT_SYMBOL_GPL(srcu_notifier_call_cha
 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
 {
 	mutex_init(&nh->mutex);
-	if (init_srcu_struct(&nh->srcu) < 0)
+	if (init_srcu_struct(&nh->srcu))
 		BUG();
 	nh->head = NULL;
 }
#1 lore raw
Quoted 9 lines · Aneesh Kumar K.V

Kernel generally follow the style

if (func()) {
/* failed case */
} else {
/* success */
}

Please submit patches inline, having to copy them from attachments to be able to reply is a pain.

Quoted 6 lines
kernel/sys.c +0 -0
index 98489d8..55cb77c 100644

kernel/sys.c EXPORT_SYMBOL_GPL(srcu_notifier_call_cha · line 523

Quoted 8 lines
kernel/sys.c +1 -1
@@ -517,7 +517,7 @@ EXPORT_SYMBOL_GPL(srcu_notifier_call_cha
 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
 {
 	mutex_init(&nh->mutex);
-	if (init_srcu_struct(&nh->srcu) < 0)
+	if (init_srcu_struct(&nh->srcu))
 		BUG();
 	nh->head = NULL;
 }

I really liked the old code better. If in the future init_srcu_struct() is changed to also return >0 for some conditions, then that would not previously have triggered BUG(), but after your changes it will. The code, as it were, perfectly expressed what it wanted to happen - if it returns less than zero it's a BUG(). I say leave it alone.

Signature · 3 lines
Jesper Juhl <[email protected]>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html
#2 lore raw
Quoted 7 lines
Quoted 7 lines

Kernel generally follow the style

if (func()) {
/* failed case */
} else {
/* success */
}

Here's my: NAK.
(At best it should be if(foo != 0) rather than if(foo), but that's just me.)

Quoted 6 lines

I really liked the old code better. If in the future init_srcu_struct() is changed to also return >0 for some conditions, then that would not previously have triggered BUG(), but after your changes it will. The code, as it were, perfectly expressed what it wanted to happen - if it returns less than zero it's a BUG(). I say leave it alone.

I agree here.

	-`J'
#3 lore raw
In reply to Jesper Juhl
Quoted 27 lines · Jesper Juhl
Quoted 9 lines · Aneesh Kumar K.V

Kernel generally follow the style

if (func()) {
/* failed case */
} else {
/* success */
}

Please submit patches inline, having to copy them from attachments to be able to reply is a pain.

Quoted 13 lines
kernel/sys.c +1 -1
index 98489d8..55cb77c 100644
@@ -517,7 +517,7 @@ EXPORT_SYMBOL_GPL(srcu_notifier_call_cha
 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
 {
     mutex_init(&nh->mutex);
-    if (init_srcu_struct(&nh->srcu) < 0)
+    if (init_srcu_struct(&nh->srcu))
         BUG();
     nh->head = NULL;
Quoted 10 lines

kernel/sys.c EXPORT_SYMBOL_GPL(srcu_notifier_call_cha · line 523

Quoted 1 line
kernel/sys.c +0 -0
 }

I really liked the old code better. If in the future init_srcu_struct() is changed to also return >0 for some conditions, then that would not previously have triggered BUG(), but after your changes it will. The code, as it were, perfectly expressed what it wanted to happen - if it returns less than zero it's a BUG(). I say leave it alone.

As per Documentation/CodingStyle

"Functions can return values of many different kinds, and one of the most common is a value indicating whether the function succeeded or failed. Such a value can be represented as an error-code integer (-Exxx = failure, 0 = success) or a "succeeded" boolean (0 = failure, non-zero = success)."

That means if the function need to indicate success it should be made to return 0. I don't see any other value being returned from init_srcu_struct. Also having a consistent style of if() check make code reading easier.

-aneesh

#4 lore raw
Quoted 10 lines · Aneesh Kumar K.V

As per Documentation/CodingStyle

"Functions can return values of many different kinds, and one of the most common is a value indicating whether the function succeeded or failed. Such a value can be represented as an error-code integer (-Exxx = failure, 0 = success) or a "succeeded" boolean (0 = failure, non-zero = success)."

That means if the function need to indicate success it should be made to return 0.

The wording is 'can', not 'should' or 'shall'. The current agreement is however that do_something()-named functions indeed 'should' return <0 for "failure" and 0 for "success" while is_something()-named functions 'should' return non-0 for "yes" and 0 for "no". But there is no rule without exceptions: Some functions like copy_to_user() return >0 in situations which can be considered a "failure" because this positive value has further meaning.

But back to your patch: I am not aware of an agreement on how to write a check for zero or a check for nonzero.

Quoted 1 line

I don't see any other value being returned from init_srcu_struct.

True.

Quoted 1 line

Also having a consistent style of if() check make code reading easier.

Also true. However (a) there is no kernel-wide consistency about this and (b) the style used in the file which you are patching is
if (do_something_returning_negative_errors() < 0)
E.g.
http://www.linux-m32r.org/lxr/http/source/kernel/sys.c?v=2.6.19-rc1#L1070

And here <0 and !0 are actually different:
http://www.linux-m32r.org/lxr/http/source/kernel/sys.c?v=2.6.19-rc1#L852

However, kernel/sys.c is not entirely consequent:
http://www.linux-m32r.org/lxr/http/source/kernel/sys.c?v=2.6.19-rc1#L1336
and the other calls to copy_{from,to}_user would have to be

	if (copy_to_user(a, b, s) != 0)

or > 0 to follow the style of the above mentioned ifs to 100%. But that's nitpicking. :-)

Signature · 3 lines
Stefan Richter
-=====-=-==- =-=- -=---
http://arcgraph.de/sr/