Nested Statements.

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

    #1

    Nested Statements.

    Hi,

    Nested statements helps to generate complex source code,
    what is the nesting limit that you normally consider?

    I know that the Halsted and McCabe analyses can define the acceptable
    level, but in your personnel point of views what is the limit before you
    re-code the block into multiple blocks

    a 3 level nesting example

    If (z is true)
    while (x < Y )
    begin
    if (z==x)
    ......
    else
    .......
    end if

    end
    end
    else

    while (x < Y )
    begin
    if (z==x)
    ......
    else
    .......
    end if

    end
    end

    end if


    I think that 3 level sometimes 4 is acceptable.
    but I was wondering would you restrict it to 3 or 4?

    is there anyway to avoid this excluding the creation of new functions?

    Thanks

  • Malcolm

    #2
    Re: Nested Statements.


    "Profetas" <xuxu_18@yahoo. com> wrote[color=blue]
    >
    > Nested statements helps to generate complex source code,
    > what is the nesting limit that you normally consider?
    >[/color]
    Ideally about three levels is the maximum that a human can easily read, but
    in practise it isn't possible to achieve this[color=blue]
    >
    > is there anyway to avoid this excluding the creation of new functions?
    >[/color]
    No always. Even creating a new function is often not the answer. A function
    should do something which is discrete and can be summarised in a short
    comment. Often complex flow logic is to handle errors or similar conditions
    and it is not possible to break it out into a function.
    You can use goto. This is a terrible solution when badly used, but
    acceptable when used carefully. eg

    MYLEVEL *createlevel(ch ar *filename)
    {
    MYLEVEL *answer = 0;
    FILE *fp = fopen("filename ", "rb");
    if(!fp)
    goto error;

    answer = malloc(sizeof(M YLEVEL));
    if(!answer)
    goto error;

    answer->cell = 0;
    /* etc */

    answer->cell = malloc(width * height);
    if(!answer->cell)
    goto error;

    etc etc etc;

    return answer;

    error:
    /*
    code to free up a partially-allocated level
    */
    return NULL;
    }


    Comment

    • CBFalconer

      #3
      Re: Nested Statements.

      Profetas wrote:[color=blue]
      >
      > Nested statements helps to generate complex source code,
      > what is the nesting limit that you normally consider?
      >
      > I know that the Halsted and McCabe analyses can define the
      > acceptable level, but in your personnel point of views what is the
      > limit before you re-code the block into multiple blocks
      >
      > a 3 level nesting example
      >
      > If (z is true)
      > while (x < Y )
      > begin
      > if (z==x)
      > ......
      > else
      > .......
      > end if
      >
      > end
      > end
      > else
      >
      > while (x < Y )
      > begin
      > if (z==x)
      > ......
      > else
      > .......
      > end if
      >
      > end
      > end
      >
      > end if
      >
      > I think that 3 level sometimes 4 is acceptable.
      > but I was wondering would you restrict it to 3 or 4?
      >
      > is there anyway to avoid this excluding the creation of new
      > functions?[/color]

      First, take your own example and avoid creating non-existent
      levels. The 'begin's (which are '{' in C) are not a level of
      control. Then limit the line length to 72 chars or so. You should
      also use the rule of 7, which says that a function should deal with
      no more than 7 items at once, because the human brain becomes
      confused with more.

      Rewriting your example into reasonable (IMO) format:

      If (z is true) {
      while (x < Y) {
      if (z == x) ......
      else .......
      }
      }
      else {
      while (x < Y) {
      if (z == x) {
      ......
      }
      else {
      .......
      }
      }
      }

      Which smells (to me) as if misconstructed. I suspect the .....
      areas should have been parametized in some form, so that the
      overall code should be simplified to:

      if (z) operateon(x, y, zz);
      else operateon(z, y, z);

      --
      Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
      Available for consulting/temporary embedded and systems.
      <http://cbfalconer.home .att.net> USE worldnet address!

      Comment

      • Peter Pichler

        #4
        Re: Nested Statements.

        "Profetas" <xuxu_18@yahoo. com> wrote:[color=blue]
        > Nested statements helps to generate complex source code,
        > what is the nesting limit that you normally consider?[/color]

        Infinity :) Seriously, my limit would be about 20, i.e. when the indentation
        reaches the middle of the screen.
        [color=blue]
        > a 3 level nesting example
        >
        > If (z is true)
        > while (x < Y )
        > begin
        > if (z==x)
        > ......
        > else
        > .......
        > end if
        > end
        > end
        > else[/color]

        What has the above to do with C?
        [color=blue]
        > I think that 3 level sometimes 4 is acceptable.
        > but I was wondering would you restrict it to 3 or 4?[/color]

        No.
        [color=blue]
        > is there anyway to avoid this excluding the creation of new functions?[/color]

        Yes, there is. But why? (One way could be using a lot of gotos. But then
        again, why?)

        Peter


        Comment

        • Mark McIntyre

          #5
          Re: Nested Statements.

          On Sat, 6 Nov 2004 23:09:04 -0000, in comp.lang.c , "Peter Pichler"
          <pichlo7@pobox. sk> wrote:
          [color=blue]
          >"Profetas" <xuxu_18@yahoo. com> wrote:[color=green]
          >> Nested statements helps to generate complex source code,
          >> what is the nesting limit that you normally consider?[/color]
          >
          >Infinity :)[/color]

          I'd suggest 127 would be a safer limit...
          5.2.4.1 Translation limits
          1 The implementation shall be able to translate and execute at least one
          program that contains at least one instance of every one of the following
          limits:
          - 127 nesting levels of blocks


          --
          Mark McIntyre
          CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
          CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

          ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
          http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
          ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

          Comment

          Working...