473,320 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Looking for good naming convention for class attributes

For many years I have been following the convention of naming all
class attributes with a leading underscore. This enables me to
quickly identify the class attributes when I encounter them in the
source code. Even the GoF book follows this convention, although it's
true that the GoF book is not an authoritative source for coding
guidelines.

Recently a colleague remarked that using a leading underscore is a bad
programming practice that should be avoided at all costs. According
to her this has to do with the fact that the compiler naming mangling
procedure also sticks leading underscores and therefore using
underscores in the source code makes the job of following the
post-processed source code (should there ever be a need) very
difficult.

Any comments?

Regards,
Sandeep
Jul 22 '05 #1
9 2309
Sandeep Sharma wrote:
For many years I have been following the convention of naming all
class attributes with a leading underscore. This enables me to
quickly identify the class attributes when I encounter them in the
source code. Even the GoF book follows this convention, although it's
true that the GoF book is not an authoritative source for coding
guidelines.

Recently a colleague remarked that using a leading underscore is a bad
programming practice that should be avoided at all costs. According
to her this has to do with the fact that the compiler naming mangling
procedure also sticks leading underscores and therefore using
underscores in the source code makes the job of following the
post-processed source code (should there ever be a need) very
difficult.

Any comments?

Regards,
Sandeep


It's not just the (relatively obscure) issue you mention. You're not
allowed to use leading underscores; they're reserved for the
implementation, per section 17.4.3.1.2 of the standard. You see lots of
leading underscores in standard library code; using leading _ yourself
can interfere with the library, and certainly isn't portable.

Jul 22 '05 #2
"Jeff Schwab" wrote...
For many years I have been following the convention of
naming all class attributes with a leading underscore.
This enables me to quickly identify the class attributes
when I encounter them in the source code. Even the GoF
book follows this convention, although it's true that
the GoF book is not an authoritative source for coding
guidelines.

Recently a colleague remarked that using a leading
underscore is a bad programming practice that should be
avoided at all costs. According to her this has to do
with the fact that the compiler naming mangling procedure
also sticks leading underscores and therefore using
underscores in the source code makes the job of following
the post-processed source code (should there ever be a
need) very difficult.

Any comments?

Regards, Sandeep


As Jeff already pointed out, the leading underscore is reserved by the
implementation. Using leading underscores is not only bad practice
but also not permitted by the standard. These days many books prefer
the *trailing* underscore:

struct X
{
int someDataMember_;
};

I don't like this convention and prefer an m_ prefix (despite it's
affiliation with Microsoft):

struct Y
{
int m_someDataMember;
};
Jul 22 '05 #3
Derek wrote in news:bu************@ID-46268.news.uni-berlin.de:

As Jeff already pointed out, the leading underscore is reserved by the
implementation. Using leading underscores is not only bad practice
but also not permitted by the standard. These days many books prefer
the *trailing* underscore:


Using a leading underscore is leagal as long as:

A) it isn't followed by another underscore,
B) it isn't followed by an uppercase letter,
C) it doesn't declare an identifier in the global namespace.

Additionally identifiers with 2 consecutive underscores are
allways reserved (hence (A) above).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
Rob Williscroft wrote:
Derek wrote in news:bu************@ID-46268.news.uni-berlin.de:

As Jeff already pointed out, the leading underscore is reserved by the
implementation. Using leading underscores is not only bad practice
but also not permitted by the standard. These days many books prefer
the *trailing* underscore:

Using a leading underscore is leagal as long as:

A) it isn't followed by another underscore,
B) it isn't followed by an uppercase letter,
C) it doesn't declare an identifier in the global namespace.

Additionally identifiers with 2 consecutive underscores are
allways reserved (hence (A) above).

Rob.


Excuse me, you're quite right. Leading underscores inside namespaces
(outside std) should be OK, although I personally detest them. I am
also a fan of m_name for private member data, mostly because I haven't
thought of anything better.

Jul 22 '05 #5
Jeff Schwab wrote:
Rob Williscroft wrote: [discussion of standard redacted]
I am
also a fan of m_name for private member data, mostly because I haven't
thought of anything better.


Yep. That's the only good Hungarianism I've found.

Jul 22 '05 #6

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.130...
Derek wrote in news:bu************@ID-46268.news.uni-berlin.de:

As Jeff already pointed out, the leading underscore is reserved by the
implementation. Using leading underscores is not only bad practice
but also not permitted by the standard. These days many books prefer
the *trailing* underscore:
Using a leading underscore is leagal as long as:

A) it isn't followed by another underscore,
B) it isn't followed by an uppercase letter,
C) it doesn't declare an identifier in the global namespace.


This true but why do you want to have to remember these cases?
It's simpler just to say never to leading underscores.
Additionally identifiers with 2 consecutive underscores are
allways reserved (hence (A) above).

Rob.
--
http://www.victim-prime.dsl.pipex.com/

