simple naming convention question

S

Song Yun Zhao

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
 
J

Jeff Schwab

Song said:
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.
 
A

Alf P. Steinbach

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.
 
S

Song Yun Zhao

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.
 
C

Cy Edmunds

Song Yun Zhao said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top