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

Home Posts Topics Members FAQ

How do you guys name the parameters of constructing methods?

xz
For example, I have a Class Date with internal variable year, month
and day.
In Java I would write:

class Date{
int year;
int month;
int day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day;
}
}

That is, simply name every parameter as what it is and use this.*
within the method body to differentiate the class variables and the
parameters.

However, in C++ it seems I cannot do this. In this particular
example, probably I can write:

Date::Date(int y, int m, int d): year(y), month(m), day(d) {
}

However, the letters "y", "m" or "d" are not that informative. Thus I
don't really like this naming style.
Is there a better way to do this?
What would you guys use to replace "y", "m" and "d" in this case?

Dec 18 '07 #1
16 1489
On Dec 18, 4:39 pm, xz <zhang.xi...@gmail.comwrote:
For example, I have a Class Date with internal variable year, month
and day.
In Java I would write:

class Date{
int year;
int month;
int day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day;
}

}

That is, simply name every parameter as what it is and use this.*
within the method body to differentiate the class variables and the
parameters.

However, in C++ it seems I cannot do this. In this particular
example, probably I can write:

Date::Date(int y, int m, int d): year(y), month(m), day(d) {

}

However, the letters "y", "m" or "d" are not that informative. Thus I
don't really like this naming style.
Is there a better way to do this?
What would you guys use to replace "y", "m" and "d" in this case?
There is no right way, its a question of style.

class Date
{
int m_year; // members
int m_month;
int m_day;
public:
Date() : m_year(2000), m_month(1), m_day(1)
{ }
Date(int year, int month, int day): m_year(year),
m_month(month),
m_day(day)
{ }
};

or use year_ and year but prefereably not _year

Dec 18 '07 #2
xz wrote:
For example, I have a Class Date with internal variable year, month
and day.
In Java I would write:

class Date{
int year;
int month;
int day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day;
}
}
In C++ you'd do

class Date {
int year;
int month;
int day;
public: // you probably meant that
Date(int year, int month, int day)
: year(year), month(month), day(day) {}
};
That is, simply name every parameter as what it is and use this.*
within the method body to differentiate the class variables and the
parameters.
Same with C++. You need 'this->' to differenciate.
However, in C++ it seems I cannot do this.
Is that so? Post the complete compilable code and the error message[s]
you get.
In this particular
example, probably I can write:

Date::Date(int y, int m, int d): year(y), month(m), day(d) {
}

However, the letters "y", "m" or "d" are not that informative. Thus I
don't really like this naming style.
Is there a better way to do this?
See above.
What would you guys use to replace "y", "m" and "d" in this case?
I've seen (and used) the convention to follow the argument names with
an underscore:

Date(int year_, int month_, int day_)
: year(year_), month(month_), day(day_) {}

Or (I know some cringe at a mere sight of it) just name your members
with a prefix (like 'm_'), arguments and local variables - without
prefices. It's easy to discern what is a member and what isn't.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 18 '07 #3
xz
On Dec 18, 3:49 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
xz wrote:
For example, I have a Class Date with internal variable year, month
and day.
In Java I would write:
class Date{
int year;
int month;
int day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day;
}
}

In C++ you'd do

class Date {
int year;
int month;
int day;
public: // you probably meant that
Date(int year, int month, int day)
: year(year), month(month), day(day) {}
};
That is, simply name every parameter as what it is and use this.*
within the method body to differentiate the class variables and the
parameters.

Same with C++. You need 'this->' to differenciate.
Now I know what was wrong. "this" in C++ is pointer instead of
reference so that you have to use "this->" instead of "this."
thanks!
However, in C++ it seems I cannot do this.

Is that so? Post the complete compilable code and the error message[s]
you get.
In this particular
example, probably I can write:
Date::Date(int y, int m, int d): year(y), month(m), day(d) {
}
However, the letters "y", "m" or "d" are not that informative. Thus I
don't really like this naming style.
Is there a better way to do this?

See above.
What would you guys use to replace "y", "m" and "d" in this case?

I've seen (and used) the convention to follow the argument names with
an underscore:

Date(int year_, int month_, int day_)
: year(year_), month(month_), day(day_) {}

Or (I know some cringe at a mere sight of it) just name your members
with a prefix (like 'm_'), arguments and local variables - without
prefices. It's easy to discern what is a member and what isn't.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 18 '07 #4
Victor Bazarov wrote:
> In this particular
example, probably I can write:

Date::Date(int y, int m, int d): year(y), month(m), day(d) {
}

However, the letters "y", "m" or "d" are not that informative. Thus I
don't really like this naming style.
Is there a better way to do this?

