From: Stefan Richter <stefanr@s5r6.in-berlin.de>
To: Jan Engelhardt <jengelh@linux01.gwdg.de>
Cc: Auke Kok <auke-jan.h.kok@intel.com>,
randy.dunlap@oracle.com,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Scott Preece <sepreece@gmail.com>
Subject: Re: [PATCH] [condingstyle] Add chapter on tests
Date: Sun, 27 May 2007 16:37:01 +0200 [thread overview]
Message-ID: <4659978D.6020802@s5r6.in-berlin.de> (raw)
In-Reply-To: <Pine.LNX.4.61.0705262213000.7344@yvahk01.tjqt.qr>
Jan Engelhardt wrote:
> + if (is_prime(number) == true)
> + return 0;
> + if (is_prime(number) == false)
> + return 1;
> +
> +should be:
> +
> + if (is_prime(number))
> + return 0;
> + if (!is_prime(number))
> + return 1;
> +
> +As far as pointers or functions returning an integer are concerned,
> +using long form tests helps to distinguish between pointers and bools
> +or functions returning boolean or integer, respectively.
> +Examples are:
> +
> + if (p == NULL)
> + return 1;
> + if (!p)
> + return 0;
> +
> + if (strcmp(haystack, needle) == 0)
> + return 1;
> + if (!strcmp(haystack, needle))
> + return 0;
The latter two examples seem odd. Didn't you mean the following?
if (p == NULL)
return 1;
if (p)
return 0;
if (strcmp(haystack, needle) == 0)
return 1;
if (strcmp(haystack, needle))
return 0;
Perhaps better:
if (p == NULL)
return NO_MEMORY;
if (p)
return MEMORY;
if (strcmp(haystack, needle) == 0)
return IS_SAME;
if (strcmp(haystack, needle))
return IS_DIFFERENT;
However, to follow your argument about non-boolean expressions, the
following would be more consequently going into your direction:
if (p == NULL)
return NO_MEMORY;
if (p != NULL)
return MEMORY;
if (strcmp(haystack, needle) == 0)
return IS_SAME;
if (strcmp(haystack, needle) != 0)
return IS_DIFFERENT;
I.e., why do the explicit comparison with 0 or NULL only when it is
tested for equality, but not when testing for inequality?
However, I agree with Scott Preece that these rules should be left out
of CodingStyle because they are contentious.
(Disclosure: I am personally used to "if (p)" and "if (!p)" tests of
pointers and many integer expressions, but I tend to the longer form in
less obvious cases like "if (strcmp(a, b) != 0)".)
--
Stefan Richter
-=====-=-=== -=-= ==-==
http://arcgraph.de/sr/
next prev parent reply other threads:[~2007-05-27 14:37 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-05-25 17:25 [PATCH 1/2] [CodingStyle] Add comment to not put spaces before tabs Auke Kok
2007-05-25 17:25 ` [PATCH 2/2] [condingstyle] Add chapter on tests Auke Kok
2007-05-25 17:34 ` Stefan Richter
2007-05-25 17:40 ` Kok, Auke
2007-05-26 19:27 ` Jan Engelhardt
2007-05-26 20:13 ` [PATCH] " Jan Engelhardt
2007-05-26 22:35 ` Scott Preece
2007-05-27 14:37 ` Stefan Richter [this message]
2007-05-27 15:02 ` Jan Engelhardt
2007-05-27 15:23 ` Stefan Richter
2007-05-27 15:52 ` Jan Engelhardt
2007-05-27 21:01 ` Guennadi Liakhovetski
2007-05-27 18:04 ` [PATCH 2/2] " Kok, Auke
2007-05-27 20:17 ` Pekka Enberg
2007-05-27 16:08 ` Jesper Juhl
2007-05-25 17:32 ` [PATCH 1/2] [CodingStyle] Add comment to not put spaces before tabs Stefan Richter
2007-05-25 19:43 ` H. Peter Anvin
2007-05-25 20:29 ` Kok, Auke
2007-05-25 21:26 ` H. Peter Anvin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4659978D.6020802@s5r6.in-berlin.de \
--to=stefanr@s5r6.in-berlin.de \
--cc=auke-jan.h.kok@intel.com \
--cc=jengelh@linux01.gwdg.de \
--cc=linux-kernel@vger.kernel.org \
--cc=randy.dunlap@oracle.com \
--cc=sepreece@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®