tags:

views:

234

answers:

7

I have heard that in C++, using an accessor ( get...() ) in a member function of the same class where the accessor was defined is good programming practice? Is it true and should it be done?

For example, is this preferred:

void display() {
    cout << getData();
}

over something like this:

void display() {
    cout << data;
}

data is a data member of the same class where the accessor was defined... same with the display() method.

I'm thinking of the overhead for doing that especially if you need to invoke the accessor lots of times inside the same class rather than just using the data member directly.

Thanks!

+7  A: 

The reason for this is that if you change the implementation of getData(), you won't have to change the rest of the code that directly accesses data.

And also, a smart compiler will inline it anyways (it would always know the implementation inside the class), so there is no performance penalty.

Zifre
And in future you can change getData to be virtual (which is just a special case of changing the implementation of getData, but perhaps not obviously so).
Steve Jessop
This also helps when we want to monitor any access to this data variable, just set break points in the accessor - of course, it should never be the reason your adopt accessor or not.
lz_prgmr
A: 

Well, Mr. Khunt, the overhead is really insignificant for accessors in most cases. The question is whether not the accessor logic needs to be invoked, or the you need direct access to the field. This is a question for each individual implementation, but in many cases, won't make much of a difference.

The real reason for accessors is to provide encapsulation of your fields to other classes - and less about the containing class.

JoshJordan
A: 

Personally, I prefer not to have dozens of extra functions (get and set per every member variable). I would just use data, and would change to getData() only when required to do something differently. Since we are talking about changing the code only in one class, it shouldn't be too difficult.

Igor Oks
If it is a commonly used base class and there are many derived class, you'd better make your decision carefully at the beginning
lz_prgmr
A: 

It depends on what you might ultimately do with your data member I suppose.

By wrapping it up in the accessor you can then do things like lazily retrieving the data if this was an expensive process and not something you want to do unless someone asks for it. On the other hand you might know that it will always be a dumb built-in type and so I can't see any advantage of going through an accessor there. As I say, it depends on the member.

Troubadour
+1  A: 

It depends. Using an accessor function provides a layer of abstraction, which could make future changes to 'data' less painful. For example, if you wanted to lazily compute the value of 'data', you could hide that computation in the accessor function.

As for the overhead - If you are referring to performance overhead, it will likely be insignificant - your accessors will almost certainly be inlined. If you are referring to coding overhead, then yes, it is a tradeoff, and you'll have to decide whether it is worth the extra effort to provide accessors.

Personally, I don't think the accessors are worth it in most cases.

Joe
+1  A: 

Yes, I think it should be done more or less unconditionally. If the state variable is in some base class it should more or less always be private. If you allow it to be protected or public, all inherited will use it directly. These classes in turn might be classes your coworkers have written in some other project. If you suddenly decide to mock about in the base class and refactor e.g. the variable name to something more suitable, all users of that state must be rewritten.

This is probably not an issue if you are the only programmer or developing some code that no one ever will use. But as soon as the number of sub classes start to grow, it might get really hairy. Gotta love transparency !

However, I'm not gods best child on this planet. Sometimes I cheat ;) When you're in the owner class, I think it's ok to access private data directly. It might even be beneficial, since you automatically know that you are modifying the actual class you're in. Given that you have some kind of naming convention that actually tells you so, e.g. some variable name with an underscore at the end: "someVariable_".

Cheers !

Magnus Skog
A: 

To my mind, the most important aspect of this question is does it make the code more readable and therefore maintainable? Personally I don't think it does so I wouldn't do this.
Certainly you should never add a private accessor just to do this, that would be cnuts.

markh44