Source-Changes archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: CVS commit: src/distrib/utils/sysinst
In article <yov5smp3ak37.fsf%ldt-sj3-010.sj.broadcom.com@localhost>,
<cgd%broadcom.com@localhost> wrote:
>At Fri, 18 Jul 2003 15:28:07 +0000 (UTC), "John Hawkinson" wrote:
>> Perry E. Metzger <perry%piermont.com@localhost> wrote on Fri, 18 Jul 2003
>> at 10:21:31 -0400 in <87n0fbx6dw.fsf%snark.piermont.com@localhost>:
>>
>> > David Laight <dsl%netbsd.org@localhost> writes:
>> > > Change a lot of 'if (strlen(x) == 0)' to 'if (x[0] === 0)'.
>> >
>> > Why bother? Was this performance critical code? The former is clearer
>> > to the reader.
>>
>> I do not think you can seriously argue that the clarity is stronger
>> either way.
>
>I'd disagree: the former includes some semantic meaning that the
>latter does not.
So what? Do you know what the programmer's intention was? Maybe he
wanted to see if the string was the empty string, in which case:
strcmp(x, "") == 0
would be more appropriate :-)
>
>the former says "check the length of the string 'x' against 0".
>
>the latter says "does the first byte of the memory pointed to by 'x'
>happen to be 0?"
And it will take a moron a mental leap to go from:
the first byte of the string is nul,
strings are nul terminated,
this is a check for the empty string.
>You might do the latter for many reasons, and a true newbie might not
>even understand that the latter has the effect of the former.
I would hope that a newbie would be only reading that code, not writing it.
>in terms of efficiency, i'd be a little surprised if current GCC
>development sources (with builtins allowed and optimization on)
>wouldn't transform the strlen(x) == 0 into the NUL cehck.
I would think that converting the strcmp(x, "") would be easier to optimize.
christos
Home |
Main Index |
Thread Index |
Old Index