`Comparability' of code snippets (from book `Code complete')

Discussion in 'C Programming' started by lovecreatesbea...@gmail.com, Nov 18, 2007.

  1. Guest

    The book `Code complete' mentions similar code snippets as the
    followings , and talks about their advantages respectively.

    But I think these code snippets are totally different in logic, and
    their functionalities are different. They don't have comparabilities.
    Am I right?


    /*code 1*/
    for (i = 0; i < N; i++){
    if (cond)
    DoSomething();
    else
    DoOtherthing();
    }

    /*code 2*/
    if (cond){
    for (i = 0; i < N; i++)
    DoSomething();
    } else {
    for (i = 0; i < N; i++)
    DoOtherthing();
    }
    , Nov 18, 2007
    #1
    1. Advertising

  2. said:

    > The book `Code complete' mentions similar code snippets as the
    > followings , and talks about their advantages respectively.
    >
    > But I think these code snippets are totally different in logic, and
    > their functionalities are different. They don't have comparabilities.
    > Am I right?


    It depends on details that are not shown.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 18, 2007
    #2
    1. Advertising

  3. "" <> wrote in message
    news:...
    > The book `Code complete' mentions similar code snippets as the
    > followings , and talks about their advantages respectively.
    >
    > But I think these code snippets are totally different in logic, and
    > their functionalities are different. They don't have comparabilities.
    > Am I right?
    >
    >
    > /*code 1*/
    > for (i = 0; i < N; i++){
    > if (cond)
    > DoSomething();
    > else
    > DoOtherthing();
    > }
    >
    > /*code 2*/
    > if (cond){
    > for (i = 0; i < N; i++)
    > DoSomething();
    > } else {
    > for (i = 0; i < N; i++)
    > DoOtherthing();
    > }
    >

    Assuming no attempt to alter cond, i or N in the functions, they should have
    exactly the same effect.

    The question is which is more efficient, and which is better style.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Nov 18, 2007
    #3
  4. wrote:
    > The book `Code complete' mentions similar code snippets as the
    > followings , and talks about their advantages respectively.
    >
    > But I think these code snippets are totally different in logic, and
    > their functionalities are different. They don't have comparabilities.
    > Am I right?
    >
    >
    > /*code 1*/
    > for (i = 0; i < N; i++){
    > if (cond)
    > DoSomething();
    > else
    > DoOtherthing();
    > }
    >
    > /*code 2*/
    > if (cond){
    > for (i = 0; i < N; i++)
    > DoSomething();
    > } else {
    > for (i = 0; i < N; i++)
    > DoOtherthing();
    > }


    Provided neither DoSomething() nor DoNothing() alter the value of cond
    or i, they're identical, and a good optimising compiler should be able
    to generate the same machinecode.
    Mark McIntyre, Nov 18, 2007
    #4
  5. santosh Guest

    In article
    <>,
    <> wrote on
    Sunday 18 Nov 2007 3:31 pm:

    > The book `Code complete' mentions similar code snippets as the
    > followings , and talks about their advantages respectively.
    >
    > But I think these code snippets are totally different in logic, and
    > their functionalities are different. They don't have comparabilities.
    > Am I right?
    >
    >
    > /*code 1*/
    > for (i = 0; i < N; i++){
    > if (cond)
    > DoSomething();
    > else
    > DoOtherthing();
    > }
    >
    > /*code 2*/
    > if (cond){
    > for (i = 0; i < N; i++)
    > DoSomething();
    > } else {
    > for (i = 0; i < N; i++)
    > DoOtherthing();
    > }


    They have the same effect but only if 'i' and 'cond' are not altered by
    either of the functions. If they are going to be volatile the first
    form is the correct one.
    santosh, Nov 18, 2007
    #5
  6. santosh wrote:
    > In article
    > <>,
    > <> wrote on
    > Sunday 18 Nov 2007 3:31 pm:
    >
    >> The book `Code complete' mentions similar code snippets as the
    >> followings , and talks about their advantages respectively.
    >>
    >> But I think these code snippets are totally different in logic, and
    >> their functionalities are different. They don't have comparabilities.
    >> Am I right?
    >>
    >>
    >> /*code 1*/
    >> for (i = 0; i < N; i++){
    >> if (cond)
    >> DoSomething();
    >> else
    >> DoOtherthing();
    >> }
    >>
    >> /*code 2*/
    >> if (cond){
    >> for (i = 0; i < N; i++)
    >> DoSomething();
    >> } else {
    >> for (i = 0; i < N; i++)
    >> DoOtherthing();
    >> }

    >
    > They have the same effect but only if 'i' and 'cond' are not altered by
    > either of the functions. If they are going to be volatile the first
    > form is the correct one.


    It depends on whether we want to evaluate ``cond'' just once, or once
    for each iteration. We just don't have enough information to determine
    which one is correct.

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 18, 2007
    #6
  7. "Keith Thompson" <> wrote
    > It depends on whether we want to evaluate ``cond'' just once, or once for
    > each iteration. We just don't have enough information to determine which
    > one is correct.
    >

    if cond is in fact a macro that has side-effects.

    But then maybe "if" has been redefined by the preprocessor, if you're going
    to get like that.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Nov 18, 2007
    #7
  8. Tor Rustad Guest

    wrote:
    > The book `Code complete' mentions similar code snippets as the
    > followings , and talks about their advantages respectively.
    >
    > But I think these code snippets are totally different in logic, and
    > their functionalities are different. They don't have comparabilities.
    > Am I right?
    >
    >
    > /*code 1*/
    > for (i = 0; i < N; i++){
    > if (cond)
    > DoSomething();
    > else
    > DoOtherthing();
    > }
    >
    > /*code 2*/
    > if (cond){
    > for (i = 0; i < N; i++)
    > DoSomething();
    > } else {
    > for (i = 0; i < N; i++)
    > DoOtherthing();
    > }


    The answer depends on weather 'cond' is a loop-invariant, or not.

    The compiler can sometimes tell, and might then replace the first code
    snippet with the second, during the optimizing.

    --
    Tor < | tr i-za-h a-z>
    Tor Rustad, Nov 18, 2007
    #8
  9. Malcolm McLean wrote:
    > "Keith Thompson" <> wrote
    >> It depends on whether we want to evaluate ``cond'' just once, or once
    >> for each iteration. We just don't have enough information to
    >> determine which one is correct.
    >>

    > if cond is in fact a macro that has side-effects.
    >
    > But then maybe "if" has been redefined by the preprocessor, if you're
    > going to get like that.


    Get like what?

    You snipped part of the article to which I replied, in which santosh wrote:

    They have the same effect but only if 'i' and 'cond' are not
    altered by either of the functions. If they are going to be
    volatile the first form is the correct one.

    If "cond" is declared volatile, then the number of times it's evaluated
    obviously can be relevant. It needn't be a macro.

    For that matter, "cond" could be an abbreviation for the word "condition", and
    could refer (in pseudo-code) to any arbitrary expression used as a condition.
    It's not entirely clear whether the code was meant to be taken literally. But
    even if it was, the issue is volatility.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 18, 2007
    #9
  10. CBFalconer Guest

    Malcolm McLean wrote:
    > "Keith Thompson" <> wrote
    >
    >> It depends on whether we want to evaluate ``cond'' just once, or
    >> once for each iteration. We just don't have enough information
    >> to determine which one is correct.

    >
    > if cond is in fact a macro that has side-effects.
    >
    > But then maybe "if" has been redefined by the preprocessor, if
    > you're going to get like that.


    You can't redefine 'if' and have a legitimate program.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 19, 2007
    #10
  11. James Kuyper Guest

    CBFalconer wrote:
    > Malcolm McLean wrote:

    ....
    >> But then maybe "if" has been redefined by the preprocessor, if
    >> you're going to get like that.

    >
    > You can't redefine 'if' and have a legitimate program.


    What does "legitimate program" mean, in this context? I don't believe
    that redefining a keyword has undefined behavior, is a syntax error, or
    violates any constraints. It's a very bad idea, but that's a different
    question entirely.
    James Kuyper, Nov 19, 2007
    #11
  12. On Sun, 18 Nov 2007 02:01:21 -0800 (PST),
    "" <> wrote:

    >The book `Code complete' mentions similar code snippets as the
    >followings , and talks about their advantages respectively.
    >
    >But I think these code snippets are totally different in logic, and
    >their functionalities are different. They don't have comparabilities.
    >Am I right?
    >
    >
    >/*code 1*/
    >for (i = 0; i < N; i++){
    > if (cond)
    > DoSomething();
    > else
    > DoOtherthing();
    >}
    >
    >/*code 2*/
    >if (cond){
    > for (i = 0; i < N; i++)
    > DoSomething();
    >} else {
    > for (i = 0; i < N; i++)
    > DoOtherthing();
    >}


    When cond is invariant while the loop is iterating (always true or
    always false), they appear to be equivalent. Can you describe a
    situation where they do not perform the same?

    When cond changes over the course of the loop, they are very obviously
    different. code 1 executes DoSomething or DoOtherthing depending on
    the *current* state of cond while code2 executes the same function
    (either DoSomething or DoOtherthing) for every iteration depending
    only on the *initial* state of cond.




    Remove del for email
    Barry Schwarz, Nov 19, 2007
    #12
  13. CBFalconer Guest

    James Kuyper wrote:
    > CBFalconer wrote:
    >> Malcolm McLean wrote:

    > ...
    >>> But then maybe "if" has been redefined by the preprocessor, if
    >>> you're going to get like that.

    >>
    >> You can't redefine 'if' and have a legitimate program.

    >
    > What does "legitimate program" mean, in this context? I don't
    > believe that redefining a keyword has undefined behavior, is a
    > syntax error, or violates any constraints. It's a very bad idea,
    > but that's a different question entirely.


    I had to search a while. From N869:

    6.4.1 Keywords

    Syntax
    [#1]
    keyword: one of
    auto enum restrict unsigned
    break extern return void
    case float short volatile
    char for signed while
    const goto sizeof _Bool
    continue if static _Complex
    default inline struct _Imaginary
    do int switch
    double long typedef
    else register union

    Semantics

    [#2] The above tokens (case sensitive) are reserved (in
    translation phases 7 and 8) for use as keywords, and shall
    not be used otherwise.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 20, 2007
    #13
  14. James Kuyper Guest

    CBFalconer wrote:
    > James Kuyper wrote:
    >> CBFalconer wrote:
    >>> Malcolm McLean wrote:

    >> ...
    >>>> But then maybe "if" has been redefined by the preprocessor, if
    >>>> you're going to get like that.
    >>> You can't redefine 'if' and have a legitimate program.

    >> What does "legitimate program" mean, in this context? I don't
    >> believe that redefining a keyword has undefined behavior, is a
    >> syntax error, or violates any constraints. It's a very bad idea,
    >> but that's a different question entirely.

    >
    > I had to search a while. From N869:
    >
    > 6.4.1 Keywords
    >
    > Syntax
    > [#1]
    > keyword: one of
    > auto enum restrict unsigned
    > break extern return void
    > case float short volatile
    > char for signed while
    > const goto sizeof _Bool
    > continue if static _Complex
    > default inline struct _Imaginary
    > do int switch
    > double long typedef
    > else register union
    >
    > Semantics
    >
    > [#2] The above tokens (case sensitive) are reserved (in
    > translation phases 7 and 8) for use as keywords, and shall
    > not be used otherwise.


    My apologies - I should have performed the search, not you. But I was
    pretty certain there was nothing to find, which tends to remove the
    motivation to look.
    James Kuyper, Nov 20, 2007
    #14
  15. Flash Gordon Guest

    James Kuyper wrote, On 20/11/07 05:14:
    > CBFalconer wrote:
    >> James Kuyper wrote:
    >>> CBFalconer wrote:
    >>>> Malcolm McLean wrote:
    >>> ...
    >>>>> But then maybe "if" has been redefined by the preprocessor, if
    >>>>> you're going to get like that.
    >>>> You can't redefine 'if' and have a legitimate program.
    >>> What does "legitimate program" mean, in this context? I don't
    >>> believe that redefining a keyword has undefined behavior, is a
    >>> syntax error, or violates any constraints. It's a very bad idea,
    >>> but that's a different question entirely.

    >>
    >> I had to search a while. From N869:
    >>
    >> 6.4.1 Keywords
    >>
    >> Syntax
    >> [#1]
    >> keyword: one of
    >> auto enum restrict unsigned


    <snip>

    >> Semantics
    >>
    >> [#2] The above tokens (case sensitive) are reserved (in
    >> translation phases 7 and 8) for use as keywords, and shall
    >> not be used otherwise.

    >
    > My apologies - I should have performed the search, not you. But I was
    > pretty certain there was nothing to find, which tends to remove the
    > motivation to look.


    Isn't the #define and replacement handled in one of the earlier
    translation phases making that section irrelevant as far as whether you
    can do a #define of if?
    --
    Flash Gordon
    Flash Gordon, Nov 20, 2007
    #15
    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. - Steve -

    Reusing Code Snippets

    - Steve -, Sep 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    416
    Greg Burns
    Oct 1, 2004
  2. Pat

    Where are my Code Snippets?

    Pat, Apr 20, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    388
    Simon Harris
    Apr 23, 2005
  3. Steve Franks
    Replies:
    2
    Views:
    353
    Juan T. Llibre
    Sep 11, 2005
  4. Davy
    Replies:
    73
    Views:
    2,374
    Tom Lucas
    Aug 21, 2006
  5. steve
    Replies:
    3
    Views:
    664
    Giovanni Azua
    Mar 11, 2009
Loading...

Share This Page