simple naming convention question

Discussion in 'C++' started by Song Yun Zhao, Dec 18, 2003.

  1. 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
     
    Song Yun Zhao, Dec 18, 2003
    #1
    1. Advertising

  2. Song Yun Zhao

    Jeff Schwab Guest

    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.
     
    Jeff Schwab, Dec 18, 2003
    #2
    1. Advertising

  3. On Thu, 18 Dec 2003 12:38:59 +1100, Song Yun Zhao <> 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.
     
    Alf P. Steinbach, Dec 18, 2003
    #3

  4. > 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.
     
    Song Yun Zhao, Dec 18, 2003
    #4
  5. Song Yun Zhao

    Cy Edmunds Guest

    "Song Yun Zhao" <> wrote in message
    news:3fe1056a$...
    > 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/
     
    Cy Edmunds, Dec 18, 2003
    #5
  6. Song Yun Zhao

    jeffc Guest

    "Alf P. Steinbach" <> wrote in message
    news:...
    > >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.
     
    jeffc, Dec 18, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick

    Which c# naming convention?

    Rick, Jan 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,802
    Paul Glavich
    Jan 19, 2004
  2. Replies:
    0
    Views:
    406
  3. michael
    Replies:
    6
    Views:
    349
    Hans Nowak
    Jul 3, 2003
  4. Stefan Achatz

    question about symbol naming convention

    Stefan Achatz, Aug 4, 2005, in forum: Ruby
    Replies:
    0
    Views:
    107
    Stefan Achatz
    Aug 4, 2005
  5. HoneyMonster
    Replies:
    9
    Views:
    168
    HoneyMonster
    Feb 9, 2012
Loading...

Share This Page