473,387 Members | 1,757 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,387 software developers and data experts.

Member variables style questions

What is the logic for dropping the C++ prepending of 'm_' from member
variables? Has there been a consensus developed on how to discriminate
between class members and automatic temp variables?
--
Richard Lewis Haggard
Jan 31 '06 #1
9 1368
I still use m_ for my variables. Otherwise, you are right, that it is hard
to determine what has local or class scope.

"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
What is the logic for dropping the C++ prepending of 'm_' from member
variables? Has there been a consensus developed on how to discriminate
between class members and automatic temp variables?
--
Richard Lewis Haggard

Jan 31 '06 #2
<"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom>> wrote:
What is the logic for dropping the C++ prepending of 'm_' from member
variables? Has there been a consensus developed on how to discriminate
between class members and automatic temp variables?


Well, not everyone has dropped it. I don't use m_ at home, but I do at
work.

Personally, I think that methods should almost always be short enough
that it's blatantly obvious which variables are member variables and
which are local variables.

If you're comfortable using m_ though, there's nothing to stop you from
using that - and if you follow the guidelines of making all member
variables private, no-one who isn't reading your source code will ever
know anyway (unless they poke around with reflection).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 31 '06 #3
And you can always obfuscate the code which makes this whole discussion mute
anyway. :)

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom>> wrote:
What is the logic for dropping the C++ prepending of 'm_' from member
variables? Has there been a consensus developed on how to discriminate
between class members and automatic temp variables?


Well, not everyone has dropped it. I don't use m_ at home, but I do at
work.

Personally, I think that methods should almost always be short enough
that it's blatantly obvious which variables are member variables and
which are local variables.

If you're comfortable using m_ though, there's nothing to stop you from
using that - and if you follow the guidelines of making all member
variables private, no-one who isn't reading your source code will ever
know anyway (unless they poke around with reflection).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 31 '06 #4
It's nice to know that even if I am being something of a stuck in my ways
old fuddy duddy, at least I'm in good company.

Most of the programmers here at this new contract site come out of a VB
background and don't have a predilection towards a lot of things that those
of us who have spent time under the C++ code Nazis have internalized. They
think a lot of things I consider to be good practice to be over the top,
wicked over kill and downright pedantic. The only thing that stops me from
entering master programmer to young whippersnapper novice lecture mode is a
fear that they might be right. None the less, I shall strive to set a good
example and thereby show these poor benighted savages the error of their
ways.
--
Richard Lewis Haggard

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom>> wrote:
What is the logic for dropping the C++ prepending of 'm_' from member
variables? Has there been a consensus developed on how to discriminate
between class members and automatic temp variables?


Well, not everyone has dropped it. I don't use m_ at home, but I do at
work.

Personally, I think that methods should almost always be short enough
that it's blatantly obvious which variables are member variables and
which are local variables.

If you're comfortable using m_ though, there's nothing to stop you from
using that - and if you follow the guidelines of making all member
variables private, no-one who isn't reading your source code will ever
know anyway (unless they poke around with reflection).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 31 '06 #5
<"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom>> wrote:
It's nice to know that even if I am being something of a stuck in my ways
old fuddy duddy, at least I'm in good company.
Well, bear in mind that there's a big difference between "m_..." and
Hungarian notation. It's perfectly possible to have either without the
other.
Most of the programmers here at this new contract site come out of a VB
background and don't have a predilection towards a lot of things that those
of us who have spent time under the C++ code Nazis have internalized. They
think a lot of things I consider to be good practice to be over the top,
wicked over kill and downright pedantic. The only thing that stops me from
entering master programmer to young whippersnapper novice lecture mode is a
fear that they might be right. None the less, I shall strive to set a good
example and thereby show these poor benighted savages the error of their
ways.


You should also bear in mind that not everything which is a good idea
in C++ is a good idea in C# though. For instance, I've seen C# code
like this:

if (5==x)

That's harder to read (to most people, I believe) than

if (x==5)

However, in C++ it makes sense to use the first to prevent a typo of

if (x=5)