See above.
>What would you guys use to replace "y", "m" and "d" in this case?

I've seen (and used) the convention to follow the argument names with
an underscore:
And I've seen (and used) the convention to do exactly that with the member
variables instead of the parameters.
Date(int year_, int month_, int day_)
: year(year_), month(month_), day(day_) {}

Or (I know some cringe at a mere sight of it) just name your members
with a prefix (like 'm_'), arguments and local variables - without
prefices. It's easy to discern what is a member and what isn't.
Another possibility is to name the parameters different in the header than
in the implementation, i.e.:

// header
class Date
{
public:
Date(int year, int month, int day);
};

// implementation
Date::Date(int y, int m, int d)
: year(y), month(m), day(d)
{
}

Dec 19 '07 #5
xz wrote:
For example, I have a Class Date with internal variable year, month
and day.
In Java I would write:

class Date{
int year;
int month;
int day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day;
}
}
You can do it that way if you use an initialization list.

class Date
{
public:
Date( int year, int month, int day ): year(year), month(month), day(day)
{}
private:
int year;
int month;
int day;
};

Personally, however, I would do it this way:

class Date
{
public:
Date( int Year, int Month, int Day ): Year_( Year ), Month_( Month ),
Day_( Day ) {}
int Year() { return Year_; }
void Year( int Year ) { Year_ = Year; }
int Month() { return Month_; }
void Month( int Month ) { Month_ = Month; }
int Day() { return Day_; }
void Day( int Day ) { Day_ = Day; }
};

Some poople don't like the use of any time of prefix or suffix but
differentiate with use of capital letters or not. Personally, I think the
use of a suffix of _ is more clear.

[SNIP]

--
Jim Langston
ta*******@rocketmail.com
Dec 19 '07 #6
On 2007-12-18 16:39:52 -0500, xz <zh*********@gmail.comsaid:
For example, I have a Class Date with internal variable year, month
and day.
In Java I would write:

class Date{
int year;
int month;
int day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day;
}
}

That is, simply name every parameter as what it is and use this.*
within the method body to differentiate the class variables and the
parameters.

However, in C++ it seems I cannot do this. In this particular
example, probably I can write:

Date::Date(int y, int m, int d): year(y), month(m), day(d) {
}

However, the letters "y", "m" or "d" are not that informative. Thus I
don't really like this naming style.
Is there a better way to do this?
What would you guys use to replace "y", "m" and "d" in this case?
I see no problem with your parameter-naming style at all. In fact,
that is what I would do too.

There is no superfluous and ugly prefixes or subfixes. Your only
complain is the single-letter variable names. However, for local
variables in a short function, short variable names are preferred.
Afterall, how many local variables or how confusing can 50 lines of
code be?

--

-kira

Dec 19 '07 #7
"Victor Bazarov" <v.********@comAcast.netwrote in news:fk9f9f$gag$1
@news.datemas.de:
> In this particular
example, probably I can write:

Date::Date(int y, int m, int d): year(y), month(m), day(d) {
}

However, the letters "y", "m" or "d" are not that informative. Thus I
don't really like this naming style.
Is there a better way to do this?

See above.
>What would you guys use to replace "y", "m" and "d" in this case?

I've seen (and used) the convention to follow the argument names with
an underscore:

Date(int year_, int month_, int day_)
: year(year_), month(month_), day(day_) {}

Or (I know some cringe at a mere sight of it) just name your members
with a prefix (like 'm_'), arguments and local variables - without
prefices. It's easy to discern what is a member and what isn't.
I've been using a 'p_' prefix to the parameters:

Date(int p_year, int p_month, int p_day)
: year(p_year), month(p_month), day(p_day) {}

Gives you a nice reminder that you're dealing with the parameter, and not
the member variable. Although I only add the p_ when there is a name
"conflict".
Dec 19 '07 #8
On Dec 19, 9:20 am, Andre Kostur <nntps...@kostur.netwrote:
Gives you a nice reminder that you're dealing with the parameter, and not
the member variable. Although I only add the p_ when there is a name
"conflict".
Or misguides an uninvolved guy looking at your code (ie, me) to think
that it represents a pointer :) I think "m_" is more widely
recognized.

Dec 19 '07 #9
On 2007-12-19 02:45:55 -0500, Ioannis Gyftos <io************@gmail.comsaid:
On Dec 19, 9:20 am, Andre Kostur <nntps...@kostur.netwrote:
>Gives you a nice reminder that you're dealing with the parameter, and not
the member variable. Although I only add the p_ when there is a name
"conflict".

