473,654 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it good or bad practice to prefix private class variables with underscore in C#

Many in our development team have came from a C++ background and are in
the practice of prefixing private class variables with an underscore to
improve readability and avoid naming collisions with local variables
and parameters. This has become our standard.

ie private string _name;

as opposed to:

ie private string name;

However, one of the newer guys has informed us that this is bad
practice.

Is this good, bad or no big deal?

Aug 4 '06 #1
13 3065
I think consistant naming is more important than naming style.

I like and use underscores when I write C/C++ code. I don't know why but I
think underscore is created for C/C++ :)

Enter C#, underscore usage is trivial I think.

"PromisedOyster " <Pr************ @hotmail.comwro te in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Many in our development team have came from a C++ background and are in
the practice of prefixing private class variables with an underscore to
improve readability and avoid naming collisions with local variables
and parameters. This has become our standard.

ie private string _name;

as opposed to:

ie private string name;

However, one of the newer guys has informed us that this is bad
practice.

Is this good, bad or no big deal?

Aug 4 '06 #2
PromisedOyster wrote:
Many in our development team have came from a C++ background and are in
the practice of prefixing private class variables with an underscore to
improve readability and avoid naming collisions with local variables
and parameters. This has become our standard.

ie private string _name;

as opposed to:

ie private string name;

However, one of the newer guys has informed us that this is bad
practice.

Is this good, bad or no big deal?
No big deal, so long as you're consistent. For a long time I worked at
a company which didn't use prefixes at all. The company I now work for
use m_ for instance fields and g_ for static fields. When I'm doing
personal development I tend not to put any prefixes on.

Methods should generally be short enough that it's obvious where a name
comes from, IMO, so prefixes aren't necessary - but if there's a big
preference for it, go with the flow.

Jon

Aug 4 '06 #3
Hi Oyster,

as other have said, consistency is the key. The only thing that comes into
mind against using underscores is that some development systems and
libraries tend to use the underscore to mark "special", "hidden" or
"internal" features.

Using an underscore in the beginning might confuse a developer with such
special methods/classes/fields/whatever.

But in C#, this shouldn't be an issue.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethi s.dystopia.fi
http://www.saunalahti.fi/janij/
Aug 4 '06 #4
On Fri, 4 Aug 2006 14:34:45 +0300, "Jani Järvinen [MVP]"
<ja***@removeth is.dystopia.fiw rote:
>Hi Oyster,

as other have said, consistency is the key. The only thing that comes into
mind against using underscores is that some development systems and
libraries tend to use the underscore to mark "special", "hidden" or
"internal" features.
Whatever naming convention you use, it is important to use names that
distinguish between fields, parameters and properties. You can
introduce annoying and difficult to predict bugs otherwise. I recently
got caught by this one

public string Item { get; set; }

public void MyMethod(string Item) {
Item = Item;
}

The compiler set the parameter to itself, causing a no-op. There was
no warning! (C# compiler needs to get a lot better at emitting
warnings IMNSHO). I think this is one of the reasons people use this
naming convention:

fields: m_FIELDNAME or other unique type of name
Parameters: camelCase
Properties: ProperCase

It guarantees that all 3 types of name are unique.
--
Phil
Aug 4 '06 #5
As everyone else has mentioned, consistency is the key. The most important
thing to keep in mind is the readability of the code, which includes naming
conventions as well as indentation, spacing, etc.

Because C# is case-sensitive, we use Pascal Case for public fields and
properties, Pascal Case preceded by underscore for private fields and
properties, and camel case for variables within methods and method
parameters. This way we can use the same identifier in 3 different ways
within the same class. Example:

private int _Count;
public int Count { get { return GetCount(_Count ); } set {
SetCount(value) ; } }

private int GetCount(int count)
{
return count + 1;
}

private void SetCount(int count)
{
_Count = count - 1;
}

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.

"PromisedOyster " <Pr************ @hotmail.comwro te in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Many in our development team have came from a C++ background and are in
the practice of prefixing private class variables with an underscore to
improve readability and avoid naming collisions with local variables
and parameters. This has become our standard.

ie private string _name;

as opposed to:

ie private string name;

However, one of the newer guys has informed us that this is bad
practice.

Is this good, bad or no big deal?

