473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Seeking a good praxis in the naming of class variables

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)
{
_name = name;
_age = age;
}
}

It's ugly, but the underscore tells me later on that this variable is
global within the class. Another way I used is single-letter argument
names in constructors (not in other methods), like in:

public class Person
{
private string name;
private string age;

public Person(string n, string a)
{
name = n;
age = a;
}
}

This looks cleaner, but it makes it harder to call the constructor (when
you can't remember the acronyms), and the names don't indicate where the
variables are declared. I hope there are other tidy and useful ways I
have overlooked. Please share your's.

Gustaf
Nov 16 '05 #1
3 1175
Gustaf,

Most people use the underscore to denote private fields in a class.
Others don't use it at all. As for your paramteters, there is clear
guidance in the naming guidelines on how to name them (since n and a are not
too descriptive, they are looked down upon).

In the case where your parameter names are the same as your field names,
you can always specify the scope by using "this", like so:

public class Person
{
private string name;
private string age;

public Person(string name, string age)
{
this.name = name;
this.age = age;
}
}

Personally, I like this approach the best.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gustaf Liljegren" <gu**************@xml.se> wrote in message
news:ug**************@TK2MSFTNGP11.phx.gbl...
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)
{
_name = name;
_age = age;
}
}

It's ugly, but the underscore tells me later on that this variable is
global within the class. Another way I used is single-letter argument
names in constructors (not in other methods), like in:

public class Person
{
private string name;
private string age;

public Person(string n, string a)
{
name = n;
age = a;
}
}

This looks cleaner, but it makes it harder to call the constructor (when
you can't remember the acronyms), and the names don't indicate where the
variables are declared. I hope there are other tidy and useful ways I have
overlooked. Please share your's.

Gustaf

Nov 16 '05 #2
Nicholas Paldino [.NET/C# MVP] wrote:
public Person(string name, string age)
{
this.name = name;
this.age = age;
} Personally, I like this approach the best.


Yes! This looks tidy. Thanks a lot.

Gustaf
Nov 16 '05 #3

"Gustaf Liljegren" <gu**************@xml.se> wrote in message
news:ug**************@TK2MSFTNGP11.phx.gbl...
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)
{
_name = name;
_age = age;
}
}

We tend to put underscores AFTER the variable names in function arguments,
and BEFORE them in class member names, so you'd get

public class Person
{
private string _name;
private string _age;
public Person( string name_, string age_ )
{
...
}
}

As a rule, I try to avoid having the name clashes, but in this sort of
example it's not always possible. Whatever you do, make sure you agree with
anyone else you're working with!

Steve
Nov 16 '05 #4

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

Similar topics

5
by: Ted | last post by:
I'm trying to come up with naming conventions for my company as we move to C#. I'm looking through the Naming Guidelines section on MSDN, but I'm unable to find a recommendation for class scope...
9
by: Sandeep Sharma | last post by:
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...
7
by: DEX | last post by:
Main page of my NC: http://www.ddmrm.com/coding/cpp/naming/cpp.naming.main.html Rules of my NC: http://www.ddmrm.com/coding/cpp/naming/cpp.naming.rules.html Comments are welcome. ...
4
by: Mark Broadbent | last post by:
stupid question time again to most of you experts but this is something that continually bothers me. I am trying to get into the habit of naming variables and controls in an assembly as per...
5
by: altergothen | last post by:
I'm currently doing a course in C# programming fundamentals. Please will you check and comment on the following assignment: Assignment: Create a simple calculator prgram that illistrates good OOP...
13
by: PromisedOyster | last post by:
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...
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...
8
by: Matt England | last post by:
My team currently using Boost Threads, but we are considering switching to ZThreads. (We seek cross-platform, C++ multithreading capabilities in an external library.) ZThread(s): ...
1
by: mk | last post by:
http://www.python.org/dev/peps/pep-0008/ "Function Names Function names should be lowercase, with words separated by underscores as necessary to improve readability." However, this PEP does...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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...
0
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...
0
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,...
0
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...
0
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...
0
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 ...

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.