Jul 22 '05 #7
sa*********@yahoo.com (Sandeep Sharma) wrote:
For many years I have been following the convention of naming all
class attributes with a leading underscore. This enables me to
quickly identify the class attributes when I encounter them in the
source code. Even the GoF book follows this convention, although it's
true that the GoF book is not an authoritative source for coding
guidelines.

Recently a colleague remarked that using a leading underscore is a bad
programming practice that should be avoided at all costs. According
to her this has to do with the fact that the compiler naming mangling
procedure also sticks leading underscores and therefore using
underscores in the source code makes the job of following the
post-processed source code (should there ever be a need) very
difficult.


The point here is to try to convey a certian piece of information, that
the variable in question is a member-variable. I see no reason to use
two characters to convey that information when one will do, so using
underscore + lower case (_variable) or 'm' + upper case (mVariable) both
seem like good options, but combining them into 'm_' seems like a waste.

Having a trailing underscore, (or any sort of trailing character) is
hard to parse for me. Maybe I'm too old school.

But why do we have to convey this particular piece of information? If
the variable is local, then it will be defined right there in the
function and will be obvious. If the variable is global... Well globals
are quite frowned upon aren't they? Maybe it's the global that needs
some special sort of code. Something to think about.

How about this, quite off the wall but interesting idea. Variables that
have a scope smaller than a function should be 1-2 characters long,
varibles with function scope should be 3-4 characters long, variables
with class scope should be 5-6 characters long, variables with file
scope 7-8 chars and variables with multiple file scope should be greater
than 8 characters. Just an idea...
Jul 22 '05 #8

"Daniel T." <po********@eathlink.net> wrote in message
news:po******************************@news06.east. earthlink.net...
sa*********@yahoo.com (Sandeep Sharma) wrote:
For many years I have been following the convention of naming all
class attributes with a leading underscore. This enables me to
quickly identify the class attributes when I encounter them in the
source code. Even the GoF book follows this convention, although it's
true that the GoF book is not an authoritative source for coding
guidelines.

Recently a colleague remarked that using a leading underscore is a bad
programming practice that should be avoided at all costs. According
to her this has to do with the fact that the compiler naming mangling
procedure also sticks leading underscores and therefore using
underscores in the source code makes the job of following the
post-processed source code (should there ever be a need) very
difficult.
The point here is to try to convey a certian piece of information, that
the variable in question is a member-variable. I see no reason to use
two characters to convey that information when one will do, so using
underscore + lower case (_variable) or 'm' + upper case (mVariable) both
seem like good options, but combining them into 'm_' seems like a waste.

Having a trailing underscore, (or any sort of trailing character) is
hard to parse for me. Maybe I'm too old school.

But why do we have to convey this particular piece of information? If
the variable is local, then it will be defined right there in the
function and will be obvious. If the variable is global... Well globals
are quite frowned upon aren't they? Maybe it's the global that needs
some special sort of code. Something to think about.


What about file static/unnamed namespace?
I like to use s_ for these and I use them a lot.

I am undecided about class static - where possible I prefer to use file
static instead but where
not I use a prefix - I just can't decide whether to use m_,s_ or something
else.

How about this, quite off the wall but interesting idea. Variables that
have a scope smaller than a function should be 1-2 characters long,
varibles with function scope should be 3-4 characters long, variables
with class scope should be 5-6 characters long, variables with file
scope 7-8 chars and variables with multiple file scope should be greater
than 8 characters. Just an idea...


variable names should always describe what they are.
Jul 22 '05 #9
Daniel T. wrote:

The point here is to try to convey a certian piece of information, that
the variable in question is a member-variable. I see no reason to use
two characters to convey that information when one will do, so using
underscore + lower case (_variable) or 'm' + upper case (mVariable) both
seem like good options, but combining them into 'm_' seems like a waste.


Why is there a shortage of Ms?

We use m_ for members and s_ for statics. However, a_ for
arguments and l_ for locals are IMO warts too far.

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: boxboy | last post by:
I am hoping someone from Microsoft could shed some insight into this question. Why did microsoft decide to use a verb based naming convtion for events rather such as Close and Click for rather...
27
by: Derek | last post by:
The company where I work uses a naming convention that I have never used before. They use mixed-case letters for public member functions, but lower-case with underscores for the rest, like this:...
3
by: clintonG | last post by:
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
10
by: jjkboswell | last post by:
I'm trying to pin down a good naming convention for the 3 things required to implement an event. You need: 1) a delegate 2) an event 3) an event handler Below is my understanding of a...
6
by: dm1608 | last post by:
I'm relatively new to ASP.NET 2.0 and am struggling with trying to find the best naming convention for the BAL and DAL objects within my database. Does anyone have any recommendations or best...
5
by: phez.asap | last post by:
I am new to Python but come from a C++ background so I am trying to connect the dots :) . I am really liking what I see so far but have some nubee questions on what is considered good form. For one...
114
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for...
14
by: Astley Le Jasper | last post by:
I'm still learning python and would like to know what's a good way of organizing code. I am writing some scripts to scrape a number of different website that hold similar information and then...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.