-1

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
user3496846
  • 1,627
  • 3
  • 16
  • 28

3 Answers3

2

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.

AlexT
  • 1,413
  • 11
  • 11
2

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    Yes, this works well to visually distinguish instance vars from local vars. In languages that permit it, I prefer underscore prefix. Neither should be used on public fields aka properties. – david.pfx Apr 04 '14 at 08:05
  • Alright, I guess underscore is the best as it gets all the support here :) Will check out guidelines, thanks. – user3496846 Apr 04 '14 at 08:37
1

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

James Kanze
  • 150,581
  • 18
  • 184
  • 329