Variable and typename naming

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

    Variable and typename naming

    In C, if I want similar names for typename and variable,
    is there any good way you can recommend me to do that?

    For example, I have a struct containing a region, which
    I have named it, appropriately, struct region, now when I
    declare variables, I want to have the same, or a very
    similar name, since I can't come up with another good name.

    Now, because the different namespaces, I could declare it
    struct region region.
    But maybe that is obscure and bad coding practice?
    Or maybe put a captial letter in the typename, like
    struct Region region, or even a
    typedef struct region { ... } Region. I don't know,
    so I'm grateful for answers.

    Thanks!
  • Walter Roberson

    #2
    Re: Variable and typename naming

    In article <obW_f.52532$d5 .207730@newsb.t elia.net>,
    Edward Gregor <edwgre@hotmail .com> wrote:[color=blue]
    >In C, if I want similar names for typename and variable,
    >is there any good way you can recommend me to do that?[/color]
    [color=blue]
    >For example, I have a struct containing a region, which
    >I have named it, appropriately, struct region, now when I
    >declare variables, I want to have the same, or a very
    >similar name, since I can't come up with another good name.[/color]
    [color=blue]
    >Now, because the different namespaces, I could declare it
    >struct region region.
    >But maybe that is obscure and bad coding practice?[/color]

    Yeah.
    [color=blue]
    >Or maybe put a captial letter in the typename, like
    >struct Region region, or even a
    >typedef struct region { ... } Region. I don't know,
    >so I'm grateful for answers.[/color]

    It isn't uncommon to use slightly different forms for different
    logical layers. For example, some people might use

    struct region_s { ... };
    typedef struct region_s region_t;
    region_t region;

    There are different naming systems in use, especially when
    it comes to procedures. For example, some people
    runthenamestoge ther, some people CapitalizeEachW ord,
    some people use_underscores _between_words, some name
    procedures with a suffix combination that indicates the types
    it operates on; and some people impose the requirement that
    a procedure name must either be a verb (e.g., die()) or else
    must be a clause indicating a verb and a (possibly qualified) noun
    (e.g., TotalSalaries, print_small_che ques). [People who do that
    often also have particular format requirements for object identifiers.]

    --
    "It is important to remember that when it comes to law, computers
    never make copies, only human beings make copies. Computers are given
    commands, not permission. Only people can be given permission."
    -- Brad Templeton

    Comment

    • Edward Gregor

      #3
      Re: Variable and typename naming

      Walter Roberson wrote:[color=blue]
      > struct region_s { ... };
      > typedef struct region_s region_t;
      > region_t region;[/color]

      What would the _s and _t suffices stand for in this example?

      Thanks for the help!

      Comment

      • Keith Thompson

        #4
        Re: Variable and typename naming

        Edward Gregor <edwgre@hotmail .com> writes:[color=blue]
        > Walter Roberson wrote:[color=green]
        >> struct region_s { ... };
        >> typedef struct region_s region_t;
        >> region_t region;[/color]
        >
        > What would the _s and _t suffices stand for in this example?[/color]

        struct and type or typedef, respectively.

        (Personally, I wouldn't bother with the typedef. "struct region" is a
        perfectly good name for the type; there's little advantage in
        inventing another name for it.)

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

        • Walter Roberson

          #5
          Re: Variable and typename naming

          In article <ln7j5vzmbs.fsf @nuthaus.mib.or g>,
          Keith Thompson <kst-u@mib.org> wrote:[color=blue]
          >Edward Gregor <edwgre@hotmail .com> writes:[color=green]
          >> Walter Roberson wrote:[color=darkred]
          >>> struct region_s { ... };
          >>> typedef struct region_s region_t;
          >>> region_t region;[/color][/color][/color]
          [color=blue][color=green]
          >> What would the _s and _t suffices stand for in this example?[/color][/color]
          [color=blue]
          >struct and type or typedef, respectively.[/color]
          [color=blue]
          >(Personally, I wouldn't bother with the typedef. "struct region" is a
          >perfectly good name for the type; there's little advantage in
          >inventing another name for it.)[/color]

          I know some people have posted along those lines in the past. I
          can't say as I agree with that viewpoint, though.

          A typedef can be viewed as an abstract type name: once something
          has been typedef'd, one can use the type name in user code without
          any consideration of what is "underneath the hood".

          "struct region" on the other hand requires that one knows something
          about what is under the hood -- namely that it is a struct. At
          the very least, that tells you some things that the implementation
          -cannot- do with it, such as arithmetic operations. That level of
          knowledge is none of the business of the user code that just needs
          the name of an abstract data type.

          One could, I suppose, go ahead and declare that -all- abstract data
          types names in a program shall be struct tags, so as to put everything
          on an equal footing. It seems to me, though, that doing so would
          merely increase the amount of typing, and would make it more of
          a nuisance at the implementation level, since one would have to
          put in a tag reference even if the underpinings turned out to be
          an arithmetic type. typedef'ing a struct tag into type name
          does not have those disadvantages.

          But then, what do I know? My courses were way back in the days when
          Aho, Hopcroft and Ullman were cutting edge.
          --
          Programming is what happens while you're busy making other plans.

          Comment

          • Keith Thompson

            #6
            Re: Variable and typename naming

            roberson@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:[color=blue]
            > In article <ln7j5vzmbs.fsf @nuthaus.mib.or g>,
            > Keith Thompson <kst-u@mib.org> wrote:[color=green]
            >>Edward Gregor <edwgre@hotmail .com> writes:[color=darkred]
            >>> Walter Roberson wrote:
            >>>> struct region_s { ... };
            >>>> typedef struct region_s region_t;
            >>>> region_t region;[/color][/color]
            >[color=green][color=darkred]
            >>> What would the _s and _t suffices stand for in this example?[/color][/color]
            >[color=green]
            >>struct and type or typedef, respectively.[/color]
            >[color=green]
            >>(Personally , I wouldn't bother with the typedef. "struct region" is a
            >>perfectly good name for the type; there's little advantage in
            >>inventing another name for it.)[/color]
            >
            > I know some people have posted along those lines in the past. I
            > can't say as I agree with that viewpoint, though.
            >
            > A typedef can be viewed as an abstract type name: once something
            > has been typedef'd, one can use the type name in user code without
            > any consideration of what is "underneath the hood".
            >
            > "struct region" on the other hand requires that one knows something
            > about what is under the hood -- namely that it is a struct. At
            > the very least, that tells you some things that the implementation
            > -cannot- do with it, such as arithmetic operations. That level of
            > knowledge is none of the business of the user code that just needs
            > the name of an abstract data type.[/color]
            [snip]

            Yes, If you want an abstract data type, a typedef is just the thing.
            (FILE in <stdio.h> is a good example of this.)

            If the code using the type needs to know that it's a struct (e.g., if
            it refers to its members), a typedef isn't particularly helpful.

            (Possibly a lot of things *should* be abstract types that aren't.)

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

            • August Karlstrom

              #7
              Re: Variable and typename naming

              Edward Gregor wrote:[color=blue]
              > In C, if I want similar names for typename and variable,
              > is there any good way you can recommend me to do that?
              >
              > For example, I have a struct containing a region, which
              > I have named it, appropriately, struct region, now when I
              > declare variables, I want to have the same, or a very
              > similar name, since I can't come up with another good name.[/color]

              What's wrong with `r'?
              [color=blue]
              > Now, because the different namespaces, I could declare it
              > struct region region.
              > But maybe that is obscure and bad coding practice?
              > Or maybe put a captial letter in the typename, like
              > struct Region region, or even a
              > typedef struct region { ... } Region. I don't know,
              > so I'm grateful for answers.[/color]

              I would do

              typedef struct region Region;
              struct region
              {
              ...
              };


              August

              --
              I am the "ILOVEGNU" signature virus. Just copy me to your
              signature. This email was infected under the terms of the GNU
              General Public License.

              Comment

              • Bill Pursell

                #8
                Re: Variable and typename naming


                Walter Roberson wrote:[color=blue]
                > In article <obW_f.52532$d5 .207730@newsb.t elia.net>,
                > Edward Gregor <edwgre@hotmail .com> wrote:[color=green]
                > >In C, if I want similar names for typename and variable,
                > >is there any good way you can recommend me to do that?[/color]
                >
                > It isn't uncommon to use slightly different forms for different
                > logical layers. For example, some people might use
                >
                > struct region_s { ... };
                > typedef struct region_s region_t;
                > region_t region;[/color]
                [color=blue]
                >From the info page for libc:[/color]
                "Some additional classes of identifier names are reserved for future
                extensions to the C language or the POSIX.1 environment. While using
                these names for your own purposes right now might not cause a problem,
                they do raise the possibility of conflict with future versions of the C
                or POSIX standards, so you should avoid these names.
                [snip]
                * Names that end with `_t' are reserved for additional type names."

                Does this state that you should not roll your own type def with a
                suffix
                "_t", or by "additional type names" is it in fact referring to your own
                additional type names and recommending "_t" as a suffix?

                Is that info page relevant to the standard? (I'm getting it from a
                stock Fedora installation.)

                Comment

                • Chris Torek

                  #9
                  Re: Variable and typename naming

                  >In article <ln7j5vzmbs.fsf @nuthaus.mib.or g>,[color=blue]
                  >Keith Thompson <kst-u@mib.org> wrote:[color=green]
                  >>(Personally , I wouldn't bother with the typedef. "struct region" is a
                  >>perfectly good name for the type; there's little advantage in
                  >>inventing another name for it.)[/color][/color]

                  In article <e1ho6q$mqs$1@c anopus.cc.umani toba.ca>
                  Walter Roberson <roberson@ibd.n rc-cnrc.gc.ca> wrote:[color=blue]
                  >I know some people have posted along those lines in the past. I
                  >can't say as I agree with that viewpoint, though.
                  >
                  >A typedef can be viewed as an abstract type name: once something
                  >has been typedef'd, one can use the type name in user code without
                  >any consideration of what is "underneath the hood".[/color]

                  But so can a "struct". After all, "struct" stands for "STRange
                  spelling for User-defined abstraCt Type", as you can see from the
                  uppercased letters in the phrase. :-)

                  Note that "struct <identifier> {" actually *does* define a new
                  type (typedef does not -- in the sequence "typedef struct <id> {"
                  it is the "struct ... {" part that defines the type). This new
                  type is different from, and incompatible with, all other
                  differently-named types:

                  struct time { double val; };
                  struct temperature { double val; };
                  struct money { double val; };

                  gives you three incompatible types, so that you cannot assign a
                  "temperatur e" to a "time", for instance.

                  In addition, structs allow (but do not require) you to make the
                  type completely opaque, by omitting the "{ <contents> }" part:

                  struct schedule;

                  Now "schedule" is an opaque type; none of its members can be
                  modified. This enforces the abstract-ness: not only *should*
                  you not inspect the innards, you *can*not.

                  The one big flaw in this technique occurs in C89, where it is
                  impossible to create constants that have a structure type. This
                  is fixed in C99, with its "compound literals".
                  [color=blue]
                  >"struct region" on the other hand requires that one knows something
                  >about what is under the hood -- namely that it is a struct. At
                  >the very least, that tells you some things that the implementation
                  >-cannot- do with it, such as arithmetic operations. That level of
                  >knowledge is none of the business of the user code that just needs
                  >the name of an abstract data type.
                  >
                  >One could, I suppose, go ahead and declare that -all- abstract data
                  >types names in a program shall be struct tags, so as to put everything
                  >on an equal footing.[/color]

                  Yes; and I tend to argue for this, except where the convenience of
                  exposing the hidden mechanisms underlying the type (so that it is
                  *not* an "abstract" type after all) is sufficiently compelling.
                  If the user is allowed to "know" that the type is integral,
                  floating-point, pointer-y, or similar, and make use of that knowledge
                  (e.g., by incrementing variables of that type), then -- and only
                  then -- does it really become a candidate for "typedef"in g.
                  [color=blue]
                  >It seems to me, though, that doing so would
                  >merely increase the amount of typing, and would make it more of
                  >a nuisance at the implementation level, since one would have to
                  >put in a tag reference even if the underpinings turned out to be
                  >an arithmetic type. typedef'ing a struct tag into type name
                  >does not have those disadvantages.[/color]

                  If you write, e.g.:

                  temperature_typ e x, y, z;
                  ...
                  z = x + y;

                  you have demonstrated that "temperatur es" are not abstract after
                  all and can simply be summed (which is not generally true of
                  temperatures, except if expressed in Kelvin or Rankine or similar).
                  So I would say, instead, "using a struct tag does not have that
                  disadvantage". :-)
                  --
                  In-Real-Life: Chris Torek, Wind River Systems
                  Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                  email: forget about it http://web.torek.net/torek/index.html
                  Reading email is like searching for food in the garbage, thanks to spammers.

                  Comment

                  • pete

                    #10
                    Re: Variable and typename naming

                    Bill Pursell wrote:[color=blue]
                    >
                    > Walter Roberson wrote:[color=green]
                    > > In article <obW_f.52532$d5 .207730@newsb.t elia.net>,
                    > > Edward Gregor <edwgre@hotmail .com> wrote:[color=darkred]
                    > > >In C, if I want similar names for typename and variable,
                    > > >is there any good way you can recommend me to do that?[/color]
                    > >
                    > > It isn't uncommon to use slightly different forms for different
                    > > logical layers. For example, some people might use
                    > >
                    > > struct region_s { ... };
                    > > typedef struct region_s region_t;
                    > > region_t region;[/color]
                    >[color=green]
                    > >From the info page for libc:[/color]
                    > "Some additional classes of identifier names are reserved for future
                    > extensions to the C language or the POSIX.1 environment. While using
                    > these names for your own purposes right now might not cause a problem,
                    > they do raise the possibility of conflict with future versions of the C
                    > or POSIX standards, so you should avoid these names.
                    > [snip]
                    > * Names that end with `_t' are reserved for additional type names."
                    >
                    > Does this state that you should not roll your own type def with a
                    > suffix
                    > "_t", or by "additional type names" is it in fact referring to your own
                    > additional type names and recommending "_t" as a suffix?
                    >
                    > Is that info page relevant to the standard? (I'm getting it from a
                    > stock Fedora installation.)[/color]

                    I have enough respect for POSIX
                    to use a more spelled out "_type" suffix instead,
                    as in e_type or d_type, even though I don't anticipate
                    writing any code for POSIX in the forseeable future.

                    --
                    pete

                    Comment

                    • Herbert Rosenau

                      #11
                      Re: Variable and typename naming

                      On Tue, 11 Apr 2006 22:36:04 UTC, Edward Gregor <edwgre@hotmail .com>
                      wrote:
                      [color=blue]
                      > In C, if I want similar names for typename and variable,
                      > is there any good way you can recommend me to do that?
                      >
                      > For example, I have a struct containing a region, which
                      > I have named it, appropriately, struct region, now when I
                      > declare variables, I want to have the same, or a very
                      > similar name, since I can't come up with another good name.
                      >
                      > Now, because the different namespaces, I could declare it
                      > struct region region.
                      > But maybe that is obscure and bad coding practice?
                      > Or maybe put a captial letter in the typename, like
                      > struct Region region, or even a
                      > typedef struct region { ... } Region. I don't know,
                      > so I'm grateful for answers.[/color]

                      I'm trained in to use only capital letters for macro names and self
                      defined types.


                      --
                      Tschau/Bye
                      Herbert

                      Visit http://www.ecomstation.de the home of german eComStation
                      eComStation 1.2 Deutsch ist da!

                      Comment

                      • pemo

                        #12
                        Re: Variable and typename naming

                        Chris Torek wrote:[color=blue][color=green]
                        >> In article <ln7j5vzmbs.fsf @nuthaus.mib.or g>,
                        >> Keith Thompson <kst-u@mib.org> wrote:[/color][/color]

                        <snip>
                        [color=blue]
                        >
                        > In addition, structs allow (but do not require) you to make the
                        > type completely opaque, by omitting the "{ <contents> }" part:
                        >
                        > struct schedule;
                        >
                        > Now "schedule" is an opaque type; none of its members can be
                        > modified. This enforces the abstract-ness: not only *should*
                        > you not inspect the innards, you *can*not.[/color]

                        <snip>

                        In APIs such as those provided for Windows, opaque types were at one time
                        *not* defined as /empty structs/ - in the end-user's header files - but
                        rather as unsigned longs, e.g., a HANDLE type was typically defined as:
                        typedef unsigned long HANDLE;

                        Now, when some OS API put some value into one of these HANDLEs the
                        programmer had no idea as to its /meaning/, i.e., it could have been an
                        index into some internal OS table, a pointer to a struct, some just a
                        number, ... whatever: being /just/ a long gave very little clue as to its
                        use/implmentation.

                        However, nowadays, I think, MS exposes most of its HANDLEs like this:

                        #define DECLARE_HANDLE( name) struct name##__ { int unused; }; typedef struct
                        name##__ *name

                        Laying that out a little clearer [yes I know about the excess spaces in the
                        macro!]

                        #define DECLARE_HANDLE( name) \
                        \
                        struct name##__ \
                        { \
                        int unused; \
                        \
                        }; \
                        \
                        typedef struct name##__ * name

                        So, nowadays they/you use this like this:

                        DECLARE_HANDLE( HANDLE);

                        HANDLE hThingmy;

                        Two questions from this then.

                        1. Although a Windows' API can still return any of the list above [an index
                        into some internal OS table, a pointer to a struct, some just a number, ...]
                        in the /guise/ of *this* type of HANDLE, it does seem to me that it
                        encourage the consumer to at least /initially think/ of some handle type as
                        [probably] some sort of struct pointer - and thus, it /perhaps/ encourages
                        the same person to explore [hack] these - so, why drop the simple /disguise/
                        of using an unsigned long [or some other /plain/ type capable of holding as
                        pointer] in favour of an opaque struct?

                        2. Why does MS use an int unused member when they could simply leave the
                        struct empty - compatibility of some sort?



                        --
                        ==============
                        *Not a pedant*
                        ==============


                        Comment

                        • Chris Torek

                          #13
                          Re: Variable and typename naming

                          >Chris Torek wrote:[color=blue][color=green]
                          >> In addition, structs allow (but do not require) you to make the
                          >> type completely opaque, by omitting the "{ <contents> }" part:
                          >> struct schedule;
                          >> Now "schedule" is an opaque type; none of its members can be
                          >> modified. This enforces the abstract-ness: not only *should*
                          >> you not inspect the innards, you *can*not.[/color][/color]

                          In article <e1vt4o$27f$1@n ews.ox.ac.uk> pemo <usenetmeister@ gmail.com> wrote:[color=blue]
                          >In APIs such as those provided for Windows, opaque types were at one time
                          >*not* defined as /empty structs/ - in the end-user's header files - but
                          >rather as unsigned longs, e.g., a HANDLE type was typically defined as:
                          >typedef unsigned long HANDLE;
                          >
                          >Now, when some OS API put some value into one of these HANDLEs the
                          >programmer had no idea as to its /meaning/, i.e., it could have been an
                          >index into some internal OS table, a pointer to a struct, some just a
                          >number, ... whatever: being /just/ a long gave very little clue as to its
                          >use/implmentation.
                          >
                          >However, nowadays, I think, MS exposes most of its HANDLEs like this:[/color]
                          [edited slightly][color=blue]
                          >#define DECLARE_HANDLE( name) struct name##__ { int unused; }; \
                          >typedef struct name##__ *name[/color]
                          ...[color=blue]
                          >So, nowadays they/you use this like this:
                          >
                          >DECLARE_HANDLE (HANDLE);
                          >
                          >HANDLE hThingmy;
                          >
                          >Two questions from this then.[/color]

                          I have a third one: why bother appending the double underscore? :-)
                          [color=blue]
                          >1. [snippage ...] why drop the simple /disguise/
                          >of using an unsigned long [or some other /plain/ type capable of holding as
                          >pointer] in favour of an opaque struct?[/color]

                          I cannot answer for them, but there is no guarantee that there is
                          any appropriate integer type (even C99 does not require that there
                          *be* an "intptr_t") , and if there is, why do a lot of work to
                          dig it out when you can just use "struct opaque *"?
                          [color=blue]
                          >2. Why does MS use an int unused member when they could simply leave the
                          >struct empty - compatibility of some sort?[/color]

                          This one is easy enough: an empty struct -- struct foo {} -- is not
                          syntactically valid. If you meant "why not just leave the type
                          incomplete", that I cannot answer.
                          --
                          In-Real-Life: Chris Torek, Wind River Systems
                          Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                          email: forget about it http://web.torek.net/torek/index.html
                          Reading email is like searching for food in the garbage, thanks to spammers.

                          Comment

                          • Dave Thompson

                            #14
                            Re: Variable and typename naming

                            On Mon, 17 Apr 2006 12:11:20 +0100, "pemo" <usenetmeister@ gmail.com>
                            wrote:
                            <snip>[color=blue]
                            > However, nowadays, I think, MS exposes most of its HANDLEs like this:
                            >
                            > #define DECLARE_HANDLE( name) struct name##__ { int unused; }; typedef struct
                            > name##__ *name[/color]
                            [color=blue]
                            > So, nowadays they/you use this like this:
                            >
                            > DECLARE_HANDLE( HANDLE);
                            >
                            > HANDLE hThingmy;
                            >
                            > Two questions from this then.
                            >
                            > 1. Although a Windows' API can still return any of the list above [an index
                            > into some internal OS table, a pointer to a struct, some just a number, ...]
                            > in the /guise/ of *this* type of HANDLE, it does seem to me that it
                            > encourage the consumer to at least /initially think/ of some handle type as
                            > [probably] some sort of struct pointer - and thus, it /perhaps/ encourages
                            > the same person to explore [hack] these - so, why drop the simple /disguise/
                            > of using an unsigned long [or some other /plain/ type capable of holding as
                            > pointer] in favour of an opaque struct?
                            >[/color]
                            Perhaps because struct uniquetag ... is a new type in C, whereas
                            typedef anything is not. This allows/requires the compiler to detect
                            if you try to misuse one type of handle (pointer) to a different one.
                            [color=blue]
                            > 2. Why does MS use an int unused member when they could simply leave the
                            > struct empty - compatibility of some sort?[/color]

                            Standard C doesn't allow a struct (or union) with no members. <OT>C++
                            does, and has sensible uses for struct/class with no explicit members
                            and/or no data members at all.</> They don't need or want
                            Windows-specific declarations portable to other systems, but might
                            want them to work on other Windows-targetable compiler(s) like GCC.

                            - David.Thompson1 at worldnet.att.ne t

                            Comment

                            Working...