Posted by: ahmedashfaque | July 15, 2015

Why accesor methods are still bad?

In object oriented programming, accessor methods are used to provide access to hidden (private) class variables. If accessor methods are good?

Using get accessor methods, a private class variable can be seen from outside a class and its value can be known. Though you can not change the value of the private class variable. So it is safe. When you really need to set value of a private class variable then you use set accessor methods. These methods allow a mechanism to change value of a private class variable from outside a class. This mechanism can be locked for unsecured connections to a class so that unauthorized access to the private class variable can be totally avoided.

So it seems that accessor methods are good. But the fact is that they are still not good enough. Why?

Any class variable is created for the purpose of exposing it to other classes. Otherwise there is no use of declaring a class variable. But we know that excessive use of class variables will lead to tight coupling between software components. There will be too many connections and too much information exchange among classes. This is a bad software design. Remember the software design mantra of loose coupling and high cohesion.

There should not be too many connections among classes. This means there should not be too many public methods and variables inside a class. Most of the methods and vaariables should be created to be used only inside a class. A public method or a variable should be used only when a connection is neeeded with another class for getting or sending any data.

For this reason use of too many private class variables and their accessor methods is a bad design.


Leave a Reply

Fill in your details below or click an icon to log in: 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: