Announcement

Wednesday, May 19, 2004

set/get functions in class.

This is actually a mail trail of a question that Ashish Khare asked me. With his permission I am putting parts of the discussion in this blog entry.

The question was
I am looking at a class which has 4 ints declared as private and for each one there is 1 Get and 1 Set functions which is public.
When all these ints are exposed ( indirectly ) to external world, why should I declare them as private? I could have all the ints as public with no Get and Set functions. What do you say? Is there any advantage in the way class is existing now ( private variables with Get and Set functions )? Is there some better way of designing a class if requirement is such that you need to get and set the variables.
My answer to this question was
In effect, there is no encapsulation in class. Only place where it helps is in debugging. If value of some variable is getting changed. Then you can track where it is getting changed by putting a break point on 'set' function.

Ideally reevaluate why you need the class and especially 'get' functions. Follow the principle of 'tell, not ask'. See where and how are you using those get functions. (.e.g 'ask') and what operations are done on those values after 'get'.

Ashish replied with :
Got ur point but basically this class is related to some database records where you often need to get and set values. For example, in a optimization study some parameter can vary from 5 to 10 with an increment of 1. So in first run, I get the value ( which is 5 ). I use this value, analyze the problem and see if criteria is met or not. If this is not met, this value is increased by 1 and now it needs to be set back as some parallel tasks also need the updates value from the record. Likewise the flow continues. I guess, in this case, I can't get away from get and set calls ( or make the variables public )!


From the words like 'I get the value ( which is 5 ). I use this value, analyze the problem and see if criteria is met or not. ' one can say that the design is in 'ASK mode' rather than 'TELL mode'. My reply was :

Lets look at your problem again. "So in first run, I get the value ( which is 5 ). I use this value, analyze the problem and see if criteria is met or not. ". That is ASKING. " Can put the "analyse" part(fully or partially) in a member function of your class and then call the function? That is "TELLING". Then you don't need 'get'. You can have another function "increment" to increment the current value. Then you can avoid explicit set.

Try to switch "TELL" mode than 'ASK'. Its not THAT difficult. You will find that the code generated is lot simpler to maintain and change.


From his response, I think he got the point. From my experience switching from 'ASK mode' to 'TELL Mode' is a major and important step in practicing Good OOD. Once you switch, design, code, maintaince, etc becomes a lot easier. Try it.

PS: One of the guidelines from my Mechanics of Code Review ppts was to question the need of every 'get' function.

Martin Fowler has written one of the best articles on this subject (Data Access Routines).

No comments: