Question

For example, I sometimes use "Set" suffix, like that:

ClassA
{
    public:
        ClassA(int xSet, int ySet):
            x(xSet),
            y(ySet)
        {
            ;
        }

    private:
        int x, y;
};

So, is there a better way to write xSet and ySet?

Was it helpful?

Solution 3

Well, the obvious answer is to name the parameters for what they are: initialX and initialY (or in setters, newX and newY). Following this logic, the member variables should probably be currentX and currentY—the usual rule is that variable names should be qualified nouns.

Which is all nice in theory. In practice, it seems that the above might be overkill (or a too rigorous application of the theory). At least, every where I've worked, there's been a convention about naming members, so that one can immediately see whether a name is a member or not. Ideally, it shouldn't be necessary, but practically, we don't work in an ideal world. The two most common conventions I've seen are m_ or my as prefixes for the member names (with s_ or our for static members). The other widespread conventions (using an underscore as prefix or suffix) should be avoided, because leading and trailing underscores aren't very visible, and make the code hard to read. (Leading underscores also tend to be used for very special symbols in the implementation, and should be especially avoided.)

OTHER TIPS

Sometimes people chose to use special suffixes/prefixes for data members to differentiate them from other variables/parameters. Popular naming conventions are:

1) m_ prefix (e.g. m_x);

2) _ suffix (e.g. x_);

I personally prefere x_ because it's less typing.

Every project/team/organization follows their own coding guidelines. If you are looking for something to adopt for yourself or your team, a google search for "C/C++ coding guidelines" should give you some useful links to get started.

Where I work, we use underscore (_) as the suffix of class member data. The arguments to constructors are the same names without the underscore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top