By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

simple naming convention question

P: n/a
hi, just wondering what ppl do in a situation like this:

say I have a class:

class Socket {
public:
void connect(string host, int port);
private:
string host;
int port;
}

Socket::connect(string host, int port)
{
host = host;
port = port;
}

The problem is that the private member data is the same as the function
argument. although C/C++ permits this, I am not sure if this is a good
idea. I really like to use lower case for my variables and intuitive names.

I am tempted to add a lower score "_" to the end of my private variables
like this:

host_ = host
port_ = port

But I read somewhere this is not recommended because libraries need to
do this and might pollute namespaces.

What do you reckon?

Song

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Song Yun Zhao wrote:
hi, just wondering what ppl do in a situation like this:

say I have a class:

class Socket {
public:
void connect(string host, int port);
private:
string host;
int port;
}

Socket::connect(string host, int port)
{
host = host;
port = port;
}

The problem is that the private member data is the same as the function
argument. although C/C++ permits this, I am not sure if this is a good
idea. I really like to use lower case for my variables and intuitive names.

I am tempted to add a lower score "_" to the end of my private variables
like this:

host_ = host
port_ = port

But I read somewhere this is not recommended because libraries need to
do this and might pollute namespaces.

What do you reckon?

Song

I name all of private, member variables things that begin with m_, e.g.
m_host and m_port. I only do that so I can accessors called host() and
port(). You can get around your immediate problem by doing this:

this->host = host;
this->port = port;

Please don't tag underscores onto the front or back of your variables.
That kind of code makes me go blind.

Jul 22 '05 #2

P: n/a
On Thu, 18 Dec 2003 12:38:59 +1100, Song Yun Zhao <so*******@tnarobag.com> wrote:
say I have a class:

class Socket {
public:
void connect(string host, int port);
private:
string host;
int port;
}

Socket::connect(string host, int port)
{
host = host;
port = port;
}

The problem is that the private member data is the same as the function
argument. although C/C++ permits this, I am not sure if this is a good
idea.
Right, it's not.

I really like to use lower case for my variables and intuitive names.
Lower case for variables, OK. Lower case for intuitive names, NOT OK.

I am tempted to add a lower score "_" to the end of my private variables
like this:

host_ = host
port_ = port
Many do.
But I read somewhere this is not recommended because libraries need to
do this and might pollute namespaces.
Nonsense.

What do you reckon?


I think the underscore impacts extremely negatively on readibility,
because most of what your code manipulates will be member names.

On the other hand many people do use the trailing underscore idea.

Just dont't use a _leading_ underscore.

One other idea is to use prefix "my" for member variables. That way you
can maintain almost the same naming standard across Java, C# and C++. When
I do that I like to modify the rule a bit for booleans, because it isn't
very readable to say "myIsEmpty", rather, I'd say "iAmEmpty" or some such.

Another idea is to use prefix "a" or "an" for arguments.

Jul 22 '05 #3

P: n/a
Lower case for variables, OK. Lower case for intuitive names, NOT OK.
can you elaborate on this?

Another idea is to use prefix "a" or "an" for arguments.


IMHO, this is probably better because it saves some typing (variables
are mentioned more frequently in the code) and keep the member variables
nice and clean.

Jul 22 '05 #4

P: n/a
"Song Yun Zhao" <so*******@tnarobag.com> wrote in message
news:3f********@news.alphalink.com.au...
hi, just wondering what ppl do in a situation like this:

say I have a class:

class Socket {
public:
void connect(string host, int port);
private:
string host;
int port;
}

Socket::connect(string host, int port)
{
host = host;
port = port;
}

The problem is that the private member data is the same as the function
argument. although C/C++ permits this, I am not sure if this is a good
idea. I really like to use lower case for my variables and intuitive names.
I am tempted to add a lower score "_" to the end of my private variables
like this:

host_ = host
port_ = port

But I read somewhere this is not recommended because libraries need to
do this and might pollute namespaces.

What do you reckon?

Song


I use m_ for member variables and i_ for intializers:

class Fruitcake
{
private:
int m_joke;
public:
Fruitcake(int i_joke) : m_joke(i_joke) {}
int joke() const {return m_joke;}
};

I also use t_ for typedef'ed types. None of this is really all that great.
It's just that having some conventions keeps me from wasting time trying to
think of a set of related variable names. For instance, in the example above
once I decided to have an accessor named "joke" the rest was automatic.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #5

P: n/a

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
But I read somewhere this is not recommended because libraries need to
do this and might pollute namespaces.


Nonsense.


Hardly nonsense. It's just that the problem is with underscores at the
beginning rather than the end.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.