Setter & Getter methods : why do we need it?

I’m not sure how hot this subject was so far. After Apple Inc. introduced dot syntanx to Objective-C, from time to time I have heard, “Dot syntax is good” and “Dot syntax is bad”.

Here is a good blog post from M Cubed Software.
The Great Dot Syntax War of 200x

If you ask me my opinion, I would say, “It depends”.
When dot syntax is used at right moment, it is really convenient. Especially when you have to write simple and patternic accessor methods, dot syntax is really time-savor.

In this post I would like to see dot syntax with a different point of view.

There are many C++ programmers out there. Some of them are students in CS, EE, CSE, Math and so on.
Some will work for companies. This means that you have wide spectrum of C++ programmers with different expertise. Then some will say “Why do we need to write accessor methods? It is more convenient to make variables public and access it directly.”
To this opinion, you may agree with it or disagree with it.
Yes. Especially for the simple and patternic accessor methods, it is actually more convenient to access public variables directly.

For complicated accessor methods, though, people will agree with writing those.
Here, I would like to express my opinion that it is good to write accessor methods even for those simple and patternic accessors.
Let’s assume this class.

class CMYClass

	CMyView * pView;


In projects I work on, there are lots of reference to pView. There are probably more than 10 CView children classes used in the project. What is bad is that all the pointers to the CView children classes are written as pView. In most cases, I didn’t have problem in figuring out when a pView is assigned to some value.
However, there was very unlucky case where it was pretty hard to find it.
In a class, there was one line, buried in other source code lines, which set the pView.
Because it is set from other class than CMyClass directly, and it was the only place where pView is set, it was very hard to figure out what sets it for what reason.

However, if accessor methods were used, it is very easy to find where the variable is set and how they are used in the pile of source codes.

Also, it is usually better to set the variable name to somewhat more proper name.
What I mean is,

CMyView *pMyView;

instead of just pView.

If people write the accessor methods with same name no matter what classes they are for, then it will also bring the same problem. So, setting the accessor method name properly is also important.

However, at least, if accessor methods are used, it is usually easier to figure out what sets it, how it is set, when it is set.

UPDATED : For Objective-C, using property accessor methods are automatically generated or you can override it. If you use the generated one, what I wanted to mean is that it can be difficult to figure out when the interested variables are set and so on. Also, from callers’ codes, it is pretty hard to find when/how/what if you use dot syntax. With C++, manually introducing accessor methods are good idea and with C++/CLI, yeah!! it also supports properties like Objective-C. So, similar principle can be applied.


2 responses to this post.

  1. For debugging alone, it is easier to put a break point in SetView() instead of working out the (Possibly numerous) place it is set.
    Other advantages are implementing logic such as assert(pView != nullptr); in get functions, releasing old pointer in set functions and asserting that the new pView is valid. Apart from GetView and SetView one could also add functions like void CMYClass::CreateViewFromFile(); void CMYClass::CreateViewFromDataBase(); etc. The possibilities are endless.


  2. not satisfied


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: