Loading ...
Sorry, an error occurred while loading the content.

Re: [XP] How to code an infinite loop (was: What do you thinkabout these two coding idioms?)

Expand Messages
  • Joshua Macy/Towers Perrin
    Why? Is it likely that C is ever going to change so that 1 fails to equate to boolean true? You might equally well argue that the evaluation of the empty
    Message 1 of 31 , Mar 1, 2001
    • 0 Attachment
      Why? Is it likely that C is ever going to change so that 1 fails to equate
      to boolean true? You might equally well argue that the evaluation of the
      empty middle clause in the for(;;) as true is an assumption. I find it hard
      to believe that someone reading C/C++ code is going to recognize for(;;) as
      being a loop forever construct but miss the fact that while(1) is the same.
      If you're going to learn to read someone else's C code, you're going to
      have to come to grips with the way the language defines truth, and to
      recognize both idioms.

      Personally, I think most such preferences depend more upon past experience
      and what you learned first than some sort of utility calculus or empirical
      measures of readability. Justifications commonly run along the lines "I
      have no trouble with X, but I worry that other people might, so I prefer
      Y."

      For the record, I prefer
      while (1)
      but that's because
      while 1:
      is a common Python idiom, and
      for(;;)
      cannot be expressed in Python.<wink>

      Joshua






      To: extremeprogramming@yahoogroups.com
      Dossy cc: (bcc: Joshua Macy/Towers Perrin)
      <dossy@panopt Subject: Re: [XP] How to code an infinite loop (was: What do you
      ic.com> thinkabout these two coding idioms?)

      02/28/2001
      08:27 PM
      Please
      respond to
      extremeprogra
      mming





      On 2001.02.28, Ryan King <rking@...> wrote:
      > Personally, I feel like "for (;;)" relies on a language oddity, and
      "while
      > (1)" is more elegant, but I'd have no problem whatsoever if a team voted
      > the other way.

      I find "for (;;)" preferrable because it doesn't rely on the numeric
      value "1" being equal to the boolean value "true" as "while (1)" does.
      And in C, there isn't a boolean type, so you can't really say
      "while (true)" and not be relying on the assumption.

      I think this is why lint-type programs favor the use of "for (;;)" over
      "while (1)" -- plus, as I mentioned, the former is less typing. ;-)

      - Dossy

      --
      Dossy Shiobara mail: dossy@...
      Panoptic Computer Network web: http://www.panoptic.com/

      To Post a message, send it to: extremeprogramming@...

      To Unsubscribe, send a blank message to:
      extremeprogramming-unsubscribe@...

      Ad-free courtesy of objectmentor.com

      Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    • azami@speakeasy.net
      ... lines I ... prefer ... Yes. I think someone is more likely to know that 1 evaluates to truth in C boolean expressions than they are to know that for(;;)
      Message 31 of 31 , Mar 1, 2001
      • 0 Attachment
        --- In extremeprogramming@y..., "Joshua Macy/Towers Perrin"
        > measures of readability. Justifications commonly run along the
        lines "I
        > have no trouble with X, but I worry that other people might, so I
        prefer
        > Y."

        Yes. I think someone is more likely to know that 1 evaluates to truth
        in C boolean expressions than they are to know that for(;;) loops
        infinitely. But if you use "TRUE" or "true" (define it as a const or
        macro if your language doesn't support it natively), your expression
        becomes very difficult to misunderstand - even for a programmer who's
        never seen C code before!
      Your message has been successfully submitted and would be delivered to recipients shortly.