Re: Newbie-Side effects?

Discussion in 'C Programming' started by Glen Herrmannsfeldt, Jun 24, 2003.

  1. "Richard Bos" <> wrote in message
    news:...
    > (prashna) wrote:
    >
    > > I am new to C and I am in a code review project( the review has to
    > > be done with respect to some predefined rules)and I came across rule
    > > which states "The right hand operand of a && or || operator shall not
    > > contain side effects.".Please let me know what are these side effects?

    >
    > Joona has already explained what a side effect is; let me add a remark
    > about this rule.
    >
    > It stinks.
    >
    > It stinks to high heaven, because it makes some useful constructs
    > impossible for, AFAICT, no good reason at all. For example, it would
    > disallow this line:
    >
    > while (--lim>0 && (c=getchar())!=EOF && c!='\n')
    > s[i++]=c;


    Not commenting on the example at all, the restriction makes some sense.
    Because the side effects are conditional on the left hand operand, it can
    sometimes be unclear that the side effect happens. Now, consider a rule
    against right hand operand side effects that could be unclear to the reader.
    (Hint: I just watched Nightline about the supreme court affirmative action
    ruling.)

    Yes, I think the rule is too strict, but consider the kind of code one could
    write that violates it? Many have probably been submitted to the IOCCC.
    OK, challenge: rewrite the above code satisfying the restriction, and
    significantly easier to understand!

    -- glen
     
    Glen Herrmannsfeldt, Jun 24, 2003
    #1
    1. Advertising

  2. Glen Herrmannsfeldt

    CBFalconer Guest

    Glen Herrmannsfeldt wrote:
    > "Richard Bos" <> wrote in message
    > > (prashna) wrote:
    > >
    > > > I am new to C and I am in a code review project( the review
    > > > has to be done with respect to some predefined rules) and I
    > > > came across rule which states "The right hand operand of a
    > > > && or || operator shall not contain side effects.". Please
    > > > let me know what are these side effects?

    > >
    > > Joona has already explained what a side effect is; let me add
    > > a remark about this rule.
    > >
    > > It stinks.
    > >
    > > It stinks to high heaven, because it makes some useful
    > > constructs impossible for, AFAICT, no good reason at all. For
    > > example, it would disallow this line:
    > >
    > > while (--lim>0 && (c=getchar())!=EOF && c!='\n')
    > > s[i++]=c;

    >
    > Not commenting on the example at all, the restriction makes some
    > sense. Because the side effects are conditional on the left hand
    > operand, it can sometimes be unclear that the side effect happens.
    > Now, consider a rule against right hand operand side effects that
    > could be unclear to the reader. (Hint: I just watched Nightline
    > about the supreme court affirmative action ruling.)
    >
    > Yes, I think the rule is too strict, but consider the kind of
    > code one could write that violates it? Many have probably been
    > submitted to the IOCCC. OK, challenge: rewrite the above code
    > satisfying the restriction and significantly easier to understand!


    while (0 < --lim)
    if ( (EOF == (c = getchar()) ||
    ('\n' == c) ) break;
    else s[i++] = c;

    Note that without knowledge of other uses for lim and i we must
    maintain their entry/exit conditions. Similarly the state of the
    stdin stream.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Jun 24, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Bancroft
    Replies:
    2
    Views:
    2,414
    =?Utf-8?B?UmFodWwgQW5hbmQ=?=
    Dec 28, 2004
  2. Anonieko
    Replies:
    0
    Views:
    1,945
    Anonieko
    Aug 12, 2005
  3. Giuseppe

    Re: Newbie-Side effects?

    Giuseppe, Jun 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,163
    Giuseppe
    Jun 24, 2003
  4. Ben Pfaff

    Re: Newbie-Side effects?

    Ben Pfaff, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    766
    Ben Pfaff
    Jun 26, 2003
  5. prashna

    Re: Newbie-Side effects?Another rule

    prashna, Jun 27, 2003, in forum: C Programming
    Replies:
    1
    Views:
    976
    Joona I Palaste
    Jun 27, 2003
Loading...

Share This Page