W
W Karas
The main purpose of private members is to provide some level of compile time checking for the rules of use of an object that must be followed, in order to keep the object in a valid state.
Given that, a private member variable with (simple, public) get/set functions just seems like pointless code bloat. It provides no additional protection against putting the object in a bad state than having the member variable be public.
Some (perhaps Smalltalk fans) argue that it's very rare that simply settinga member variable should properly be part of an objects public interface. Therefore, it's best to follow the rule of thumb that all member variablesshould be private.
It can also be argued that it is often important, for code maintenance, to be able to quickly identify external changes to a member variable, as opposed to read access to the variable. A counter-argument is that some sort ofcode browsing tool should be available to prevent the need to bloat the code in order to highlight this distinction.
In some cases, it could be likely that the get/set function would eventually do more than simple get/set a single member variable. That would be a clear case where keeping the member variable private would be the correct choice. But, as I have seen Dr. Bjarne argue, it's easy to go overboard with future-safety.
Your thoughts?
Given that, a private member variable with (simple, public) get/set functions just seems like pointless code bloat. It provides no additional protection against putting the object in a bad state than having the member variable be public.
Some (perhaps Smalltalk fans) argue that it's very rare that simply settinga member variable should properly be part of an objects public interface. Therefore, it's best to follow the rule of thumb that all member variablesshould be private.
It can also be argued that it is often important, for code maintenance, to be able to quickly identify external changes to a member variable, as opposed to read access to the variable. A counter-argument is that some sort ofcode browsing tool should be available to prevent the need to bloat the code in order to highlight this distinction.
In some cases, it could be likely that the get/set function would eventually do more than simple get/set a single member variable. That would be a clear case where keeping the member variable private would be the correct choice. But, as I have seen Dr. Bjarne argue, it's easy to go overboard with future-safety.
Your thoughts?