Skip to main content

              Don’t use Setters and Minimize Getters


In the modern IDE’s, getters and setters are created automatically or in some cases including a specific library creates them automagically, injecting these accessor methods. This so-called feature of the IDE is cool but evil! We also have libraries that do this in code, like Lombok. Programmers don’t write these methods and the IDE enables us to do these incorrect things quickly.

Let me take a step back for a moment. I know a lot of you are surprised by the topic name and let us get into the details of it. In an object-oriented world, an object represents a real-world entity. You always interact with the object through its behavioural methods. What these getters/setters do is that they expose the internals of the object and provide you with a mechanism to modify an object's internal state.

Let us take an example. Suppose you have an employee object with the following data

Employee Object
Name:
Sherlock Holmes
Age
130
Address
221-B Baker Street, SW1A2NE

Here, assume you have setters. Any process that has access to the Employee object can modify this object. Say, it can change the name or age etc., basically introduces what we in today’s parlance call “Side-effects”. This change violates the fundamental principle of object-oriented practice – Encapsulation.  Why does this happen? programmers think an object as a data structure which holds the data rather than think it as a real-world object with its defined behaviour.

Side-effects are the enemy of any developer as it causes hard to debug bugs, hard to write test cases etc.,

If you are creating the object either through a constructor (or an overloaded constructor) or through a public method which invokes a private constructor, it avoids any “Mutation” of state and hence restricts side effects. Also, it avoids what is called “failure atomicity”, which means either you get the object, or you don’t. you don’t get an inconsistent object

Now let’s discuss getters.

Getters (also known as accessor methods) expose an object's internal state. This thinking leads us to expose the internals of the object, say the age or an address outside without the context of whose age it is. This breaks (again!) the encapsulation principle. We can always argue that getters are methods that take the private variable and show it outside (mostly, with very few changes), but why should a private variable (its an object state at the end of the day) be exposed outside? Can't the consumers access the required state through behavioural methods? I’m not saying don’t use getters ever, just review every getter method and appropriately share the value you would want to the outside world, rather than the internal state of that object. This in simple terms, means that don’t directly expose the value of the private variable to the outside world. Think of how this can be exposed as an object’s attribute/behaviour. The key here is “don’t ask for information to do something. Instead, as the object that has the information to do it for you”. CRC cards enforce this type of “object thinking”.

Let’s illustrate this with an  example.

Asking for “get me the age” from an employee object, where age is a private attribute is a getter, whereas “get me if the employee is of a valid age for employment” is a behaviour of the object.

This is the first of many posts on the series of “Object Thinking”. Stay tuned.

Comments

Popular posts from this blog

After a Fortnight !! Well the rays of the sun has finally managed to reach after a brief stop on the way..These couple of weeks have been a bit busy...Foosball is the buzzword and everyone is addicted to it...(why the hell not ??). The match between Czech Republic and the African Lions (Ghana) was just outstanding... they really made the Czechs 'OUT' Standing with their Agressive display of Skill combined with their determination to stay Alive... This arena brings out the best in everyteam.. The only sad point of watching this is that 'WE a nation of over a billion people do not have 10 players to play, whereas Trinidard and Tobago, not even the size of podicherry has managed to qualify and even hold England for 83 mins...GOD will we ever Qualify ?? I dont want to end on a pessimistic note.. but still.. IMPOSSIBLE is NOTHING.. let me rephrase is a bit.. IMPOSSIBLE IS NO THING !!
The Conflicts I have been thinking about the renewed fighting between the Tigers and the Srilankan Army and the unnecessary loss of civilian lives. This conflict made me read some more on other hotspots around the world and this is what I came up with 1. The Palestine 2. Srilanka 3. Kashmir 4. Ireland If we observe, we can see that the British were involved in all of the above problems.. They created Israel in 1948 and started [1]. They divided India/Paksistan and then left Kashmir independent [2]. They were ruling Srilanka and they didnt do anything about the ethnic problem [3] and [4] is their own backyard. I am not for armed struggle and not for dividing a country on ethnic lines,but the British could have done something better atleast in case of [1] and [3]. Let me conclude on an optimistic tone. 'LET THERE BE PEACE LEST THE WORLD GOES TO PIECES'
Wish u all a happy Independence Day.. just happened to see the advt of some programs telecast for independence day... have some doubts.. would somebody take pains to make these clear to this innocent fellow 1. what is the relation between independence and Namitha... 2. what is the relation between cine programs and the spirit of independence.. awaiting clarifications...