question about code style specifically variable declaration using _ prefix

Discussion in 'C++' started by gara.matt@gmail.com, Jul 20, 2007.

  1. Guest

    Hi,

    I was wondering, I've been reading some C++ code written by others,
    usually libraries and stuff, and I've come across what strikes me as a
    distinctive style that pervades most of the code I've been reading.
    What I'm talking about is that some variables are declared with a "_"
    prefix while others lack it. For example you may find a variable and
    even functions like

    int _temp

    and one like

    int temp

    >From a Java perspective, this style is quite new to me, seeing as the

    Java compiler does not accept variables of the aforementioned type. I
    have, however, seen this used also in C, but not to the same degree,
    and I have ignored it. But it has come to a point where I see it so
    much that I think it carries some kind of significance such that if I
    understood then I could grasp code quicker.

    Explain away, or BS away, I'm willing to listen.

    Thanks,

    Matt
    , Jul 20, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    Re: question about code style specifically variable declaration using_ prefix

    wrote:
    > Hi,
    >
    > I was wondering, I've been reading some C++ code written by others,
    > usually libraries and stuff, and I've come across what strikes me as a
    > distinctive style that pervades most of the code I've been reading.
    > What I'm talking about is that some variables are declared with a "_"
    > prefix while others lack it. For example you may find a variable and
    > even functions like
    >
    > int _temp
    >
    > and one like
    >
    > int temp
    >

    Names with either a leading underscore followed by a capital letter or
    two leading underscores are reserved for the implementation. That's why
    you are seeing them in libraries.

    It's common practice to avoid all names with a single leading underscore
    in application code.

    --
    Ian Collins.
    Ian Collins, Jul 20, 2007
    #2
    1. Advertising

  3. Re: question about code style specifically variable declaration using_ prefix

    On 2007-07-20 09:59, wrote:
    > Hi,
    >
    > I was wondering, I've been reading some C++ code written by others,
    > usually libraries and stuff, and I've come across what strikes me as a
    > distinctive style that pervades most of the code I've been reading.
    > What I'm talking about is that some variables are declared with a "_"
    > prefix while others lack it. For example you may find a variable and
    > even functions like
    >
    > int _temp
    >
    > and one like
    >
    > int temp
    >
    >>From a Java perspective, this style is quite new to me, seeing as the

    > Java compiler does not accept variables of the aforementioned type. I
    > have, however, seen this used also in C, but not to the same degree,
    > and I have ignored it. But it has come to a point where I see it so
    > much that I think it carries some kind of significance such that if I
    > understood then I could grasp code quicker.
    >
    > Explain away, or BS away, I'm willing to listen.


    If you have read code mostly from libraries, and especially system
    libraries, this is understandable and could be considered good (or not)
    but if they are not system libraries it's probably not a good idea. The
    C++ standard specifies that all names beginning with __ (two
    underscores) are reserved for the implementation, this means that if you
    write an applications you should *never ever* use names that starts with
    two underscores. Names starting with _ (a single underscore) however are
    reserved for the implementation only in certain circumstances (such as
    in the global namespace, or followed by an upper case letter).

    So the question is whether your code can be considered part of the
    implementation, in most cases this is not so but system libraries can
    sometimes be considered as such. However since C++ supports namespaces
    there's really never a need to use identifiers starting with underscore
    and since it can sometimes lead to trouble the general advice seems to
    be to avoid using them at all.


    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jul 20, 2007
    #3
  4. Erik Wikström wrote:

    > On 2007-07-20 09:59, wrote:
    >> Hi,
    >>
    >> I was wondering, I've been reading some C++ code written by others,
    >> usually libraries and stuff, and I've come across what strikes me as a
    >> distinctive style that pervades most of the code I've been reading.
    >> What I'm talking about is that some variables are declared with a "_"
    >> prefix while others lack it. For example you may find a variable and
    >> even functions like
    >>
    >> int _temp
    >>
    >> and one like
    >>
    >> int temp
    >>
    >>>From a Java perspective, this style is quite new to me, seeing as the

    >> Java compiler does not accept variables of the aforementioned type. I
    >> have, however, seen this used also in C, but not to the same degree,
    >> and I have ignored it. But it has come to a point where I see it so
    >> much that I think it carries some kind of significance such that if I
    >> understood then I could grasp code quicker.
    >>
    >> Explain away, or BS away, I'm willing to listen.

    >
    > If you have read code mostly from libraries, and especially system
    > libraries, this is understandable and could be considered good (or not)
    > but if they are not system libraries it's probably not a good idea. The
    > C++ standard specifies that all names beginning with __ (two
    > underscores) are reserved for the implementation, this means that if you
    > write an applications you should *never ever* use names that starts with
    > two underscores.


    Any name _containing_ two sequential underscores are reserved to the
    implementation. It may be at the beginning, but also in the middle or at
    the end. It doesn't really matter if it is in a nested namespace either.

    > Names starting with _ (a single underscore) however are
    > reserved for the implementation only in certain circumstances (such as
    > in the global namespace, or followed by an upper case letter).


    If followed by an uppercase letter, the name is reserved everywhere. Names
    beginning with an underscore followed by a lowercase letter are reserved in
    the global namespace, just as any name from the C standard library is.

    --
    rbh
    Robert Bauck Hamar, Jul 20, 2007
    #4
  5. Re: question about code style specifically variable declaration using_ prefix

    On 2007-07-20 10:41, Robert Bauck Hamar wrote:
    > Erik Wikström wrote:
    >
    >> On 2007-07-20 09:59, wrote:
    >>> Hi,
    >>>
    >>> I was wondering, I've been reading some C++ code written by others,
    >>> usually libraries and stuff, and I've come across what strikes me as a
    >>> distinctive style that pervades most of the code I've been reading.
    >>> What I'm talking about is that some variables are declared with a "_"
    >>> prefix while others lack it. For example you may find a variable and
    >>> even functions like
    >>>
    >>> int _temp
    >>>
    >>> and one like
    >>>
    >>> int temp
    >>>
    >>>>From a Java perspective, this style is quite new to me, seeing as the
    >>> Java compiler does not accept variables of the aforementioned type. I
    >>> have, however, seen this used also in C, but not to the same degree,
    >>> and I have ignored it. But it has come to a point where I see it so
    >>> much that I think it carries some kind of significance such that if I
    >>> understood then I could grasp code quicker.
    >>>
    >>> Explain away, or BS away, I'm willing to listen.

    >>
    >> If you have read code mostly from libraries, and especially system
    >> libraries, this is understandable and could be considered good (or not)
    >> but if they are not system libraries it's probably not a good idea. The
    >> C++ standard specifies that all names beginning with __ (two
    >> underscores) are reserved for the implementation, this means that if you
    >> write an applications you should *never ever* use names that starts with
    >> two underscores.

    >
    > Any name _containing_ two sequential underscores are reserved to the
    > implementation. It may be at the beginning, but also in the middle or at
    > the end. It doesn't really matter if it is in a nested namespace either.


    Oh, you're right. Lucky that I've never used it (not that I can thing of
    any situation where putting two underscore in a name might seem like a
    good idea).

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Jul 20, 2007
    #5
  6. Re: question about code style specifically variable declaration using_ prefix

    wrote:
    > Hi,
    >
    > I was wondering, I've been reading some C++ code written by others,
    > usually libraries and stuff, and I've come across what strikes me as a
    > distinctive style that pervades most of the code I've been reading.
    > What I'm talking about is that some variables are declared with a "_"
    > prefix while others lack it. For example you may find a variable and
    > even functions like
    >
    > int _temp
    >
    > and one like
    >
    > int temp
    >
    >>From a Java perspective, this style is quite new to me, seeing as the

    > Java compiler does not accept variables of the aforementioned type. I
    > have, however, seen this used also in C, but not to the same degree,
    > and I have ignored it. But it has come to a point where I see it so
    > much that I think it carries some kind of significance such that if I
    > understood then I could grasp code quicker.
    >
    > Explain away, or BS away, I'm willing to listen.
    >
    > Thanks,
    >
    > Matt
    >


    I use that style to indicate member variables. I think it's commonly
    accepted that it's good to distinguish member variables from others
    (even in Java) but I'm often criticised here for my particular way of
    doing it. I don't care, it's correct and I like it.

    John
    John Harrison, Jul 21, 2007
    #6
  7. Bo Persson Guest

    John Harrison wrote:
    :: wrote:
    ::: Hi,
    :::
    ::: I was wondering, I've been reading some C++ code written by
    ::: others, usually libraries and stuff, and I've come across what
    ::: strikes me as a distinctive style that pervades most of the code
    ::: I've been reading. What I'm talking about is that some variables
    ::: are declared with a "_" prefix while others lack it. For example
    ::: you may find a variable and even functions like
    :::
    ::: int _temp
    :::
    ::: and one like
    :::
    ::: int temp
    :::
    :::: From a Java perspective, this style is quite new to me, seeing
    :::: as the
    ::: Java compiler does not accept variables of the aforementioned
    ::: type. I have, however, seen this used also in C, but not to the
    ::: same degree, and I have ignored it. But it has come to a point
    ::: where I see it so much that I think it carries some kind of
    ::: significance such that if I understood then I could grasp code
    ::: quicker.
    :::
    ::: Explain away, or BS away, I'm willing to listen.
    :::
    ::: Thanks,
    :::
    ::: Matt
    :::
    ::
    :: I use that style to indicate member variables. I think it's
    :: commonly accepted that it's good to distinguish member variables
    :: from others (even in Java) but I'm often criticised here for my
    :: particular way of doing it. I don't care, it's correct and I like
    :: it.

    I guess the criticism is that, although correct, a leading underscore
    indicates that a name is now *either* a member variable, or a global
    implementation-specific name, or possibly both.

    I understand that some of your colleagues can be a bit confused by
    this. :)


    Bo Persson
    Bo Persson, Jul 21, 2007
    #7
  8. Re: question about code style specifically variable declaration using_ prefix

    On 2007-07-21 13:01, Bo Persson wrote:
    > John Harrison wrote:
    > :: wrote:
    > ::: Hi,
    > :::
    > ::: I was wondering, I've been reading some C++ code written by
    > ::: others, usually libraries and stuff, and I've come across what
    > ::: strikes me as a distinctive style that pervades most of the code
    > ::: I've been reading. What I'm talking about is that some variables
    > ::: are declared with a "_" prefix while others lack it. For example
    > ::: you may find a variable and even functions like
    > :::
    > ::: int _temp
    > :::
    > ::: and one like
    > :::
    > ::: int temp
    > :::
    > :::: From a Java perspective, this style is quite new to me, seeing
    > :::: as the
    > ::: Java compiler does not accept variables of the aforementioned
    > ::: type. I have, however, seen this used also in C, but not to the
    > ::: same degree, and I have ignored it. But it has come to a point
    > ::: where I see it so much that I think it carries some kind of
    > ::: significance such that if I understood then I could grasp code
    > ::: quicker.
    > :::
    > ::: Explain away, or BS away, I'm willing to listen.
    > :::
    > ::: Thanks,
    > :::
    > ::: Matt
    > :::
    > ::
    > :: I use that style to indicate member variables. I think it's
    > :: commonly accepted that it's good to distinguish member variables
    > :: from others (even in Java) but I'm often criticised here for my
    > :: particular way of doing it. I don't care, it's correct and I like
    > :: it.
    >
    > I guess the criticism is that, although correct, a leading underscore
    > indicates that a name is now *either* a member variable, or a global
    > implementation-specific name, or possibly both.


    And the risk that someday a less knowledgeable programmer will come and
    make a change to your code and follow your style but uses a capital
    letter as first letter. One of the main reasons it'd discouraged is
    probably to prevent people who don't know better from screwing up in
    hard-to-debug ways.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jul 21, 2007
    #8
  9. James Kanze Guest

    On Jul 21, 1>01 pm, "Bo Persson" <> wrote>
    > John Harrison wrote>
    > >> wrote>


    > >>> I was wondering, I've been reading some C++ code written by
    > >>> others, usually libraries and stuff, and I've come across what
    > >>> strikes me as a distinctive style that pervades most of the code
    > >>> I've been reading. What I'm talking about is that some variables
    > >>> are declared with a "_" prefix while others lack it. For example
    > >>> you may find a variable and even functions like


    > >>> int _temp


    > >>> and one like


    > >>> int temp


    > >>> From a Java perspective, this style is quite new to me,
    > >>> seeing as the Java compiler does not accept variables of
    > >>> the aforementioned type.


    This is, of course, false. Java is even more liberal than C++,
    as it not only allows names to begin with either a _ or $ (the
    latter is illegal in C++), but doesn't reserve any of them for
    the implementation.

    > >>> I have, however, seen this used also in C, but not to the
    > >>> same degree, and I have ignored it. But it has come to a point
    > >>> where I see it so much that I think it carries some kind of
    > >>> significance such that if I understood then I could grasp code
    > >>> quicker.


    > >>> Explain away, or BS away, I'm willing to listen.


    > >> I use that style to indicate member variables. I think it's
    > >> commonly accepted that it's good to distinguish member variables
    > >> from others (even in Java) but I'm often criticised here for my
    > >> particular way of doing it. I don't care, it's correct and I like
    > >> it.


    > I guess the criticism is that, although correct, a leading underscore
    > indicates that a name is now *either* a member variable, or a global
    > implementation-specific name, or possibly both.


    Not really. The criticism is that, regardless of what the
    standard says, system headers commonly DO define macros which
    start with a _, and assume that this will not cause problems.
    Using a _ prefix is just asking for trouble.

    In general, as others have pointed out, library code intended to
    be considered part of the implementation will make extensive use
    of such symbols, precisely because the user is not allowed to
    define them.

    Outside of such library code, it seems pretty universal to
    distinguish member variables from other names. In principle, if
    you name well, it shouldn't be necessary, but such conventions
    certainly don't do any harm. In cases where the variable
    represents an externally visible attribute, they also allow the
    setter/getter functions to have the name of the attribute. I've
    seen at least four different conventions for this:
    _member
    member_
    m_member
    myMember
    The first is, as I've just said, looking for trouble. The
    second is, IMHO, just too ugly. The latter two also have the
    advantage of extending easily to distinguish between static
    members (s_member, ourMember) and non-static members. I use the
    last in my own code; I like the fact that the results are
    pronouceable. But I'll willingly either, according to the
    requirements of the local standards.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 22, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Bedford
    Replies:
    1
    Views:
    6,917
    Chris Bedford
    Oct 6, 2003
  2. Tim Tyler
    Replies:
    36
    Views:
    1,462
    Darryl L. Pierce
    Dec 10, 2004
  3. Ian Giblin
    Replies:
    4
    Views:
    589
    Dave Townsend
    Nov 15, 2004
  4. Chris  Chiasson
    Replies:
    6
    Views:
    614
    Richard Tobin
    Nov 14, 2006
  5. Bolin
    Replies:
    4
    Views:
    408
Loading...

Share This Page