Or misguides an uninvolved guy looking at your code (ie, me) to think
that it represents a pointer :) I think "m_" is more widely
recognized.
Or maybe we should just do away with prefixes and suffixes, and just
use italic or bold or even colors.

Oh wait, I think some IDE's may actually do this already. :)

--

-kira

Dec 19 '07 #10
On Dec 19, 12:56 am, Rolf Magnus <ramag...@t-online.dewrote:
Victor Bazarov wrote:
In this particular
example, probably I can write:
Date::Date(int y, int m, int d): year(y), month(m), day(d) {
}
However, the letters "y", "m" or "d" are not that informative. Thus I
don't really like this naming style.
Is there a better way to do this?
See above.
What would you guys use to replace "y", "m" and "d" in this case?
I've seen (and used) the convention to follow the argument
names with an underscore:
And I've seen (and used) the convention to do exactly that
with the member variables instead of the parameters.
Not much better. Names should be meaningful. What does _ mean?
Date(int year_, int month_, int day_)
: year(year_), month(month_), day(day_) {}
Or (I know some cringe at a mere sight of it) just name your
members with a prefix (like 'm_'), arguments and local
variables - without prefices. It's easy to discern what is
a member and what isn't.
Another possibility is to name the parameters different in the
header than in the implementation, i.e.:
// header
class Date
{
public:
Date(int year, int month, int day);
};
// implementation
Date::Date(int y, int m, int d)
: year(y), month(m), day(d)
{
}
I don't like that either.

I don't think that there is one univeral convention that covers
everything. If the class is really just a structure with
constructors (but public data members), I'll use the same name
for the parameters and the members. If the class is mutable, it
would make sense (both in C++ and in Java) to name the
parameters "initialYear", etc. If the data members are private,
prefixing them with my works well for me. (Again, this
convention applies to Java as well as to C++.) Other conditions
are also possible. Just don't start or end names with an
underscore---it's too ugly.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 19 '07 #11
Rolf Magnus wrote:
Victor Bazarov wrote:
I've seen (and used) the convention to follow the argument names
with an underscore:

And I've seen (and used) the convention to do exactly that with the
member variables instead of the parameters.

My company's model coding standard requires that:

Rule 45a - A class data member shall have a trailing underscore.
Placing an underscore at the end of a class attribute allows you to
easily determine where all the class attributes are being used in the
code. In the example below, including the underscore makes it easier
to understand the code in the contructor's initializer list.

[example skipped]


Brian

Dec 19 '07 #12
On 19 Dec., 08:20, Andre Kostur <nntps...@kostur.netwrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in news:fk9f9f$gag$1
@news.datemas.de:


In this particular
example, probably I can write:
Date::Date(int y, int m, int d): year(y), month(m), day(d) {
}
However, the letters "y", "m" or "d" are not that informative. Thus I
don't really like this naming style.
Is there a better way to do this?
See above.
What would you guys use to replace "y", "m" and "d" in this case?
I've seen (and used) the convention to follow the argument names with
an underscore:
Date(int year_, int month_, int day_)
: year(year_), month(month_), day(day_) {}
Or (I know some cringe at a mere sight of it) just name your members
with a prefix (like 'm_'), arguments and local variables - without
prefices. It's easy to discern what is a member and what isn't.

I've been using a 'p_' prefix to the parameters:

Date(int p_year, int p_month, int p_day)
: year(p_year), month(p_month), day(p_day) {}

Gives you a nice reminder that you're dealing with the parameter, and not
the member variable. Although I only add the p_ when there is a name
"conflict".
I like that rule and use it myself. As does James Kanze, I believe
that the trailing underscore is just to ugly.

/Peter
Dec 19 '07 #13
peter koch wrote:
On 19 Dec., 08:20, Andre Kostur <nntps...@kostur.netwrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in
news:fk9f9f$gag$1 @news.datemas.de:
Date(int p_year, int p_month, int p_day)
: year(p_year), month(p_month), day(p_day) {}

Gives you a nice reminder that you're dealing with the parameter,
and not the member variable. Although I only add the p_ when there
is a name "conflict".

I like that rule and use it myself. As does James Kanze, I believe
that the trailing underscore is just to ugly.
Matter of opnion. Personally, I don't mind the trailing underscore, but
that leading p_ is horrible. In fact, anything attached to the front of
a variable is, to my mind, a means of reducing readability.

Brian
Dec 19 '07 #14
On Dec 19, 11:41 pm, "Default User" <defaultuse...@yahoo.comwrote:
Matter of opnion. Personally, I don't mind the trailing underscore,
It's not just a problem of esthetics. An underscore is not
particularly visible, and a trailing (or leading) underscore is
even less visible. If the name is the last symbol on the line,
it can easily be overlooked.

