indentation

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

    indentation

    I have had several complaints by some people who wish to help me and I
    wish to get the problem straight. I wrote this small utility myself and
    added some indentation and I wonder if it is acceptable. It does make source
    easier to read.

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char **argv) {
    if (argc!=3) {
    fprintf(stderr, "usage error\n");
    return -1;
    }
    double x,y;
    x=strtod(argv[1],NULL);
    y=strtod(argv[2],NULL);
    printf("%.2f\n" ,y/x);
    return 0;
    }

    Is this a good example of a properly indended program?

    Bill


  • Joe Wright

    #2
    Re: indentation

    Bill Cunningham wrote:
    I have had several complaints by some people who wish to help me and I
    wish to get the problem straight. I wrote this small utility myself and
    added some indentation and I wonder if it is acceptable. It does make source
    easier to read.
    >
    #include <stdio.h>
    #include <stdlib.h>
    >
    int main(int argc, char **argv) {
    if (argc!=3) {
    fprintf(stderr, "usage error\n");
    return -1;
    }
    double x,y;
    x=strtod(argv[1],NULL);
    y=strtod(argv[2],NULL);
    printf("%.2f\n" ,y/x);
    return 0;
    }
    >
    Is this a good example of a properly indended program?
    >
    Bill
    >
    >
    Find the GNU 'indent' utility. It gives us..

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char **argv)
    {
    if (argc != 3) {
    fprintf(stderr, "usage error\n");
    return -1;
    }
    double x, y;
    x = strtod(argv[1], NULL);
    y = strtod(argv[2], NULL);
    printf("%.2f\n" , y / x);
    return 0;
    }

    ...given your program as input. What do you think?

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---

    Comment

    • CBFalconer

      #3
      Re: indentation

      Bill Cunningham wrote:
      >
      I have had several complaints by some people who wish to help me
      and I wish to get the problem straight. I wrote this small utility
      myself and added some indentation and I wonder if it is acceptable.
      It does make source easier to read.
      >
      #include <stdio.h>
      #include <stdlib.h>
      >
      int main(int argc, char **argv) {
      if (argc!=3) {
      fprintf(stderr, "usage error\n");
      return -1;
      }
      double x,y;
      x=strtod(argv[1],NULL);
      y=strtod(argv[2],NULL);
      printf("%.2f\n" ,y/x);
      return 0;
      }
      >
      Is this a good example of a properly indended program?
      Close. The main problems are too little indentation (I suggest 3
      spaces) and the lack of spaces in the source. Also the variable
      declaration should not occur after executable code. Compare the
      following:

      #include <stdio.h>
      #include <stdlib.h>

      int main(int argc, char **argv) {
      double x,y;

      if (argc != 3) {
      fprintf(stderr, "usage error\n");
      return -1;
      }
      x = strtod(argv[1], NULL);
      y = strtod(argv[2], NULL);
      printf("%.2f\n" , y/x);
      return 0;
      } /* main */

      I like to mark the closing brace of a function with the function
      name.

      There is a program around called indent (GNU version 2.2.9 here)
      which does all this for you. It is configurable. I use the
      following configuration for it (really just one long line in
      indent.pro):

      -kr -l66 -i3 -bad -di16 -lc66 -nce -ncs -cbi0 -bbo -pmt -psl -ts1
      -cdw -ppi 3

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


      ** Posted from http://www.teranews.com **

      Comment

      • Bill Cunningham

        #4
        Re: indentation


        "Joe Wright" <joewwright@com cast.netwrote in message
        news:xdKdnd0Wq_ nixb_VnZ2dnUVZ_ jSdnZ2d@comcast .com...
        ..given your program as input. What do you think?
        >
        The first copy of the file I wrote has had its indentation changed by
        the news server or client. It was indented when I posted it and the
        indentation was removed. Maybe nntp does that. The progran was originally
        indented like this.

        if (argc!3) {
        fprintf(stderr, "usage error\n");
        return -1;
        }

        Thatis what was copied onto the post and it was posted without the
        indentation.

        Bill


        Comment

        • Ian Collins

          #5
          Re: indentation

          Bill Cunningham wrote:
          "Joe Wright" <joewwright@com cast.netwrote in message
          news:xdKdnd0Wq_ nixb_VnZ2dnUVZ_ jSdnZ2d@comcast .com...
          >..given your program as input. What do you think?
          >>
          >
          The first copy of the file I wrote has had its indentation changed by
          the news server or client. It was indented when I posted it and the
          indentation was removed. Maybe nntp does that. The progran was originally
          indented like this.
          >
          You probably used tabs rather than spaces. Stick to (two) spaces and
          your code will be left unmolested.

          --
          Ian Collins.

          Comment

          • santiago538

            #6
            Re: indentation

            Keith Thompson wrote:
            "Bill Cunningham" <nospam@nspam.c omwrites:
            > I have had several complaints by some people who wish to help me and I
            >wish to get the problem straight. I wrote this small utility myself and
            >added some indentation and I wonder if it is acceptable. It does make source
            >easier to read.
            >>
            >#include <stdio.h>
            >#include <stdlib.h>
            >>
            >int main(int argc, char **argv) {
            > if (argc!=3) {
            > fprintf(stderr, "usage error\n");
            > return -1;
            > }
            > double x,y;
            > x=strtod(argv[1],NULL);
            > y=strtod(argv[2],NULL);
            > printf("%.2f\n" ,y/x);
            > return 0;
            > }
            >>
            > Is this a good example of a properly indended program?
            >
            Better, but not yet good.
            >
            A single space per indentation level is IMHO insufficient.
            It's still very hard to see what's indented. I like 4-column
            indentation myself; I consider 2 to be the bare minimum. (Some
            advocate 8-column indentation, which certainly encourages Any
            decent text editor should do most of the work for you automatically
            (":se ai" or ":set autoindent" in vi, for example).
            >
            There are several common styles for indentation and brace placement.
            Your program exhibits none of them. That's not necessarily A Bad
            Thing, but it's a bit jarring, and there's rarely any good reason
            not to follow one of the existing styles.
            >
            Two of the most common styles are:
            >
            1. K&R style. An opening brace '{' goes at the end of a line,
            preceded by a space (except possibly for the opening brace of a
            function definition, for historical reasons). Enclosed lines are
            indented by one level (e.g., by 4 columns). The closing brace is
            aligned directly under the beginning of the line containing the
            opening brace (this is where you go astray).
            >
            2. Opening and closing braces appear on lines by themselves.
            Enclosed lines are indented by one level.
            >
            Rarer, but still valid, styles are:
            >
            3. Like 2, but the braces are aligned with the enclosed lines.
            >
            4. Like 2, but the braces are half-indented (e.g., by 2 columns).
            I think this is the GNU-recommended style.
            >
            That is the GNU style and is the default provided by indent. It is
            fairly common, especially by open source enthusiasts, though I cannot
            think of a more hideous style, and I've seen my share of truly awful C code.

            Comment

            • Bill Cunningham

              #7
              Re: indentation


              "Ian Collins" <ian-news@hotmail.co mwrote in message
              news:68f6kcF2ro 4ulU1@mid.indiv idual.net...
              You probably used tabs rather than spaces. Stick to (two) spaces and
              your code will be left unmolested.
              >
              Exactly. I always do. Maybe it's a bad habit.

              Bill


              Comment

              • cr88192

                #8
                Re: indentation


                "Bill Cunningham" <nospam@nspam.c omwrote in message
                news:%7sUj.3801 $Ve.393@trnddc0 8...
                I have had several complaints by some people who wish to help me and I
                wish to get the problem straight. I wrote this small utility myself and
                added some indentation and I wonder if it is acceptable. It does make
                source easier to read.
                >
                #include <stdio.h>
                #include <stdlib.h>
                >
                int main(int argc, char **argv) {
                if (argc!=3) {
                fprintf(stderr, "usage error\n");
                return -1;
                }
                double x,y;
                x=strtod(argv[1],NULL);
                y=strtod(argv[2],NULL);
                printf("%.2f\n" ,y/x);
                return 0;
                }
                >
                Is this a good example of a properly indended program?
                >
                well, this gets into this big hairy area known as "coding style" (most have
                opinions, few can completely agree...).


                now, I usually code in notepad, which has inflexible 8-space tabs, so
                usually I use this.
                if the tab space is adjustable, usually I like 4 space tabs.

                2 or 3 spaces is IMO too little.
                1 space is just horrid (may as well not indent at all...).


                usually, I put opening and closing braces on their own lines, and closing
                braces are indended the same as the opening braces.

                int main(int argc, char *argv[])
                {
                FILE *fd;
                if(argv<2)
                {
                printf("usage: %s <filename>\n" , argv[0]);
                return(-1);
                }

                fd=fopen(argv[1], "rb");
                ...
                return(0);
                }

                note that EXIT_SUCCESS and EXIT_FAILURE are considered "more correct" for
                main return values, but 0 and -1 are more common/traditional.


                IMO, both forms:
                if(...)
                {

                and:
                if(...) {

                are fairly common and acceptable, but most people put the brace on its own
                line for functions, and rarely for structs or unions.

                it is common for commas to be followed by a space ("f(x, y);" but not
                "f(x,y);").
                some people precede/follow parens and/or operators with spaces.

                if certain single-letter variable names are used (especially, i,j,k,s,t,...)
                it is almost obligatory that they be certain types (i,j,k are int, s,t are
                'char *', ...).

                return is often/usually written as if it were a function call (common in C,
                rare in C++).

                and so on...

                Bill
                >
                >

                Comment

                • Richard Heathfield

                  #9
                  Re: indentation

                  cr88192 said:

                  <snip>
                  >
                  now, I usually code in notepad,
                  Oh dear. :-)
                  which has inflexible 8-space tabs, so
                  usually I use this.
                  if the tab space is adjustable, usually I like 4 space tabs.
                  Tab/space wars are so 1990s, though, aren't they?
                  2 or 3 spaces is IMO too little.
                  And IMO 3 is too many. Vive la difference!
                  1 space is just horrid (may as well not indent at all...).
                  Agreed.
                  usually, I put opening and closing braces on their own lines, and closing
                  braces are indended the same as the opening braces.
                  Agreed again.

                  You forgot <stdio.h>
                  int main(int argc, char *argv[])
                  {
                  FILE *fd;
                  if(argv<2)
                  You meant argc.
                  {
                  printf("usage: %s <filename>\n" , argv[0]);
                  If argc is 0, the behaviour is undefined. If it is >= 1, argv[0] must
                  represent the program name in some way, but need not be a string
                  representing the invocation name for the program. It could even be a pid!
                  return(-1);
                  This has no portable meaning (and the parentheses are redundantly
                  superfluous).
                  }
                  >
                  fd=fopen(argv[1], "rb");
                  ...
                  return(0);
                  Again, the parentheses are superfluously redundant.
                  }
                  >
                  note that EXIT_SUCCESS and EXIT_FAILURE are considered "more correct" for
                  main return values,
                  0 is fine - it means success.
                  but 0 and -1 are more common/traditional.
                  A -1 return value has no de jure meaning in C (which is, at least, in
                  keeping with the better kinds of tradition - if we knew why we did them,
                  they wouldn't be traditions!).

                  To indicate failure portably, use EXIT_FAILURE.
                  IMO, both forms:
                  if(...)
                  {
                  >
                  and:
                  if(...) {
                  >
                  are fairly common and acceptable, but most people put the brace on its
                  own line for functions, and rarely for structs or unions.
                  The word "most" is arguable. K&R's style is perniciously persistent even
                  now. And a significant number of Allman adherents /do/ put a struct brace
                  on its own line.
                  it is common for commas to be followed by a space ("f(x, y);" but not
                  "f(x,y);").
                  True, and wise.
                  some people precede/follow parens and/or operators with spaces.
                  True, and a matter of taste, I think. My own taste is for parentheses not
                  to "command" any whitespace, but for binary operators to be separated from
                  their operands by a space.
                  if certain single-letter variable names are used (especially,
                  i,j,k,s,t,...) it is almost obligatory that they be certain types (i,j,k
                  are int, s,t are 'char *', ...).
                  No, not really. Common, yes. Obligatory? Hardly.
                  >
                  return is often/usually written as if it were a function call (common in
                  C, rare in C++).
                  return /isn't/ a function call, and it seems to me from perusing this group
                  and from what I've seen of good C code (in well-regarded literature, in
                  workplaces, and on the Web) that few if any experienced C programmers
                  treat it like one.

                  --
                  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

                  Comment

                  • Mark McIntyre

                    #10
                    Re: indentation

                    Bill Cunningham wrote:
                    "Joe Wright" <joewwright@com cast.netwrote in message
                    news:xdKdnd0Wq_ nixb_VnZ2dnUVZ_ jSdnZ2d@comcast .com...
                    >..given your program as input. What do you think?
                    >>
                    >
                    The first copy of the file I wrote has had its indentation changed by
                    the news server or client. It was indented when I posted it and the
                    indentation was removed. Maybe nntp does that. The progran was originally
                    indented like this.
                    >
                    if (argc!3) {
                    fprintf(stderr, "usage error\n");
                    return -1;
                    }
                    This is one of many possible styles. The main thing is to use
                    intentation to identify each block, and to use a consistent style.

                    The two commonest are probable

                    if (x)
                    {
                    bodyhere();
                    }

                    and
                    if (x) {
                    bodyhere();
                    }

                    The one you used above is less common in my experience.

                    Comment

                    • Nick Keighley

                      #11
                      Re: indentation

                      On 8 May, 01:51, "Bill Cunningham" <nos...@nspam.c omwrote:
                          I have had several complaints by some people who wish to help me
                      and I wish to get the problem straight. I wrote this small utility
                      myself and added some indentation and I wonder if it is acceptable.
                      It does make source easier to read.
                      that's the idea! :-)
                      #include <stdio.h>
                      #include <stdlib.h>
                      >
                      int main(int argc, char **argv) {
                       if (argc!=3) {
                        fprintf(stderr, "usage error\n");
                        return -1;
                        }
                       double x,y;
                       x=strtod(argv[1],NULL);
                       y=strtod(argv[2],NULL);
                       printf("%.2f\n" ,y/x);
                       return 0;
                       }
                      >
                          Is this a good example of a properly indended program?

                      well it's better! In fact it's good enough to prevent
                      me whining about indentation. Personnally I prefer the
                      braces to line up, but the above is ok. I definitly
                      prefer whitespace around binary operators (eg. =)
                      and I'd use more blank lines. But on usenet saving vertical
                      space can be a good thing.

                      #include <stdio.h>
                      #include <stdlib.h>

                      int main (int argc, char **argv)
                      {
                      double x;
                      double y;

                      if (argc != 3)
                      {
                      fprintf (stderr,"usage error\n");
                      return -1;
                      }

                      x = strtod (argv [1], NULL);
                      y = strtod (argv [2], NULL);

                      printf ("%.2f\n", y / x);

                      return 0;
                      }

                      the main thing is to group things together that
                      logically belong together, leave enough whitespace
                      (horizontal and vertical) to aid readability and
                      above all, be consistent.

                      Oh, and never use tabs!

                      [mutter, one project had a standard of 4 column indent
                      and 8 space tabs. So you mixed tabs and spaces. eek!]


                      --
                      Nick Keighley

                      "The Real Programmer wants a "you asked for it, you got it"
                      text editor--complicated, cryptic, powerful, unforgiving,
                      dangerous. TECO, to be precise."

                      Comment

                      • pereges

                        #12
                        Re: indentation

                        i use tabs for eg.

                        void func1(void)
                        {
                        ...

                        Comment

                        • pete

                          #13
                          Re: indentation

                          pereges wrote:
                          i use tabs for eg.
                          >
                          void func1(void)
                          {
                          ...
                          My tab key is configured to give me four space characters
                          in my source code editor.
                          Tab characters don't post very well.

                          --
                          pete

                          Comment

                          • brix99luftballons

                            #14
                            Re: indentation



                            Richard Heathfield ha scritto:
                            cr88192 said:
                            >
                            <snip>
                            >
                            >now, I usually code in notepad,
                            >>
                            >
                            Oh dear. :-)
                            >
                            Oh dear dear :-)
                            >which has inflexible 8-space tabs, so
                            >usually I use this.
                            >if the tab space is adjustable, usually I like 4 space tabs.
                            >>
                            And IMO 3 is too many. Vive la difference!
                            >
                            >
                            You forgot <stdio.h>
                            You meant argc.
                            >
                            A type , may be...
                            If argc is 0, the behaviour is undefined. If it is >= 1, argv[0] must
                            represent the program name in some way, but need not be a string
                            representing the invocation name for the program. It could even be a pid!
                            >
                            argc must be 1 (the program name ia always passed as argument), by
                            standard.
                            But, yes, it is best to check it...
                            > return(-1);
                            >>
                            >
                            This has no portable meaning (and the parentheses are redundantly
                            superfluous).
                            >
                            I'm not agree, it' more readable.
                            >>
                            > fd=fopen(argv[1], "rb");
                            > ...
                            > return(0);
                            >>
                            >
                            Again, the parentheses are superfluously redundant.
                            >
                            Question of style: for me it is better to place parentesis around the
                            returned value.
                            What about an horrible return like this one ????
                            return a*32 / b + c - d * e / oh_my_good ;
                            0 is fine - it means success.
                            >
                            >
                            Ok, it's a standard value
                            A -1 return value has no de jure meaning in C (which is, at least, in
                            keeping with the better kinds of tradition - if we knew why we did them,
                            they wouldn't be traditions!).
                            >
                            To indicate failure portably, use EXIT_FAILURE.
                            >
                            :-)

                            brix

                            Comment

                            • pete

                              #15
                              Re: indentation

                              brix99luftballo ns wrote:
                              argc must be 1 (the program name ia always passed as argument), by
                              standard.
                              No. argc may be zero.

                              N869
                              5.1.2.2.1 Program startup

                              [#2] If they are declared, the parameters to the main
                              function shall obey the following constraints:

                              -- The value of argc shall be nonnegative.

                              -- argv[argc] shall be a null pointer.

                              -- If the value of argc is greater than zero, the array
                              members argv[0] through argv[argc-1] inclusive shall
                              contain pointers to strings, which are given
                              implementation-defined values by the host environment
                              prior to program startup. The intent is to supply to
                              the program information determined prior to program
                              startup from elsewhere in the hosted environment. If
                              the host environment is not capable of supplying
                              strings with letters in both uppercase and lowercase,
                              the implementation shall ensure that the strings are
                              received in lowercase.

                              -- If the value of argc is greater than zero, the string
                              pointed to by argv[0] represents the program name;
                              argv[0][0] shall be the null character if the program
                              name is not available from the host environment. If
                              the value of argc is greater than one, the strings
                              pointed to by argv[1] through argv[argc-1] represent
                              the program parameters.

                              --
                              pete

                              Comment

                              Working...