Not a very good Idea!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vashwath@rediffmail.com

    Not a very good Idea!

    Hi all,
    As per our coding rule, a line should not have more than 80 characters.
    When accessing a structure elements which are deeply nested, we are not
    able follow this rule. To acheive this Can we use macro?

    For example,

    struct1.str1.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;

    #define STRUCT2 struct2.str2.st 1.s1
    #define STRUCT3 struct3.str3.st 3.s3

    and then use

    struct1.str1.st rct3 = STRUCT2 + STRUCT3;

    Is this a good Idea(if not please let me know what are the problems it
    may cause)?Or Is there a better Idea than this?

  • Arthur J. O'Dwyer

    #2
    Re: Not a very good Idea!


    On Mon, 3 Apr 2005 vashwath@rediff mail.com wrote:[color=blue]
    >
    > Hi all,
    > As per our coding rule, a line should not have more than 80 characters.
    > When accessing a structure elements which are deeply nested, we are not
    > able follow this rule. To acheive this Can we use macro?[/color]

    Oh, dear.
    [color=blue]
    > For example,
    >
    > struct1.str1.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;[/color]

    First of all, that's fewer than 80 columns. Secondly, the C programming
    language is whitespace-insensitive (in most ways), so you can always write

    struct1.str1.st rct3 = struct2.str2.st 1.s1
    + struct3.str3.st 3.s3;

    A much better alternative is to rework your data structure, and use the
    much simpler

    x = y + z;

    Of course, this will require getting rid of a lot of tiny intermediate
    struct definitions (struct1, str1, st1, s1, and so on). But that's a
    very good thing. Nested structs are bad for readability, and mostly bad
    for thinking in general.

    If you can't do that (for example, if you have more silly "house rules"
    that require lots of nested data structures), you can use

    struct foo *p = &struct2.str2.s t1;
    struct foo *q = &struct3.str3.s t3;

    struct1.str1.st rct3 = p->s1 + q->s3;

    Again, not the best solution, but a darn sight better than trying to
    use the preprocessor to wiggle out of a problem you yourself created
    with nested data structures.

    HTH,
    -Arthur

    Comment

    • Keith Thompson

      #3
      Re: Not a very good Idea!

      vashwath@rediff mail.com writes:[color=blue]
      > As per our coding rule, a line should not have more than 80 characters.
      > When accessing a structure elements which are deeply nested, we are not
      > able follow this rule. To acheive this Can we use macro?
      >
      > For example,
      >
      > struct1.str1.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;
      >
      > #define STRUCT2 struct2.str2.st 1.s1
      > #define STRUCT3 struct3.str3.st 3.s3
      >
      > and then use
      >
      > struct1.str1.st rct3 = STRUCT2 + STRUCT3;
      >
      > Is this a good Idea(if not please let me know what are the problems it
      > may cause)?Or Is there a better Idea than this?[/color]

      struct1.str1.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;

      is obviously less than 80 columns, but assuming it's meant as an
      example, there's no reason it all has to be on one line. The
      following are equivalent:

      struct1.str1.st rct3 =
      struct2.str2.st 1.s1 + struct3.str3.st 3.s3;

      struct1.str1.st rct3 =
      struct2.str2.st 1.s1 +
      struct3.str3.st 3.s3;

      struct1
      .str1
      .strct3
      =
      struct2
      .str2
      .st1
      .s1
      +
      struct3
      .str3
      .st3
      .s3;

      It would probably be even better to restructure your code so you're
      not using so many deeply nested structures.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • pete

        #4
        Re: Not a very good Idea!

        Keith Thompson wrote:
        [color=blue]
        > struct1.str1.st rct3 =
        > struct2.str2.st 1.s1 +
        > struct3.str3.st 3.s3;[/color]

        I prefer to start, rather than to terminate,
        a continued line with a binary operator,
        to make it more obvious that it's a continued line.

        struct1.str1.st rct3
        = struct2.str2.st 1.s1
        + struct3.str3.st 3.s3;

        --
        pete

        Comment

        • Erik de Castro Lopo

          #5
          Re: Not a very good Idea!

          vashwath@rediff mail.com wrote:[color=blue]
          >
          > Hi all,
          > As per our coding rule, a line should not have more than 80 characters.
          > When accessing a structure elements which are deeply nested, we are not
          > able follow this rule. To acheive this Can we use macro?
          >
          > For example,
          >
          > struct1.str1.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;
          >
          > #define STRUCT2 struct2.str2.st 1.s1
          > #define STRUCT3 struct3.str3.st 3.s3
          >
          > and then use
          >
          > struct1.str1.st rct3 = STRUCT2 + STRUCT3;
          >
          > Is this a good Idea[/color]

          No.
          [color=blue]
          > (if not please let me know what are the problems it
          > may cause)?[/color]

          Difficult to read, difficult to debug when it goes wrong.
          [color=blue]
          > Or Is there a better Idea than this?[/color]

          Use a C99 compiler or a compiler that understand inline and
          use something like


          static inline void
          struct_add (STRUCT_NAME *result, const STRUCT_NAME *left, const STRUCT_NAME *right)
          { result->whatever.resul t = left->x.y.z + right->x.y.z ;
          }

          and you can call that using:

          struct_add (&result, &left, &right) ;
          
          if they are defined as structs or

          struct_add (result, left, right) ;

          if you already have pointers to the structs.

          Erik
          --
          +-----------------------------------------------------------+
          Erik de Castro Lopo nospam@mega-nerd.com (Yes it's valid)
          +-----------------------------------------------------------+
          Learning Linux is like joining a cult. Sure it's fun at first but
          you waste time, become brainwashed, and then have to be de-programmed
          by Bill Gates before you can work for Him again.
          - Ray Lopez, in UFhK4.33289$y4. 1192894@newsrea d1.prod.itd.ear thlink.net

          Comment

          • CBFalconer

            #6
            Re: Not a very good Idea!

            vashwath@rediff mail.com wrote:[color=blue]
            >
            > As per our coding rule, a line should not have more than 80
            > characters. When accessing a structure elements which are deeply
            > nested, we are not able follow this rule. To acheive this Can we
            > use macro?
            >
            > For example,
            >
            > struct1.str1.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;
            >
            > #define STRUCT2 struct2.str2.st 1.s1
            > #define STRUCT3 struct3.str3.st 3.s3
            >
            > and then use
            >
            > struct1.str1.st rct3 = STRUCT2 + STRUCT3;
            >
            > Is this a good Idea(if not please let me know what are the
            > problems it may cause)?Or Is there a better Idea than this?[/color]

            Not in my opinion. It shows up the lack of a 'with' statement in
            C, but that is another matter. You can simply break the statement
            up:

            struct1.str1.st rct3 = struct2.str2.st 1.s1
            + struct3.str3.st 3.s3;

            I also consider an 80 char line length excessive, 72 is better. If
            you are continuously falling off the right it probably indicates
            that you are failing to break your code up into small enough
            modules.

            --
            "If you want to post a followup via groups.google.c om, don't use
            the broken "Reply" link at the bottom of the article. Click on
            "show options" at the top of the article, then click on the
            "Reply" at the bottom of the article headers." - Keith Thompson


            Comment

            • Eric Sosman

              #7
              Re: Not a very good Idea!

              vashwath@rediff mail.com wrote:[color=blue]
              > Hi all,
              > As per our coding rule, a line should not have more than 80 characters.
              > When accessing a structure elements which are deeply nested, we are not
              > able follow this rule. To acheive this Can we use macro?
              >
              > For example,
              >
              > struct1.str1.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;[/color]

              Others have mentioned various ways of inserting line
              breaks to keep individual lines short, but I have not yet
              seen anything as radical as

              struct1

              Comment

              • Lawrence Kirby

                #8
                Re: Not a very good Idea!

                On Mon, 04 Apr 2005 11:34:58 +0000, CBFalconer wrote:

                ....
                [color=blue]
                > Not in my opinion. It shows up the lack of a 'with' statement in
                > C, but that is another matter.[/color]

                IMO Pascal's with statement is one of the truely nasty parts of that
                language. C has a vastly better way of dealing with that problem using
                pointers to structures or unions. In effect you can create a with
                construct where each structure being aliased is named explicitly.
                [color=blue]
                > You can simply break the statement
                > up:
                >
                > struct1.str1.st rct3 = struct2.str2.st 1.s1
                > + struct3.str3.st 3.s3;[/color]

                Yes, there are lots of ways of handling this, this looks good here.
                Intermediaries could be appropriate in some circumstances, e.g.

                s2211 = struct2.str2.st 1.s1;
                s3333 = struct3.str3.st 3.s3;
                struct1.str1.st rct3 = s2211 + s3333;

                Using with-like pointers could be natural if you access a number of
                members from an innermost structure

                type *const p221 = &struct2.str2.s t1;
                type *const p333 = &struct3.str3.s t3;

                struct1.str1.st rct3 = p221->s1 + p333->s3;

                struct2.str3.st rct1 = p221->s3 + p333->s1; /* for example */

                This can work for the LHS too.
                [color=blue]
                > I also consider an 80 char line length excessive, 72 is better. If
                > you are continuously falling off the right it probably indicates
                > that you are failing to break your code up into small enough
                > modules.[/color]

                It is very easy to get to and beyond 80 columns, although I try to limit
                myself to that for C code. It depends on indentation amount, expression
                complexity, whether you have some reasonable length text in string
                literals and so on. Making modules (do you mean functions?) too small is
                not a good thing, it creates complexity in the interfaces and call tree.

                Lawrence


                Comment

                • Alan Balmer

                  #9
                  Re: Not a very good Idea!

                  On Mon, 04 Apr 2005 09:50:17 GMT, pete <pfiland@mindsp ring.com> wrote:
                  [color=blue]
                  >Keith Thompson wrote:
                  >[color=green]
                  >> struct1.str1.st rct3 =
                  >> struct2.str2.st 1.s1 +
                  >> struct3.str3.st 3.s3;[/color]
                  >
                  >I prefer to start, rather than to terminate,
                  >a continued line with a binary operator,
                  >to make it more obvious that it's a continued line.
                  >
                  >struct1.str1.s trct3
                  > = struct2.str2.st 1.s1
                  > + struct3.str3.st 3.s3;[/color]

                  An interesting take. I generally read code from top to bottom, so
                  prefer the binary operator on the end of the line, to make it more
                  obvious that there's more to come.

                  --
                  Al Balmer
                  Balmer Consulting
                  removebalmercon sultingthis@att .net

                  Comment

                  • Andrey Tarasevich

                    #10
                    Re: Not a very good Idea!

                    Keith Thompson wrote:[color=blue][color=green]
                    >> As per our coding rule, a line should not have more than 80 characters.
                    >> When accessing a structure elements which are deeply nested, we are not
                    >> able follow this rule. To acheive this Can we use macro?
                    >>
                    >> For example,
                    >>
                    >> struct1.str1.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;
                    >> ...[/color]
                    > ...
                    > is obviously less than 80 columns, but assuming it's meant as an
                    > example, there's no reason it all has to be on one line. The
                    > following are equivalent:
                    >
                    > struct1.str1.st rct3 =
                    > struct2.str2.st 1.s1 + struct3.str3.st 3.s3;
                    >
                    > struct1.str1.st rct3 =
                    > struct2.str2.st 1.s1 +
                    > struct3.str3.st 3.s3;
                    >
                    > struct1
                    > .str1
                    > .strct3
                    > =
                    > struct2
                    > .str2
                    > .st1
                    > .s1
                    > +
                    > struct3
                    > .str3
                    > .st3
                    > .s3;
                    > ...[/color]

                    And there's always the '\' at the end of line, which greatly expands the
                    number of variants this code can be reformatted

                    struct1.s\
                    tr1.strct\
                    3 = struc\
                    t2.str2.s\
                    t1.s1 + s\
                    truct3.st\
                    r3.st3.s3;

                    Of course, professional programmers always format their code so that the
                    way the code looks reflects what this code does. The above, for example,
                    is a nice way to format a code that calculates the area of a rectangle.

                    :)

                    --
                    Best regards,
                    Andrey Tarasevich

                    Comment

                    • Walter Roberson

                      #11
                      Re: Not a very good Idea!

                      In article <mn.25417d548dc fd804.15512@YOU RBRAnoos.fr>,
                      Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:[color=blue]
                      >pete wrote on 04/04/05 :[color=green]
                      >> I prefer to start, rather than to terminate,
                      >> a continued line with a binary operator,
                      >> to make it more obvious that it's a continued line.[/color][/color]
                      [color=blue][color=green]
                      >> struct1.str1.st rct3
                      >> = struct2.str2.st 1.s1
                      >> + struct3.str3.st 3.s3;[/color][/color]
                      [color=blue]
                      >Agreed.[/color]


                      FORTRAN lives ;-)

                      (and, no, I didn't mean "Fortran").
                      --
                      Usenet is like a slice of lemon, wrapped around a large gold brick.

                      Comment

                      • Emmanuel Delahaye

                        #12
                        Re: Not a very good Idea!

                        pete wrote on 04/04/05 :[color=blue]
                        > Keith Thompson wrote:
                        >[color=green]
                        >> struct1.str1.st rct3 =
                        >> struct2.str2.st 1.s1 +
                        >> struct3.str3.st 3.s3;[/color]
                        >
                        > I prefer to start, rather than to terminate,
                        > a continued line with a binary operator,
                        > to make it more obvious that it's a continued line.
                        >
                        > struct1.str1.st rct3
                        > = struct2.str2.st 1.s1
                        > + struct3.str3.st 3.s3;[/color]

                        Agreed.

                        --
                        Emmanuel
                        The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                        The C-library: http://www.dinkumware.com/refxc.html

                        ..sig under repair

                        Comment

                        • Chris Croughton

                          #13
                          Re: Not a very good Idea!

                          On Mon, 04 Apr 2005 10:17:19 -0700, Alan Balmer
                          <albalmer@att.n et> wrote:
                          [color=blue]
                          > On Mon, 04 Apr 2005 09:50:17 GMT, pete <pfiland@mindsp ring.com> wrote:
                          >[color=green]
                          >>Keith Thompson wrote:
                          >>[color=darkred]
                          >>> struct1.str1.st rct3 =
                          >>> struct2.str2.st 1.s1 +
                          >>> struct3.str3.st 3.s3;[/color]
                          >>
                          >>I prefer to start, rather than to terminate,
                          >>a continued line with a binary operator,
                          >>to make it more obvious that it's a continued line.
                          >>
                          >>struct1.str1. strct3
                          >> = struct2.str2.st 1.s1
                          >> + struct3.str3.st 3.s3;[/color]
                          >
                          > An interesting take. I generally read code from top to bottom, so
                          > prefer the binary operator on the end of the line, to make it more
                          > obvious that there's more to come.[/color]

                          I do as well, but it's a style thing (roughly half of us at work prefer
                          the operators at the end and the others prefer them at the start of the
                          next line).

                          There is no "one true way"...

                          Chris C

                          Comment

                          • Ian Pilcher

                            #14
                            Re: Not a very good Idea!

                            Chris Croughton wrote:[color=blue]
                            > There is no "one true way"...[/color]

                            You're new around here, aren't you?

                            --
                            =============== =============== =============== =============== ============
                            Ian Pilcher i.pilcher@comca st.net
                            =============== =============== =============== =============== ============

                            Comment

                            • Walter Roberson

                              #15
                              Re: Not a very good Idea!

                              In article <yNGdnUq9UKBpWM zfRVn-oA@comcast.com> ,
                              Ian Pilcher <i.pilcher@comc ast.net> wrote:
                              :Chris Croughton wrote:
                              :> There is no "one true way"...

                              :You're new around here, aren't you?

                              Apparently, for half of us, it would have been fine if you had written,

                              You
                              're new around here
                              , aren't you?
                              --
                              "[...] it's all part of one's right to be publicly stupid." -- Dave Smey

                              Comment

                              Working...