from slipping into the code. In C# the mistaken version doesn't compile
anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 31 '06 #6
i still use m_ prefix to all member variables that is used on property
declarations.

private readonly string m_fileName = string.Empty;
public string FileName{
get{ return m_fileName; }
}
this way i have an idea that the variable is used by the property and
nothing else. I just put underscrore (_) for private fields.

Feb 1 '06 #7

"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
What is the logic for dropping the C++ prepending of 'm_' from member
variables? Has there been a consensus developed on how to discriminate
between class members and automatic temp variables?
--
Richard Lewis Haggard


I used to do it but I have found that in practice the only place where I
seemded to need it regularly was in constructors and I now prefer to use

this.surname = surname

rather than

m_surname = surname

because it is more readable in the bulk of the code where fields and locals
or parameters rarely seem to want to clash.

P.S. There are some people out there who will mangle their parameter names
to avoid clashes with fields - Show them no mercy!
Another advantage of using "this" is that you get more out of intellisense.
Feb 1 '06 #8
> What is the logic for dropping the C++ prepending of 'm_' from member variables?

Well, I never "dropped" it because I moved from C to C#, with a tour
through Java in between. C# is a whole new language, so I wouldn't
consider not following C++ practices in C# to be "dropping" anything.

That said, I have (reluctantly) adopted the convention of prepending
"_" to my member variables, not in order to distinguish them from
automatic variables (I prepend "this." to indicate that) but rather to
improve the workings of Intellisense and the debugger. It's a real
bummer in the debugger's locals window to have to scroll past dozens of
expensive-to-compute properties in order to get to the (no-cost) field
I want to see. The "_" puts all of the fields at the start of each
object's watch display, so I can see the object state first, and then
scroll for property values if I want to.

Plus, it solves the problem in Intellisense of picking the field by
accident instead of the property, or vice versa.

Feb 1 '06 #9

"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
What is the logic for dropping the C++ prepending of 'm_' from member
variables?
Well, I never "dropped" it because I moved from C to C#, with a tour
through Java in between. C# is a whole new language, so I wouldn't
consider not following C++ practices in C# to be "dropping" anything.

That said, I have (reluctantly) adopted the convention of prepending
"_" to my member variables, not in order to distinguish them from
automatic variables (I prepend "this." to indicate that) but rather to
improve the workings of Intellisense and the debugger. It's a real
bummer in the debugger's locals window to have to scroll past dozens of
expensive-to-compute properties in order to get to the (no-cost) field
I want to see.


1) As a design guidline anything that is expensive to compute or is likely
to throw an exception should be a method rather than a property. The whole
point of properties is that they appear to be fields.

2) I have started using DebuggerBrowsableAttribute to suppress trivial
properties which simplifies things a bit.
The "_" puts all of the fields at the start of each
object's watch display, so I can see the object state first, and then
scroll for property values if I want to.

Plus, it solves the problem in Intellisense of picking the field by
accident instead of the property, or vice versa.


I know what you mean but for some reason i find leading "_" particularly
jarring to read - more so than "m_".

Feb 1 '06 #10

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

Similar topics

3
by: prabhu | last post by:
hello to all, Please can anybody tell me the differnece between static and ordinary member variables. thankyou in advance, vishnu
9
by: Joost Ronkes Agerbeek | last post by:
Are member objects constructed before the body of the constructor executes? Consider the following example. Is this okay or is it possible that _bar will be created after the call to DoBar()? ...
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
27
by: thomasp | last post by:
Variables that I would like to make available to all forms and modules in my program, where should I declare them? At the momment I just created a module and have them all declared public there. ...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
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...
4
by: Mizipzor | last post by:
I have some troubles with a member variable that seems to be missing in a class. In short, heres what I do; class A is the parent class, B inherits from A and C inherits from B (hope I used the...
7
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
3
by: shaun roe | last post by:
This may be a question of style: Sometimes I need a little helper function (e.g. checkRange(val, lo, hi)) in my .cpp and have a habit of making these functions simple standalone functions in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.