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).

Tuesday, May 18, 2004

Lets create a check list

Lets create a check list

This is Last of the 3 ideas that I posted in 'some ideas that just don't work'. Check lists were favourite of ISO 9000 (at least the ISO 9000 implementations, that I have seen).

If your code is bad, customers are cribbing, create a check list for coding errors, check list for spec documents, check list for testing, check list for code review, check list for release etc etc etc. Generally check lists start small, grow really fast. Soon they have of life of their own and they just refuse to die. Soon you are faced with huge check lists which are impossible to follow. Worst, you have a check list which is irrelevant, but you have to make a tick (else ISO auditor or internal process people catch you). So you have to follow some stupid, rigid outdated rules just to be 'compliant'.

Lot of times check lists are used as replacemen for training. Good developer, development practices cannot be replaced by training. Good coding practices should become a habit of developers and should not remain in some check list that no one believes.

Unfortunately, I have seen 'lets create a check list' syndrome too many times. May be it comes from the fact that there is problem, and you have to show to your management that you are doing something about it. Creating a check list something you can show, its tangible. Better practices and training are intangible and take time to show the results. I think thats root cause of 'lets create a check list' syndrome.

Monday, May 17, 2004

Lets use a project metric to measure individual performance

'We need to distinguish between good and bad developers and promote good developers. We need an objective way to do this. We want to tie the compensation for developer to its performance. We need an Objective way to measure Developer Performance. '

A very nice and persuasive argument. Usually this ends with 'lets use a project metric to measure indivisual performance. Lets use bug/KLOC'. Remember 'what gets measured gets done'. Problem is it gets done even if it is harmful to overall goal.

Combine these two facts, and results are not difficult to predict. Developers will try to minimize bugs/kloc even if jeopardizes the project. Simplest to reduce bugs/kloc is to increase KLOC. So cut/paste instead of reuse. Initial bug/KLOC figures look good. Overtime, the project will be a mess but by then original team is out. Any other metric like time to implement a feature, bugs per feature, bugs per module all such combination result in bad metrics for measuring individual performance.

Obviously, Slowly your good people will leave, because they try to do right thing for the project and rather than get rewarded, get punished for it. The Company is on downward spiral.

It reminds me of Dilbert comment 'How to detect if your company is doomed ?', If you keep on hearing words like processes, metrics, ISO, CMM etc., your company is doomed. There is a grain of truth in it.