Aug 4 '06 #6
I use Visual Studio 2005 and a warning is generated in this case.
>...I recently
got caught by this one

public string Item { get; set; }

public void MyMethod(string Item) {
Item = Item;
}

The compiler set the parameter to itself, causing a no-op. There was
no warning!

Aug 4 '06 #7
For ref, in 2.0 this is caught as a warning:

Warning 1 Assignment made to same variable; did you mean to assign something
else? C:\{path to file}\Program.c s 10 9 WindowsApplicat ion1

Marc
Aug 4 '06 #8
Serious software teams almost always have standards about naming, design,
deployment... So we, developers, are generally not free to choose our
favorite naming convension.

"PromisedOyster " <Pr************ @hotmail.comwro te in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Many in our development team have came from a C++ background and are in
the practice of prefixing private class variables with an underscore to
improve readability and avoid naming collisions with local variables
and parameters. This has become our standard.

ie private string _name;

as opposed to:

ie private string name;

However, one of the newer guys has informed us that this is bad
practice.

Is this good, bad or no big deal?

Aug 4 '06 #9
Do whatever will make the code clearer for you and the other memebers on the
team (current and future). As long as these are private fields I don't think
you should follow any standard rules.
--
Stoitcho Goutsev (100)

"PromisedOyster " <Pr************ @hotmail.comwro te in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Many in our development team have came from a C++ background and are in
the practice of prefixing private class variables with an underscore to
improve readability and avoid naming collisions with local variables
and parameters. This has become our standard.

ie private string _name;

as opposed to:

ie private string name;

However, one of the newer guys has informed us that this is bad
practice.

Is this good, bad or no big deal?

Aug 4 '06 #10

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

Similar topics

3
3789
by: Erik De Keyser | last post by:
Hi group, I have a couple of projects with up to 8 forms. On most projects I write the code on each form with no modules. The last project I use 1 module and minimal code on each form , in fact just the code for each visual object, and all the subroutines are placed in a module. What is the best practice where I can place my VB code, at each form or in a separate module ? Pro's & con's ? Performance benefit ?
5
2013
by: Steven Bethard | last post by:
Philippe C. Martin wrote: > class Debug_Stderr: > __m_text = '' > __m_log_text = None > __m_dbg = None > __m_refresh_count = 0 <rant> I don't see the benefit in 99.9% of cases for making class variables
6
312
by: Nick Dreyer | last post by:
In VB.NET I would like to not have to create property get/set procedures for every class variable I want to expose to Excel VBA projects in COM builds. Can anyone tell me if that is possible, or refer me to some documents that will make me understand why this apparent limitation of VB.NET COM builds is acceptable, i.e. why in "good" object code programs I should expect to never have to create large numbers of public class variables that...
7
2599
by: zalzon | last post by:
Is it good practice in C++ for a member function to return data or is it better that data is stored in private member variable and printed in the member function? should i be using int function1() { .... return i
3
2191
by: Andy Dingley | last post by:
We've all seen this structure many times: <ul> <li><a href="..." >Click here</a></li> <li><a href="..." >Click here</a></li> </ul> Now it's obvious good practice to have sensible link texts, and also sensible use of title attributes on either the<a> or <li> elements. What's a good rule of thumb for these attributes ?
3
1186
by: Gustaf Liljegren | last post by:
Here's a small "problem" I often run into. The argument names in the constructor are usually the same as the those I want to store away as class variables. This example shows the problem and how I use to solve it: public class Person { private string _name; private string _age; public Person(string name, string age)
6
1507
by: David Whitchurch-Bennett | last post by:
Hi There, I have created a very simple web user control, containing only a combo box. I have created a class which contains some private object variables, for example... Private _anObject as Object Public Property anObject() As Object Get
66
3685
by: KimmoA | last post by:
Hey! Some questions about C that have been bugging me for a while... 1) Is inline a valid C keyword or not? I was kind of surprised of not finding it in C, to be honest. My "The C Programming Language" book doesn't mention it. 2) I understand that C doesn't care about whitespace that much, but why did they make it impossible to use the minus ('-') char in variable names? I now have to incorrectly name my "hi-score" variable "hiscore"....
8
7503
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine, like this: for(var i=0; i<list; i++) { var name = list; }
0
8815
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
8707
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
8482
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
8593
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...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
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
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1593
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.