C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

names with leading underscores
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Gene Bushuyev
Guest





PostPosted: Thu Oct 27, 2005 4:41 pm    Post subject: names with leading underscores Reply with quote



What do your coding standards say about using names with leading underscores
followed by a lowercase letter? Do they
prohibit/limit/allow/encourage/prescribe using them? If so, what rational is
given and are any examples provided?

We know that the blessed standard reserves such names for implementation in
the global and std namespaces (17.4.3.1.2). Does it mean that it's
reasonable to allow/prescribe using them in other places? One coding
standard requires using those names for non-public class data members. I
think the wisdom of that decision is questionable, and the code below
demonstrates that it can be error-prone. It was intended to print 123,
instead it prints 121. It's really easy to overlook template lookup problem,
and no known to me compiler warns about it. Here is a simplified code,

#include <iostream>

// say _one is defined by implementation
// in the global namespace

int _one = 1;

// a template class also using _name
// convention for members

template<class T>
struct A
{
T _one;
A(T i) : _one(i) {}
void print() const { std::cout << _one; }
};

// derived class overlooked the lookup
// problem

template struct B : A<T>
{
B(T t) : A<T>(t) {}
void print() const { std::cout << _one; }
};

int main(int argc, char* argv[])
{
std::cout << _one; // prints 1
A B<int>(3).print(); // prints 1
}

--
Gene Bushuyev ~ Cadence Design Systems
http://www.cadence.com/company/index.aspx


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
nickelplate
Guest





PostPosted: Fri Oct 28, 2005 11:34 am    Post subject: Re: names with leading underscores Reply with quote



I avoid using leading underscores in front of *anything* in my code,
just to be on the safe side. A more sensible convention for non-public
class variables would be a leading "m" or "m_", or a trailing
underscore.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Heinz Ozwirk
Guest





PostPosted: Sat Oct 29, 2005 12:49 am    Post subject: Re: names with leading underscores Reply with quote



"Gene Bushuyev" <blackhole (AT) spamguard (DOT) com> schrieb im Newsbeitrag
news:zFV7f.7192$7h7.6189 (AT) newssvr21 (DOT) news.prodigy.com...
Quote:
What do your coding standards say about using names with leading
underscores
followed by a lowercase letter? Do they
prohibit/limit/allow/encourage/prescribe using them? If so, what rational
is
given and are any examples provided?

We know that the blessed standard reserves such names for implementation
in
the global and std namespaces (17.4.3.1.2). Does it mean that it's
reasonable to allow/prescribe using them in other places? One coding
standard requires using those names for non-public class data members. I
think the wisdom of that decision is questionable, and the code below
demonstrates that it can be error-prone. It was intended to print 123,
instead it prints 121. It's really easy to overlook template lookup
problem,
and no known to me compiler warns about it. Here is a simplified code,

#include <iostream

// say _one is defined by implementation
// in the global namespace

int _one = 1;

// a template class also using _name
// convention for members

template struct A
{
T _one;
A(T i) : _one(i) {}
void print() const { std::cout << _one; }
};

// derived class overlooked the lookup
// problem

template struct B : A {
B(T t) : A void print() const { std::cout << _one; }
};

int main(int argc, char* argv[])
{
std::cout << _one; // prints 1
A B<int>(3).print(); // prints 1
}

Whatever might be the reason why your program compiled with your compiler
outputs the wrong result, is no argument for or against a leading underscore
in an identifier. With such a broken compiler you would get the same result
if you'd replace all occurences of "_one" by some other identifier like
"two".

Heinz



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Allan W
Guest





PostPosted: Sat Oct 29, 2005 1:30 am    Post subject: Re: names with leading underscores Reply with quote

Gene Bushuyev wrote:
[SNIP]
Quote:
the code below
demonstrates that it can be error-prone. It was intended to print 123,
instead it prints 121. It's really easy to overlook template lookup problem,
and no known to me compiler warns about it. Here is a simplified code,

#include <iostream

// say _one is defined by implementation
// in the global namespace
int _one = 1;

// a template class also using _name
// convention for members
template struct A
{
T _one;
A(T i) : _one(i) {}
void print() const { std::cout << _one; }
};

// derived class overlooked the lookup
// problem
template struct B : A {
B(T t) : A void print() const { std::cout << _one; }
};

int main(int argc, char* argv[])
{
std::cout << _one; // prints 1
A B<int>(3).print(); // prints 1
}

But this *DOES* print 123 on my computer (Visual Studio .Net 2003).

