Friday, March 20, 2009

Data Hiding

Today my blog is not directly related with eclipse. Lets have an OO discussion.

This started with a thought provoking arguement with one of my colleague. 

Problem - We have a public method which needs to return a data structure, say a 'List'. Which method signature attributes to a better design. (a) or (b)

(a) public List getRecords();
(b) public RecordCollectionVO getRecords();, where RecordCollectionVO is an object which internally stores a List instance.

Lets analyse both the cases. 

Arguements in favour of (a)
  1. The method is implemented based on design by contract.
  2. An extra class just to hold a list is an overhead.
  3. Tomorrow if the 'List' changes to a Map, anyway the module which access this module needs to make a few code changes. Then what the heck in changing the method signature as well. Why are you lazy to change one extra line of code?
  4. The chances for the data structure 'List' to change are rare.
Arguements in favour of (b)
  1. The method returns an object which hides the internal data structure which it uses.
  2. Tomorrow if the methods wants to return a Map instead of a List, the method signature will be unaffected. Hence which ever module that invokes this method doesn't need to change the way it access this method. 
I would opt for (b). Why?

Lets analyse a scenario which addresses both cases.

Case (a)

Consider three modules X, Y, Z.

Module X has the following method.
public List getRecords();

Module Y accesses module X and delegates the work to Module Z.
List list = moduleX.getRecords();
moduleZ.modify(list);

ModuleZ has the following method.
public void modify(List list);

I get a change request from my customer. I need to change the datastructure from List to a Map. In this case I need to modify the method signature, which inturn forces me to modify module X, module Y and module Z.

Case (b)

Consider three modules X, Y, Z.

Module X has the following method.
public RecordCollectionVO getRecords();

Module Y access module X and delegates the work to Module Z.
RecordCollectionVO rcVO = moduleX.getRecords();
moduleZ.modify(rcVO);

ModuleZ has the following method.
public void modify(RecordCollectionVO rcVO);

Now I need to change the datastructure List used inside RecordCollectionVO to a Map. In this case I do not need to modify the method signature. Thats good. But I have something more important here. I do not need to change even a single line of code in Module Y. The modules which need code changes are Module X and Module Z. 

If I can save atleast a single module from a single line of code change, I do not need to test that module again.

What ever, these are just two schools of thought. 

Thursday, March 19, 2009

Pragmatic Visuals!

A few months before, when I joined in my new project as a software developer, one problem was alarming me. This is a project which is running for the last two years. I need to understand the code to add value to the project.

If you have coded for at least a couple of years you might know that the programs of many software projects wear out by the sheer complexity of the programs itself. You need to be extra cautious to see that the programs do not rot.

I read hundreds of java classes in my new project which had thousands of lines of code. I generated the UML diagrams using IBM's Rational Software Architect. The diagrams were drawn in my 15" monitor. It was like looking at a football ground through a key hole. I could see bits and pieces of the diagrams in a single glance. I scrolled and scrolled and scrolled. My brain found it hard to store the previous images in it when I scrolled to new locations. Damn! I need something more magnificent, more useful.

I tried the eclipse pde incubator project - visualization tool to see the relation between the different eclipse plugins! Things are getting better and better.

Oh! World! We need to progress a lot with our visualization technologies to handle the complexity of already written code. May be... to generate code as well.

I wished I had a device like Tom Cruise had in Minority Report, where I can project the UML diagrams in the space around me. A 3-dimensional projection. I should be able to walk through the diagrams and see the connections between the classes, packages and plugins which makes up bigger modules. I should be able to turn the diagrams with small devices connected in my finger tips. I should be able to create new modules, modify it and generate code to see a running application with a few clicks.

With GEF-3d around the corner we may see some innovative tools in the near future.

Definitely human race will pick up to beat the complexity around us. See this video about a mind boggling gadget. No more descriptions, see it by yourself.

Thursday, March 12, 2009

Speed .... drhhhh

The Whetstone benchmark is a benchmark for evaluating the performance of computers.

According to Whetstone, you can see below the number of MWIPS (Million Instructions per second) for java and C++ applications in an Intel core 2 duo 1.8 GHz machine.

Java = Around 1000 MWIPS.
C++ = Around 1600 MWIPS.








Whetstone Results






If your application is slow who else can you blame now? Definitely your algorithms!!! Revamp all your old slow paced code.

Let your application top the throttle ... Speed up ... drhhhh

madhu