Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 7 Mar 2002 02:16:30 -0500
From:      Brian T.Schellenberger <bts@babbleon.org>
To:        Poul-Henning Kamp <phk@critter.freebsd.dk>, obrien@FreeBSD.ORG
Cc:        Garance A Drosihn <drosih@rpi.edu>, Mike Meyer <mwm-dated-1015843484.1eabc5@mired.org>, hackers@FreeBSD.ORG
Subject:   Re: RFC: style(9) isn't explicit about booleans for testing -- an actual analysis of the code!
Message-ID:  <20020307071630.E26DBBA03@i8k.babbleon.org>
In-Reply-To: <52204.1015480748@critter.freebsd.dk>
References:  <52204.1015480748@critter.freebsd.dk>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thursday 07 March 2002 12:59 am, Poul-Henning Kamp wrote:
| In message <20020306192234.B55297@dragon.nuxi.com>, "David O'Brien" writes:
| >Implies???  I thought I was quite explicit:
| >
| >    to prevent is "if (!strcmp(a,b))" which when read is extremely wrong
| > of that is actually happening.
| >
| >! is pronounced "NOT".  When read "if not string compare a with b then do
| > X", is the opposite of the the logic of the expression does.  Which is
| > "if string compare a with b is equal then do X".  ["if (strcmp(a,b) ==
| > 0)"]
|
| Well, we're clearly into "IMO" land here, so lets ignore that :-)

No, we aren't, and let's not.

Maybe your brain has gotten used to it, but to us ordinary mortals, even us 
ordinary mortals who've been slogging C code for time periods that can be 
measured in decades  (yikes!), it is very tempting to read

  i f (!strcmp(a,b,l))

as "if the strings don't compare" which , in ordinary usage, means don't 
compare *equal*.  But of course the C program is going to proceed to do 
exactly the opposite of that.

Now, I personally find the constructions

   if (!p)  do_thing_for_null_pointer;

and 

   if (p)  do_thing_for_valid_pointer;

quite readable and would prefer that an abstract Guide To Perfect Style 
blessed them since I find them quite readable as "if I don't have a pointer" 
and "if I do have a pointer".

But  *ALL* of this is beside the nominal point ANYWAY, which is to discuss 
the proper wording for the man(9) style guide which is supposed to document 
how things things are actually done in the kernel, not personal preference.

===================================================================

 So I just grepped the code

(find /usr/src -type f -exec grep 'if.*strcmp' {} \;)

and analyize the results.

Overall, there are 1831 !strcmp test and 3363 strcmp.*==0 tests

Within the kernel there are 84 !strcmp tests and 155 strcmp.*== 0 tests

So 65% either way in favor of the proposed change in wording -- a 2:1 
majority of the code votes FOR the proposed change.

(This is subject to minor error given that it's all based on greps and 
statistics, but I did the analysis a few different ways using different 
plausible regular expressions to determine the two code paths, and all  of 
analysis showed that it was at least 2:1 using the == / != 0 tests.)


-- 
Brian T. Schellenberger . . . . . . .   bts@wnt.sas.com (work)
Brian, the man from Babble-On . . . .   bts@babbleon.org (personal)
                                ME -->  http://www.babbleon.org
http://www.eff.org   <-- GOOD GUYS -->  http://www.programming-freedom.org 

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020307071630.E26DBBA03>