Furthermore, I don't see what this has to do with names starting with
an underscore... change all occurrences of "_one" to "one" and it
will work exactly the same way...


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Sat Oct 29, 2005 1:07 pm    Post subject: Re: names with leading underscores Reply with quote

Gene Bushuyev wrote:
Quote:
What do your coding standards say about using names with
leading underscores followed by a lowercase letter?

Most of the ones I'm used to forbid underscores anywhere.
CamelCase seems to be the in thing.

Quote:
Do they prohibit/limit/allow/encourage/prescribe using them?
If so, what rational is given and are any examples provided?

We know that the blessed standard reserves such names for
implementation in the global and std namespaces (17.4.3.1.2).

The problem is that most code needs more than the standard
provides. I've seen _[a-z].* as macros in both Windows and Unix
headers, for example, and would avoid it in any code that I
wrote.

Quote:
Does it mean that it's reasonable to allow/prescribe using
them in other places?

One coding standard requires using those names for non-public
class data members. I think the wisdom of that decision is
questionable, and the code below demonstrates that it can be
error-prone. It was intended to print 123, instead it prints
121. It's really easy to overlook template lookup problem, and
no known to me compiler warns about it. Here is a simplified
code,

#include <iostream

// say _one is defined by implementation
// in the global namespace

int _one = 1;

// a template class also using _name
// convention for members

template struct A
{
T _one;
A(T i) : _one(i) {}
void print() const { std::cout << _one; }
};

// derived class overlooked the lookup
// problem

template struct B : A {
B(T t) : A void print() const { std::cout << _one; }
};

int main(int argc, char* argv[])
{
std::cout << _one; // prints 1
A B<int>(3).print(); // prints 1
}

Well, it's a rather contrived example, IMHO. But it is still
another argument not to use this convention. The fact that in
certain circomstances, you may end up with a macro with such a
name is an ever stronger reason. As is the fact that it's
unpronounciable.

For members, my and our are my prefered convention when
CamelCase is used, e.g. myOne, or ourOne if it is a static
member. When CamelCase is not used, I've also seen m_ and s_ in
these circumstances. And the convetion of putting an underscore
at the end (which I find perfectly horrible).

--
James Kanze GABI Software
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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
sting
Guest





PostPosted: Sat Oct 29, 2005 7:41 pm    Post subject: Re: names with leading underscores Reply with quote

It seems to me namespaces would be of great use in this case. By the
way, if the C++ standard library would define a global, this global
would most certainly be within the std namespace, wouldn't it?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gene Bushuyev
Guest





PostPosted: Sat Oct 29, 2005 8:11 pm    Post subject: Re: names with leading underscores Reply with quote

"Allan W" <allan_w (AT) my-dejanews (DOT) com> wrote

[...]
Quote:
But this *DOES* print 123 on my computer (Visual Studio .Net 2003).

But it's only because the compiler is broken.

Quote:

Furthermore, I don't see what this has to do with names starting with
an underscore... change all occurrences of "_one" to "one" and it
will work exactly the same way...


I hoped I explained the problem in my original post ... Well, here it goes
again. Names that start with underscore followed by lower case character are
reserved in global and std:: namespaces: they are used by compiler and library
implementation. Thus an easy to make mistake like the one I showed that
otherwise would be caught by compiler goes unnoticed.
I found more arguments in a few years old discussion
([url]http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/bb2d5f9ed7e3246c/55f1599562383537)[/url],
so it doesn't make much sense rehashing it again unless somebody got a new
argument.

-- Gene Bushuyev
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Valentin Samko
Guest





PostPosted: Sat Oct 29, 2005 8:11 pm    Post subject: Re: names with leading underscores Reply with quote

Gene Bushuyev wrote:
Quote:
#include <iostream
int _one = 1;
template struct A
{
T _one;
A(T i) : _one(i) {}
void print() const { std::cout << _one; }
};
template struct B : A {
B(T t) : A void print() const { std::cout << _one; }
};
int main(int argc, char* argv[])
{
std::cout << _one; // prints 1
A B<int>(3).print(); // prints 1
}

Yes, the compiler (which implements a two phase lookup) should print 1 2 1, but
I do not
see what this has to do with the leading underscore. If you want it to print 1 2
3, then
in class B replace
void print() const { std::cout << _one; }
with
void print() const { std::cout << this->_one; }

The problem here is that when the compiler parses the class B, it does not know
the class
T yet, and it can not assume anything about class A, as it can be specialised
later. So,
it can not see _one in class A<T>. So, unless you explicitly tell it to use _one
in the
parent (by this->_one), it will find _one in the global scope.

