Is this str_rev() ok?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pete

    #16
    Re: Is this str_rev() ok?

    Skarmander wrote:[color=blue]
    >
    > Skarmander wrote:[color=green]
    > > pete wrote:[color=darkred]
    > >> Skarmander wrote:[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    > >>> In the unlikely case I have to implement strcpy()
    > >>> in standard C, I'd probably use
    > >>>
    > >>> while ((*dest++ = *source++) != '\0');
    > >>
    > >> An equality expression with three side effects,
    > >> is a little too busy for my taste.
    > >>
    > >> I'm more partial to:
    > >> do {
    > >> *dest = *source++;
    > >> } while (*dest++ != '\0');
    > >>[/color]
    > > I thought about this, but in the end,
    > > the urge to keep the symmetry of
    > > the ++ operators was just too strong to ignore.
    > >
    > > If I really wanted to split up all the side effects, of which I'm no
    > > great fan either, I'd go all the way:
    > >
    > > for (;;)
    > > *dest = *source*;[/color]
    >
    > Typo, obviously. Some newsreaders will helpfully *highlight* it. :-)[/color]

    I avoid break statements when it's not too hard to do.

    I also prefer

    for (rc = getc(fp); rc != EOF; rc = getc(fp))

    to

    while ((rc = getc(fp)) != EOF)

    I guess it's not necessarily the "three side effects"
    in an equality expression that I don't like.

    --
    pete

    Comment

    • Eric Sosman

      #17
      Re: Is this str_rev() ok?



      pete wrote On 06/27/06 16:35,:[color=blue]
      >
      > I also prefer
      >
      > for (rc = getc(fp); rc != EOF; rc = getc(fp))
      >
      > to
      >
      > while ((rc = getc(fp)) != EOF)[/color]

      There's no point arguing with Gus, but one reason
      to prefer the latter form is that you type the expression
      only once instead of twice. That's not too bad when the
      expression is a simple as this one, but even so it hands
      a human reader the job of verifying that the expressions
      are identical -- or, if they're not, whether they're
      intentionally or accidentally different:

      for (rc = getc(fp); rc != EOF; rc = fgetc(fp))

      for (rc = getc(conn.fp); rc != EOF; rc = getc(fp))

      for (rc = getc(fp); rc != EOF; r = getc(fp))

      --
      Eric.Sosman@sun .com

      Comment

      • Skarmander

        #18
        Re: Is this str_rev() ok?

        pete wrote:[color=blue]
        > Skarmander wrote:[color=green]
        >> Skarmander wrote:[color=darkred]
        >>> pete wrote:
        >>>> Skarmander wrote:[/color][/color]
        >[color=green][color=darkred]
        >>>>> In the unlikely case I have to implement strcpy()
        >>>>> in standard C, I'd probably use
        >>>>>
        >>>>> while ((*dest++ = *source++) != '\0');
        >>>> An equality expression with three side effects,
        >>>> is a little too busy for my taste.
        >>>>
        >>>> I'm more partial to:
        >>>> do {
        >>>> *dest = *source++;
        >>>> } while (*dest++ != '\0');
        >>>>
        >>> I thought about this, but in the end,
        >>> the urge to keep the symmetry of
        >>> the ++ operators was just too strong to ignore.
        >>>
        >>> If I really wanted to split up all the side effects, of which I'm no
        >>> great fan either, I'd go all the way:
        >>>
        >>> for (;;)
        >>> *dest = *source*;[/color]
        >> Typo, obviously. Some newsreaders will helpfully *highlight* it. :-)[/color]
        >
        > I avoid break statements when it's not too hard to do.
        >[/color]
        There's just no pleasing you.
        [color=blue]
        > I also prefer
        >
        > for (rc = getc(fp); rc != EOF; rc = getc(fp))
        >[/color]
        Oh no! A duplicated statement! *faints*

        Just kidding. But I don't imagine many programmers write this.
        [color=blue]
        > to
        >
        > while ((rc = getc(fp)) != EOF)
        >
        > I guess it's not necessarily the "three side effects"
        > in an equality expression that I don't like.
        >[/color]
        It doesn't win any beauty contests, but it's C. It wasn't meant to win
        beauty contests. C's maxim is "brevity is... wit", and in that respect it's
        a very witty language.

        S.

        Comment

        • pete

          #19
          Re: Is this str_rev() ok?

          Eric Sosman wrote:[color=blue]
          >
          > pete wrote On 06/27/06 16:35,:[color=green]
          > >
          > > I also prefer
          > >
          > > for (rc = getc(fp); rc != EOF; rc = getc(fp))
          > >
          > > to
          > >
          > > while ((rc = getc(fp)) != EOF)[/color]
          >
          > There's no point arguing with Gus, but one reason
          > to prefer the latter form is that you type the expression
          > only once instead of twice. That's not too bad when the
          > expression is a simple as this one, but even so it hands
          > a human reader the job of verifying that the expressions
          > are identical -- or, if they're not, whether they're
          > intentionally or accidentally different:
          >
          > for (rc = getc(fp); rc != EOF; rc = fgetc(fp))
          >
          > for (rc = getc(conn.fp); rc != EOF; rc = getc(fp))
          >
          > for (rc = getc(fp); rc != EOF; r = getc(fp))[/color]

          Good point!

          --
          pete

          Comment

          Working...