Re: strlen(), K+1: clarification



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.
.



Relevant Pages

  • Re: Batch file and MFC (Properly Terminating Application)
    ... I never worked with MFC before and I wanted a response ... at it and then make corrections-that is, practice, practice and ... that processes user inputsuch as clicking of Exit Button. ... I give user a few seconds for each input via a while loop where I call ...
    (microsoft.public.vc.mfc)
  • Re: Perform Thru/Go to vs. Perform - Compile Speed
    ... various fields and possibilities but no necessity to loop or jump out of line. ... As a maintenance programmer by trade ... The people who never use THRU because disaster can happen if there's an extra label ... there are quite correct in their practice. ...
    (comp.lang.cobol)
  • Re: strlen(), K+1: clarification
    ... )> And if Clive used an invariant in a for loop, well, what's wrong with ... a for loop. ... It IS good practice. ... of the terminating value is automatically hoisted out of the loop ...
    (comp.programming)
  • Re: Determine if open(2) created or opened (again)
    ... Michael B Allen said: ... In practice the code might work for most cases, ... the exact race condition that makes this functionality impossible. ... goes through the second "if" structure within the while loop ...
    (comp.unix.programmer)
  • Re: strlen(), K+1: clarification
    ... )> And if Clive used an invariant in a for loop, well, what's wrong with ... a for loop. ... It IS good practice. ... You all think I'm paranoid, ...
    (comp.programming)

Loading