--

Valentin Samko - http://www.valentinsamko.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gene Bushuyev
Guest





PostPosted: Sat Oct 29, 2005 8:12 pm    Post subject: Re: names with leading underscores Reply with quote

"Heinz Ozwirk" <hozwirk.SPAM (AT) arcor (DOT) de> wrote

[...]
Quote:

Whatever might be the reason why your program compiled with your compiler
outputs the wrong result, is no argument for or against a leading underscore
in an identifier. With such a broken compiler you would get the same result
if you'd replace all occurences of "_one" by some other identifier like
"two".


I didn't quite understand what you tried to say. If you meant to say that there
were some flaws in the logic or the example presented, just say exactly what. If
you compiler produces something different than what I indicated, that is because
it doesn't implement lookup correctly.

-- Gene Bushuyev
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gene Bushuyev
Guest





PostPosted: Sat Oct 29, 2005 8:17 pm    Post subject: Re: names with leading underscores Reply with quote

"kanze" <kanze (AT) gabi-soft (DOT) fr> wrote

Quote:
Gene Bushuyev wrote:
[...]
We know that the blessed standard reserves such names for
implementation in the global and std namespaces (17.4.3.1.2).

The problem is that most code needs more than the standard
provides. I've seen _[a-z].* as macros in both Windows and Unix
headers, for example, and would avoid it in any code that I
wrote.

Yes, I found an old discussion where Steve Clamage basically said the same. It's
a good argument.

[...]
Quote:
Well, it's a rather contrived example, IMHO. But it is still
another argument not to use this convention. The fact that in

It is contrived. The idea was to demonstrate that even within the limits of the
standard, a frequent mistake like overlooking two-stage lookup can lead to
undiagnosed errors. I need to respond to the claim that as long as we don't use
underhanded names in global scope they are as safe as "normal" names.

Quote:
certain circomstances, you may end up with a macro with such a
name is an ever stronger reason. As is the fact that it's
unpronounciable.

For members, my and our are my prefered convention when
CamelCase is used, e.g. myOne, or ourOne if it is a static
member. When CamelCase is not used, I've also seen m_ and s_ in
these circumstances. And the convetion of putting an underscore
at the end (which I find perfectly horrible).

I personally feel all those warts are unpleasant for an eye and an ear. I used
to use trailing underscores and I never particularly liked them. Now I'm
questioning whether there is a need to embellish names at all. Firstly, that
information is not particularly useful. Secondly, a good IDE will provide that
information on the fly. For example, IDE can color names depending on the
context using background parser. In VC7.1 IDE a popup window provides the
information about the type when the cursor is hovering over the name.
Interesting that in the example I showed, VC popup window correctly shows the
global name, but compiler looks up in the base class instead.

-- Gene Bushuyev
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gene Bushuyev
Guest





PostPosted: Sun Oct 30, 2005 1:20 pm    Post subject: Re: names with leading underscores Reply with quote

"Valentin Samko" <c++.moderated (AT) digiways (DOT) com> wrote

[...]
Quote:
Yes, the compiler (which implements a two phase lookup) should print 1 2 1,
but
I do not
see what this has to do with the leading underscore. If you want it to print 1
2
3, then

Because 1) the two-phase lookup errors are common; 2) they are normally caught
by compiler, but if there is the same global _name, the error may go unnoticed.
For example, some compilers provide _exit() global function. If the example had
an _exit member, compiler wouldn't complain, just code would do something
unexpected.

-- Gene Bushuyev
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Valentin Samko
Guest





PostPosted: Sun Oct 30, 2005 3:00 pm    Post subject: Re: names with leading underscores Reply with quote

Gene Bushuyev wrote:

Quote:
Yes, the compiler (which implements a two phase lookup) should print 1 2 1,
but
I do not
see what this has to do with the leading underscore. If you want it to print 1
2
3, then

Because
1) the two-phase lookup errors are common;
I wouldn't say that they are more common than other types of errors, it is just that not

all the C++ developers are familiar with the two phase lookup, and so people tend not to
notice these types of errors even when reviewing someone else's code.

Quote:
2) they are normally caught
by compiler, but if there is the same global _name, the error may go unnoticed.

I still do not see what this has to do with the leading underscores. The error may go
unnoticed if that variable is already visible. It does not matter whether it was declared
by the standard library (and brought into your scope), by a 3rd party library, or by your
own code in a header file you accidentally included (maybe indirectly).

