473,836 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2335
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_someDataMembe r;
};
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
implementatio n. 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.RE MOVE.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*********@yah oo.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********@eat hlink.net> wrote in message
news:po******** *************** *******@news06. east.earthlink. net...
sa*********@yah oo.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
5033
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 than prepending them with On as in OnClose and OnClick? The way it is now, I encounter a lot of naming conflicts/ambiguity when trying to follow Microsoft's reccommened approach in naming my own events.
27
6743
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: class Foo { public: void somePublicMemberFunction(); protected:
3
4355
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 so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET...
14
3150
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... (both are "Pascal Case" with no leading or trailing qualifiers). For example... I'll be modelling something, e.g. a computer, and I'll
10
17806
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 naming convention based on the Windows Forms events. Can someone let me know if this convention is standard,
6
4083
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 practices for naming my objects? I currently have all my type classses simply called "JobSummaryClass" or "JobDetailsClass". These classes simply contain the public properties and the get/set functions for the object. Is this an appropriate naming...
5
1307
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 thing I am used to class variables being accessable only through methods instaed of directly refrenced from the object instence. From what I have read it looks like when you access a class variable directly in Python it has something in the...
114
7892
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 any comments. --
14
1844
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 collating it all together. Obviously each site needs to be handled differently, but once the information is collected then more generic functions can be used. Is it best to have it all in one script or split it into per site scripts that can then...
0
10836
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10585
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7786
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5819
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4009
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.