Variable naming conventions.

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

    Variable naming conventions.

    Apart from the some rules set by the C standard what , in your
    opinion, are good naming convetions for variables ? Can you please
    give an example or two ?

  • vippstar@gmail.com

    #2
    Re: Variable naming conventions.

    On Aug 9, 3:27 pm, pereges <Brol...@gmail. comwrote:
    Apart from the some rules set by the C standard what , in your
    opinion, are good naming convetions for variables ? Can you please
    give an example or two ?
    I like to prefix my variables with O0318_

    Comment

    • William Pursell

      #3
      Re: Variable naming conventions.

      On 9 Aug, 13:27, pereges <Brol...@gmail. comwrote:
      Apart from the some rules set by the C standard what , in your
      opinion, are good naming convetions for variables ? Can you please
      give an example or two ?
      Use all lowercase letters, and full words separated by underscores:

      good examples:
      char *first_name;

      bad examples:
      char *fn;
      char *firstName;
      char *frst_nm; /* ugh */

      In some cases, short names may be okay as long as their scope
      is less than 10 lines or so. IOW, this is okay:

      {
      char *fn; /* first name */
      ...
      }

      **ONLY IF** the total number of new lines between the opening brace
      and
      the closing brace is small enough that the comment will be visible
      98% of the time a person sees fn in use. And fn should probably
      appear in only a few places. Even then it is questionable.
      Code for readability. If you are coding to save keystrokes,
      stop coding. (Besides, any reasonable editor will autocomplete
      names for you, so there really is no excuse to use short names.)



      Comment

      • John Floren

        #4
        Re: Variable naming conventions.

        On 2008-08-09, pereges <Broli00@gmail. comwrote:
        Apart from the some rules set by the C standard what , in your
        opinion, are good naming convetions for variables ? Can you please
        give an example or two ?
        >
        Read "The Practice of Programming" by Kernighan and Pike. The
        appropriate sections can be read at http://preview.tinyurl.com/6fpfw9
        (google books), page 3.
        In my opinion, all C programmers should read this book.

        John

        Comment

        • James Harris

          #5
          Re: Variable naming conventions.

          On 9 Aug, 12:27, pereges <Brol...@gmail. comwrote:
          Apart from the some rules set by the C standard what , in your
          opinion, are good naming convetions for variables ? Can you please
          give an example or two ?
          Code Complete by Steve McConnell has a long section on data names -
          pages 185 to 213. IIRC even after reading it, though, I still felt
          some questions were unanswered.

          FWIW the main options I think are

          All lower case: filename, finaldollarbala nce
          Initial lower case: fileName, finalDollarBala nce
          Initial upper case: FileName, FinalDollarBala nce
          Underscore separator: file_name, final_dollar_ba lance
          Hyphen separator (not C): file-name, final-dollar-balance

          Of these the first is ok for short names (single word or single
          letter) but falls down with words that can run together differently:

          barbend

          could be bar_bend or barb_end, for example.

          Since you asked for opinions for data names I guess my preferences are
          InitialUpperCas e and underscore_sepa rator.

          --
          HTH,
          James

          Comment

          • James Harris

            #6
            Re: Variable naming conventions.

            On 9 Aug, 23:36, James Harris <james.harri... @googlemail.com wrote:
            On 9 Aug, 12:27, pereges <Brol...@gmail. comwrote:
            >
            Apart from the some rules set by the C standard what , in your
            opinion, are good naming convetions for variables ? Can you please
            give an example or two ?
            >
            Code Complete by Steve McConnell has a long section on data names -
            pages 185 to 213. IIRC even after reading it, though, I still felt
            some questions were unanswered.
            >
            FWIW the main options I think are
            >
            All lower case: filename, finaldollarbala nce
            Initial lower case: fileName, finalDollarBala nce
            Initial upper case: FileName, FinalDollarBala nce
            Underscore separator: file_name, final_dollar_ba lance
            Hyphen separator (not C): file-name, final-dollar-balance
            >
            Of these the first is ok for short names (single word or single
            letter) but falls down with words that can run together differently:
            >
            barbend
            >
            could be bar_bend or barb_end, for example.
            >
            Since you asked for opinions for data names I guess my preferences are
            InitialUpperCas e and underscore_sepa rator.
            >
            I should have said, some people like to include type information as
            part of the name, either at the beginning or the end.

            --
            James

            Comment

            • Richard Heathfield

              #7
              Re: Variable naming conventions.

              James Harris said:

              <snip>
              I should have said, some people like to include type information as
              part of the name, either at the beginning or the end.
              There should be a law against that.

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

              • Keith Thompson

                #8
                Re: Variable naming conventions.

                pereges <Broli00@gmail. comwrites:
                Apart from the some rules set by the C standard what , in your
                opinion, are good naming convetions for variables ? Can you please
                give an example or two ?
                If you're working on a project with others, follow any conventions
                imposed by the project, even if you think they're ugly.

                If you have the luxury of choosing a convention for yourself, any
                choice you make will be controversial.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                Nokia
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • CBFalconer

                  #9
                  Re: Variable naming conventions.

                  Richard Heathfield wrote:
                  James Harris said:
                  >
                  <snip>
                  >
                  >I should have said, some people like to include type information
                  >as part of the name, either at the beginning or the end.
                  >
                  There should be a law against that.
                  Except for pointers.

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


                  Comment

                  • Ian Collins

                    #10
                    Re: Variable naming conventions.

                    CBFalconer wrote:
                    Richard Heathfield wrote:
                    >James Harris said:
                    >>
                    ><snip>
                    >>
                    >>I should have said, some people like to include type information
                    >>as part of the name, either at the beginning or the end.
                    >There should be a law against that.
                    >
                    Except for pointers.
                    >
                    Why?

                    --
                    Ian Collins.

                    Comment

                    • William Pursell

                      #11
                      Re: Variable naming conventions.

                      On 10 Aug, 04:16, CBFalconer <cbfalco...@yah oo.comwrote:
                      Richard Heathfield wrote:
                      James Harris said:
                      >
                      I should have said, some people like to include type information
                      as part of the name, either at the beginning or the end.
                      >
                      There should be a law against that.
                      >
                      Except for pointers.
                      That's absurd. Treating pointers specially just adds to the
                      beginner's perception that they are somehow mysterious.

                      int value;
                      int *pi_value;

                      is inconsistent and ugly.



                      Comment

                      • Chris M. Thomasson

                        #12
                        Re: Variable naming conventions.

                        "Keith Thompson" <kst-u@mib.orgwrote in message
                        news:lnhc9ta6ak .fsf@nuthaus.mi b.org...
                        pereges <Broli00@gmail. comwrites:
                        >Apart from the some rules set by the C standard what , in your
                        >opinion, are good naming convetions for variables ? Can you please
                        >give an example or two ?
                        >
                        If you're working on a project with others, follow any conventions
                        imposed by the project, even if you think they're ugly.
                        >
                        If you have the luxury of choosing a convention for yourself, any
                        choice you make will be controversial.
                        =^D

                        Comment

                        • James Harris

                          #13
                          Re: Variable naming conventions.

                          On 10 Aug, 07:14, William Pursell <bill.purs...@g mail.comwrote:
                          On 10 Aug, 04:16, CBFalconer <cbfalco...@yah oo.comwrote:
                          >
                          Richard Heathfield wrote:
                          James Harris said:
                          >
                          >I should have said, some people like to include type information
                          >as part of the name, either at the beginning or the end.
                          >
                          There should be a law against that.
                          >
                          Except for pointers.
                          >
                          That's absurd. Treating pointers specially just adds to the
                          beginner's perception that they are somehow mysterious.
                          >
                          int value;
                          int *pi_value;
                          >
                          is inconsistent and ugly.
                          But what about (and I'm not recommending this; only asking the
                          question)

                          int value;
                          int *value_p;

                          --
                          James

                          Comment

                          • James Harris

                            #14
                            Re: Variable naming conventions.

                            On 10 Aug, 04:16, CBFalconer <cbfalco...@yah oo.comwrote:
                            Richard Heathfield wrote:
                            James Harris said:
                            >
                            <snip>
                            >
                            I should have said, some people like to include type information
                            as part of the name, either at the beginning or the end.
                            >
                            There should be a law against that.
                            >
                            Except for pointers.
                            What about types:

                            size_t

                            ?

                            --
                            James

                            Comment

                            • Ian Collins

                              #15
                              Re: Variable naming conventions.

                              James Harris wrote:
                              On 10 Aug, 07:14, William Pursell <bill.purs...@g mail.comwrote:
                              >On 10 Aug, 04:16, CBFalconer <cbfalco...@yah oo.comwrote:
                              >>
                              >>Richard Heathfield wrote:
                              >>>James Harris said:
                              >>>>I should have said, some people like to include type information
                              >>>>as part of the name, either at the beginning or the end.
                              >>>There should be a law against that.
                              >>Except for pointers.
                              >That's absurd. Treating pointers specially just adds to the
                              >beginner's perception that they are somehow mysterious.
                              >>
                              >int value;
                              >int *pi_value;
                              >>
                              >is inconsistent and ugly.
                              >
                              But what about (and I'm not recommending this; only asking the
                              question)
                              >
                              int value;
                              int *value_p;
                              >
                              It's pretty obvious a pointer's a pointer by the way its used.

                              --
                              Ian Collins.

                              Comment

                              Working...