Quote:
For example, some compilers provide _exit() global function. If the example had
an _exit member, compiler wouldn't complain, just code would do something
unexpected.
And there's abort() function without a leading underscore. So, you would get into the same

trouble if you had "abort" there.

I understand the problem you are talking about, and it would be nice if compiler had an
option to generate warnings in such cases, but I don't understand how the case of leading
underscores is any special.

--

Valentin Samko - http://www.valentinsamko.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gene Bushuyev
Guest





PostPosted: Mon Oct 31, 2005 10:50 am    Post subject: Re: names with leading underscores Reply with quote

"Valentin Samko" <c++.moderated (AT) digiways (DOT) com> wrote

Quote:
Gene Bushuyev wrote:
[...]
Because
1) the two-phase lookup errors are common;
I wouldn't say that they are more common than other types of errors, it is
just that not
all the C++ developers are familiar with the two phase lookup, and so people
tend not to
notice these types of errors even when reviewing someone else's code.

Unfortunately, they are. And compilers are not upto standard yet. Just look at
the first responses to the original post. In my practice I see many developers
having "huh?" written on their faces when compiler reports lookup errors.

Quote:

2) they are normally caught
by compiler, but if there is the same global _name, the error may go
unnoticed.

I still do not see what this has to do with the leading underscores. The error
may go
unnoticed if that variable is already visible. It does not matter whether it
was declared
by the standard library (and brought into your scope), by a 3rd party library,
or by your
own code in a header file you accidentally included (maybe indirectly).

But the global names in user code do not have the embellishments the members do.
If say trailing underscore is used for members, there is no global name_
variable and thus there is no danger of this error slipping unnoticed. This only
happens with leading underscores exactly because compiler/library may use them
in the global scope.

-- Gene Bushuyev
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
James Kanze
Guest





PostPosted: Mon Oct 31, 2005 10:53 am    Post subject: Re: names with leading underscores Reply with quote

sting wrote:

Quote:
It seems to me namespaces would be of great use in this case.
By the way, if the C++ standard library would define a global,
this global would most certainly be within the std namespace,
wouldn't it?

You mean like errno and NULL? Or for that matter, the operator
new function.

Most symbols in the C++ library are in std. But there are
exceptions, for historical reasons. And some (also for
historical reasons) are macros.

Still, the question here didn't concern so much symbols defined
by the C++ standard, but by the implementation. And for various
reasons, these might not be the std namespace. (To begin with,
in the implementations I'm familiar with, most of the
implementation headers are shared with C. Which pretty much
means that they don't use namespaces.)

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
James Kanze
Guest





PostPosted: Mon Oct 31, 2005 10:53 am    Post subject: Re: names with leading underscores Reply with quote

Gene Bushuyev wrote:
Quote:
"kanze" <kanze (AT) gabi-soft (DOT) fr> wrote in message
news:1130514790.390819.49200 (AT) g44g2000cwa (DOT) googlegroups.com...

[...]
Quote:
For members, my and our are my prefered convention when
CamelCase is used, e.g. myOne, or ourOne if it is a static
member. When CamelCase is not used, I've also seen m_ and s_
in these circumstances. And the convetion of putting an
underscore at the end (which I find perfectly horrible).

I personally feel all those warts are unpleasant for an eye
and an ear. I used to use trailing underscores and I never
particularly liked them. Now I'm questioning whether there is
a need to embellish names at all. Firstly, that information is
not particularly useful. Secondly, a good IDE will provide
that information on the fly. For example, IDE can color names
depending on the context using background parser. In VC7.1 IDE
a popup window provides the information about the type when
the cursor is hovering over the name. Interesting that in the
example I showed, VC popup window correctly shows the global
name, but compiler looks up in the base class instead.

I'm of two minds myself. Logically, a good name should say it
all; if you need such "warts", your names aren't good enough.
Practically...

-- being lazy, I tend to name the member and the parameter both
state (or state and myState), rather than currentState and
newState, which is really what they are (and it is rather
obvious, in a member function setState, which one is the
member, and which the parameter).

-- Rose (and doubtlessly other tools) has mechanisms for
automatically generating the code is you request it, but you
need to define the naming conventions, because *it* doesn't
automatically know what adjective would best apply.

-- The killer: my customers want it, and insist on it. And
since they pay the bills. If they insist on something
"wrong", like the prefix underscore, I'll argue strongly
against it, and try to change their mind. If the insist on
something that is simply ugly, like the post-fix underscore,
I'll complain somewhat about the esthetics and point out the
alternatives, but I won't insist. If they user my... or
m_..., I'll just go along, without saying anything.

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.