Saturday, January 7, 2012

The Power of One

Let's start with a question why big softwares are generally complex??????

Any Guesses !!!!!

Let me give it a try
It tend to achieve lots of functionalities which makes it tightly coupled which are difficult to test, debug and maintain. Obvious answer to this problem is engineer it well, create loose coupling between modules and many other intelligent sounding words which are repeatedly endlessly by every architect of this world. But practical implementation of those design principle are difficult because we often makes mistake in defining modules, defining scope of classes and sometimes methods as well. Below are the few thoughts of mine which are not that intelligent sounding but might help in creating a better designed software:

1. Create Software boundaries: Well, every estimation book use this word but generally all softwares deviate from this principle. So at the start only we should be clear with what all functionality and resources are in scope of the software we are creating.

2. Distribute responsibility between different modules: Define responsibility of every module very clearly and ensure that every module is not taking any extra responsibility.

3. Power of One: Ensure that every module takes one and only one responsibility, secondly there should be one and only one reason for which a module can be changed. Module can be as large as a dll and can be as small as a method.


Let's understand it with an example

Method to sort a list

private List sortedList SortList (List unsortedList)
{
//write some code

return sortedList;
}

So above method SortList have a responsibility to sort a list (just a single responsibility). Now we deep-down on sorting of list.
1. we have to choose sorting algorithm
2. we have to sort the list using that algorithm.

So to sort a list we identified 2 task to be completed.
Now take first task choose sorting algorithm. Sorting algorithm can be based on certain parameters like length of list; and then those parameters are required to sort a list.

1. Choose sorting algorithm choice parameters based on the list.
2. Check sorting algorithm based on the parameter defined in the previous step.

So the method would look like

private List SortList (List unsortedList)
{
string sortingalgo = FetchSortingAlgo(unsortedList);
List sortedList = SortbasedonAlgo(sortingalgo, unsortedList);

return sortedList;
}
private string FetchSortingAlgo(List unsortedList)
{
Parameters sortParameters = FetchSortParameters(unsortedList);
string sortingalgo = SortAlgo(sortParameters);
return sortingalgo;
}

So the task Sortlist is broken down into smaller methods and there by in smaller responsibilities.

If there is some change in the code due to change request or a bug then we should be clear that whether it is adding some new task or responsibility or it is changing existing responsibility, and based on that we should make the decision of extending a design or modification of design. Having said that the title of this blog The Power of One is trying to highlight that every module should to address only one responsibilities and if not possible then number of responsibilities should be minimal. I am eagerly waiting for ur thoughts and comments and till then keep coding.

No comments:

Post a Comment