What I find more insidious is the way accessor methods naturally encourage you to treat them as if they are performance-equivalent with member variables. People will reference them in loops, blithely unaware that they're building and rebuilding the same data structures in accessors that are only superficially similar to member variables.
What do you do to avoid this? I try to name my heavy-lifting methods so that they do not look like simple accessors, but sometimes it is inevitable.
For instance, a large dataset may have a method to get the length of the dataset. If the dataset does not maintain a member variable that has that value, then you have to calculate it. Later, for performance, you might implement an ivar that is updated whenever the dataset changes, thus making the method a simple accessor.
5 comments
[ 576 ms ] story [ 819 ms ] threadThe rest of the article has some interesting points, but I found it too wordy.
For instance, a large dataset may have a method to get the length of the dataset. If the dataset does not maintain a member variable that has that value, then you have to calculate it. Later, for performance, you might implement an ivar that is updated whenever the dataset changes, thus making the method a simple accessor.
(Obviously this doesn't apply in languages like ObjC or C++ where accessors are often necessary for memory management)