Re: strlen(), K+1: clarification
- From: cri@xxxxxxxx (Richard Harter)
- Date: Tue, 26 Feb 2008 16:39:20 GMT
On Tue, 26 Feb 2008 09:00:36 +0000 (UTC), Willem
<willem@xxxxxxxx> wrote:
spinoza1111 wrote:
) On Feb 26, 3:28 am, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
)> And if Clive used an invariant in a for loop, well, what's wrong with
)> that? That's good practice.
)
) ...when Clive, another thug, uses it. In 2003, I posted code with
) strlen in the for condition, and was stalked for using an invariant in
) a for loop.
)
) It IS good practice.
All good practices have their exceptions.
One of the exceptions to this good practice is strlen().
This is because strlen() is O(N).
Just accept that and move on.
Just as a small discordant note to this otherwise excessively
edifying discussion, in cases like this it often is useful to put
the terminating value in the initialization and count down to
zero. Thus, if it doesn't matter which way the loop index runs,
one can say:
for(i = strlen(S); i-- > 0;) {...} /* Counting down */
instead of
for(i = 0; i<strlen(S); i++) {...} /* Counting up */
Of course if you must access the bytes of S sequentially
increasing you can write
for(i = strlen(S),j=0; i-- > 0; j++) {...} /* or */
for(i = strlen(S),p=S; i-- > 0; p++) {...}
This little bit of ecentricity has its points. The computation
of the terminating value is automatically hoisted out of the loop
body. The new terminating condition is a comparison with 0,
usually a more efficient test than a comparison against a
non-zero value.
As a sociological and psychological note, I opine that it is good
practice to preferentially write descent loops rather than ascent
loops. Sometimes one is preferable, sometimes the other, and
often it doesn't matter. However the cultural reinforcement for
counting upwards is so pervasive that people forget that they can
count either way, and, when they do remember, they find the
process awkward and fraught with unfamiliarity. Preferentially
using the unfamiliar form makes for a certain limberness of
thought.
Richard Harter, cri@xxxxxxxx
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
.
- Follow-Ups:
- Re: strlen(), K+1: clarification
- From: Clive D. W. Feather
- Re: strlen(), K+1: clarification
- From: spinoza1111
- Re: strlen(), K+1: clarification
- From: Willem
- Re: strlen(), K+1: clarification
- References:
- strlen(), K+1: clarification
- From: spinoza1111
- Re: strlen(), K+1: clarification
- From: Ben Bacarisse
- Re: strlen(), K+1: clarification
- From: spinoza1111
- Re: strlen(), K+1: clarification
- From: Willem
- Re: strlen(), K+1: clarification
- From: Randy Howard
- Re: strlen(), K+1: clarification
- From: Willem
- Re: strlen(), K+1: clarification
- From: spinoza1111
- Re: strlen(), K+1: clarification
- From: Stephen Howe
- Re: strlen(), K+1: clarification
- From: Richard Heathfield
- Re: strlen(), K+1: clarification
- From: spinoza1111
- Re: strlen(), K+1: clarification
- From: Willem
- strlen(), K+1: clarification
- Prev by Date: Re: Working as programmer with Bachelor's degree
- Next by Date: Re: strlen(), K+1: clarification
- Previous by thread: Re: strlen(), K+1: clarification
- Next by thread: Re: strlen(), K+1: clarification
- Index(es):
Relevant Pages
|
Loading