Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > simple naming convention question

Reply
Thread Tools

simple naming convention question

 
 
Song Yun Zhao
Guest
Posts: n/a
 
      12-18-2003
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

 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      12-18-2003
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.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-18-2003
On Thu, 18 Dec 2003 12:38:59 +1100, Song Yun Zhao <(E-Mail Removed)> 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.

 
Reply With Quote
 
Song Yun Zhao
Guest
Posts: n/a
 
      12-18-2003

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

 
Reply With Quote
 
Cy Edmunds
Guest
Posts: n/a
 
      12-18-2003
"Song Yun Zhao" <(E-Mail Removed)> wrote in message
news:3fe1056a$(E-Mail Removed)...
> 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/


 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      12-18-2003

"Alf P. Steinbach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
question about symbol naming convention Stefan Achatz Ruby 0 08-04-2005 04:22 PM
Easy Schema Question About Naming Convention needin4mation@gmail.com XML 0 06-15-2005 02:19 PM
WS C3550 naming convention question frishack@gmail.com Cisco 2 02-23-2005 07:29 PM
Question regarding naming convention michael Python 6 07-03-2003 02:58 AM



Advertisments