Today my blog is not directly related with eclipse. Lets have an OO discussion.
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)
- The method is implemented based on design by contract.
- An extra class just to hold a list is an overhead.
- 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?
- The chances for the data structure 'List' to change are rare.
Arguements in favour of (b)
- The method returns an object which hides the internal data structure which it uses.
- 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.