There's also the problem of how you pronounce it.
but that leading p_ is horrible. In fact, anything attached to
the front of a variable is, to my mind, a means of reducing
readability.
It depends on the rest of the convention. To start with, of
course, standard English usage is to put the modifier before
what it modifies, so you will almost always have something
"attached" to the front of a variable name (since variable names
should be qualified nouns). Something like "currentState",
rather than just "State" (which would be the name of the type).
I don't think that this is what you're talking about, however,
but used rigorously, it probably would obliviate the need for
any other indicators. In practice, I've found that some sort of
convention helps for member variables (and only member
variables). I use "my" (and "our" for static members) because
they *are* qualifiers. But if a client prefers m_ (and s_), it
doesn't bother me too much---since I just treat it as a special
spelling of "my" and "our", and pronounce it like that in my
head:-).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 20 '07 #15
James Kanze wrote:
On Dec 19, 11:41 pm, "Default User" <defaultuse...@yahoo.comwrote:
Matter of opnion. Personally, I don't mind the trailing underscore,

It's not just a problem of esthetics. An underscore is not
particularly visible, and a trailing (or leading) underscore is
even less visible. If the name is the last symbol on the line,
it can easily be overlooked.
That's your opinion. I don't have that problem.
There's also the problem of how you pronounce it.
When it's necessary to disambiguate a variable in conversation, we find
a way. It rarely comes up.


Brian
Dec 20 '07 #16
On Dec 20, 6:16 pm, "Default User" <defaultuse...@yahoo.comwrote:
James Kanze wrote:
On Dec 19, 11:41 pm, "Default User" <defaultuse...@yahoo.comwrote:
Matter of opnion. Personally, I don't mind the trailing
underscore,
It's not just a problem of esthetics. An underscore is not
particularly visible, and a trailing (or leading) underscore is
even less visible. If the name is the last symbol on the line,
it can easily be overlooked.
That's your opinion. I don't have that problem.
It's not really just an opinion; it's base on sound typological
principles. How serious the problem is will depend on the fonts
you are using. If the font displays the underscore three points
thick, for example, it's certainly not going to be overlooked.
Most fonts, at least most fonts I use, display it as a very,
very fine line well below the baseline. Where it isn't readily
apparent. (At least one font I've used put it so far below the
baseline that it merged with the top of capital letters on the
line below.) This is consistent with it's usual uses:
underlining text, or replacing a space in a context where a
space would be illegal.
There's also the problem of how you pronounce it.
When it's necessary to disambiguate a variable in
conversation, we find a way.
One can always find a way.
It rarely comes up.
You don't discuss your code enough with collegues:-).

Ideally, you shoud be able to discuss code over the telephone
with no need of meta information, just by applying the coding
guidelines to the pronounced symbol names. It is a ideal that
is, admittedly, rarely fully met---in particular, typenames and
variable names often only differ by the capitalization of their
first letter. (This violates another rule: that the edit
distance between any two symbols should be at least 2, so that a
single typo won't result in one symbol being interpreted as
another. This is perhaps less critical when one of the symbols
is a type name, and the other not, because such
misinterpretations will almost certainly cause a compiler error
anyway.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 21 '07 #17

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

Similar topics

6
by: Dave Theese | last post by:
Please see questions in the code below... Thanks! #include <iostream> using namespace std; class foo_class { public: foo_class() {cout << "Constructing..." << endl;}
11
by: Andrew Thompson | last post by:
I have written a few scripts to parse the URL arguments and either list them or allow access to the value of any parameter by name. <http://www.physci.org/test/003url/index.html>...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
21
by: ryanmhuc | last post by:
I know the subject might be confusing. I am no beginner with javascript but I haven't been able to figure out how to get the javascript file name from code inside the file. So you have an HTML...
18
by: NickName | last post by:
The following example may further clarify the question, Select fieldA, filedB, fieldC >From tblX Where fieldD = Forms!formName!controlName What I'd like to know is how to determine or get...
11
by: Ahmet AKGUN | last post by:
Hi; is it possible to open one form in .net platform that we have its name in string ? I have string sFormName = "frmCustomer"; and I must automatically open Customer form. or is it...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
11
by: Alexander Walker | last post by:
Hello I would like to write a method that allows me to pass a reference to an instance of a class, the name of a property of that class and a value to set that property to, the method would then...
24
by: Mike Hofer | last post by:
Please forgive the cross-post to multiple forums. I did it intentionally, but I *think* it was appropriate given the nature of my question. I'm working on an open source code library to help...
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
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,...
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,...
1
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: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.