Friday, November 09, 2007

Privateering

Nothing divides programmers more than coding style, in particular the representation of private member variables.

Here are some of the options;



class Class
{
    m_member
    m_Member

    _member
    _Member

    member
    Member
};




The problem is, if you are like me you don't particularly like prefixes like m_ or _ and the problem with not using a prefix is that it can conflict with method names.

Using prefixes unfortunately can create bad code since it encourages programmers not to think about naming or the number of members in a class.

The solution can be to use a lower case member name and an upper case method name, but then there is no distinction between local variables and member variables within a method body.

Another less common solution which i use sometimes, since i always prefer to use no prefixes and uppercase member names just like methods, is to wrap my private members in a struct to remove the naiming conflict but keep the cleanliness and readability.



class Class
{

struct PrivateMembers
{
Member;
}

PrivateMembers Private;

void Member()
{
Private.Member;
}
};