“Only talk to your immediate friends”

Let’s get one thing clear- we are making software- not looking for enlightenment

In Software development- sometimes ignorance can be bliss.
The lesser your class knows- the less complicated it will be.The more it knows- more it has to worry about.

Lets start with an example
I have a class OrderManager. This class is responsible for Order creation.
It calls another class- OrderValidator- who is responsible for Order validation -i.e. whether the order is good to go or not. There could be many validations that need to be done. e.g. ( A) whether the input is correct. (B) Do we have enough inventory ? (C) Can we deliver the order by the date requested?
Now all these tasks may in turn be done by different classes- because you wouldn’t want to overload one class with multiple responsibilities.

So we end up with three classes
OrderManager- who oversees the complete operation
OrderValidator who gives a go ahead for the order creation. It will in turn call another set of classes to take this decision.
InventoryValidator – who validates the inventory

Now lets talk about “friends”
OrderValidator is a friend of OrderManager
InventoryValidator is a friend of OrderValidator
As long as Ordermanger is not aware of the existence of InventoryValdiator- you are following this rule
However if we expose the InventoryValidator to OrderManager- you are guilty of breaking the rule

Code Example I – Following the rule

class OrderManager
{

public void createOrder ()
{
orderValidator.validateOrder();
}

}

class OrderValidator
{

public validateOrder (Order anOrder)
{
inventoryValidator.validateInventory (order.getRequestedAmount());
}

}

Example II Breaking the rule

class OrderManager
{

public void createOrder ()
{
orderValidator.validateOrder();
inventoryValidator= orderValidator.getInventoryValidator()
inventoryValidator.validateInventory (order.getRequestedAmount());
}

}

class OrderValidator
{

public validateOrder (Order anOrder)
{
}

public InventoryValidator getInventoryValidator ()
{
return inventoryValidator ;
}

}

Why do we have this rule ?
Like I said earlier on- the less you know the better you will be.Consider the previous example.Lets say down the road the Inventory Management got more complex and you need to look at a dstributed inventory .Now the Order creation process hasn’t really changed. We are still checking for inventory- the only difference we are checking inventory in more places or in a different fashion

If we have designed the system as per the first example- you will not see any change made to the OrderManagement system- but the second system will almost definately impose a change on the OrderManagement

What can we do to follow this rule ?
Any method in your class can invoke only the following types of methods

A) Methods in the same class.

B) Methods on the parameters  to the method

C) Method of the objects that were instantiated in the method

D) Method of the objects owned as member of the class

If you follow the above- you will be a true protector of this rule

 

But isnt this too restrictive?
It sure is and takes lots of discipline to follow it. And like any other priciple- its a friendly guideline.

And who do we thank for this?
NorthEastern University.

 http://www.ccs.neu.edu/home/lieber/LoD.html

 

“A class should have only one reason to change.”

Every class exists for  a specific reason. When you design a class- its essential that you know what that reason is and you know it well. And that reason should be the only reason that the class exists and there should be no other reason for that class to exist.

Lets start by some examples
e.g. We have a class OrderValidator. Now this is a class which is written to validate an Order.
Now this class shall change only when a validation rule changes- and not for any other reason
If we change the name of the column in the table in the Database and you have a direct impact on this class- you are not following the Single responsibility principle
If you change the client from web to a desktop based swing client- and you feel the need to change this class. You are violating the Single responsibility principle

Another example – You have a class which sends a message to an external system.
This class should change only when the communication mechanism changes. Like you are adding more capabilities to send messages in different formats or via different channels.
If the message generation logic changes and you need to change the class which sends the message- you are guilty of violating the Single responsibility Principle.

But why do we have Single Responsibility Principle?
-More the reasons to change a class- more often will that class change. This will give rise to greater opportunities for defects to creep in.
-The more a class does- more complicated it will be and hence harder to maintain in the long run.

How do we enforce it ?
- Keep your classes concise. If you need to accomplish two tasks- write two classes. Don’t be shy about writing multiple classes. Number of classes doesn’t increase complexity as would by overloading the same class with multiple tasks.
- Good communication.Put the objective of the class on top of the class to remind yourself (and the poor souls who will work on the class after you are long gone) – what the objective of the class is.
Many times the class looses its objective- simply because it was not well communicated to all.
- Ask yourself questions. What all situations will warrant a change to this class? What can change in the requirements that I will have to revisit and change this class ?

What causes this rule to be broken ?
-Shortcuts.Often a developer feels too lazy to write a new class and would rather add another rmethod to the same class. A class may have had a specific purpose when it was written- but over time things might get muddled. As more and more people contributete to the project- each may not understand exactly what the purpose wa and may see the class in a different light.
-Ill-defined responsibility for the class. When the class is first conceived- that is the only time its responsibility is decided and it should be well defined in clear terms. and that reason should be embedded as comments on top of the class for all to see and honor.

But…
“I dropped a column from the table and now I have to change the Validator,the DAO layer, UI layer”
That’s OK. This principle doesn’t prohibit that a single change can cause multiple classes to change
If you have dropped an attribute from the Model- you have basically dropped the validation referring to that attribute. And since its a change in the validation- its OK to change the OrderValidator.

Finally…
As always- Single Responsibility Principle is a guideline for writing robust class. Not the law.There will be exceptions- always.

So who do I thank for this down-to-earth principle?
http://en.wikipedia.org/wiki/Tom_DeMarco

Good Luck !!!

 

So finally I did it.
Bro and I have been talking about this for ages -to make a personal website for him.

Last week I decided – before they shut down the Internet -we might as well do something  while we have a chance.

The Prelude:

I have worked on web technologies for the last 9 years or so and consider myself quite savvy with “whats out there”
Having worked mostly on enterprise applications only till now- this is first time – I am making a website targeted towards an everyday web user: Someone who uses web for fun or to discover and not because that’s his job

And that makes a difference- quite  a bit of difference I think.

The vision:

There were couple of things that I knew immediately that I want to do

A Blog : Where Sanjeev can share his thoughts and ideas

A forum: Something where everyone can participate.This would make the site self-sustaining.

And then the usual hang-ons: About,Disclaimer,Contact us etc.

I wasn’t looking for something real fancy to start with- but wanted it to look professional and neat.

The domain name :

For now we are using the name that we already have- sanjeevnarula.com. If Sanjeev wants we will get additional domain as well- but that’s trivial

The Hosting:

I wanted to start on a modest budget.But once I realized that its possible to host multiple sites on a single hosting- I was glad

I wanted to host this site as well – so that was neat

The Technology:

Hmmm

This was tricky.

I didn’t want to start with something grandeur like RoR or Grails or Spring

Something that could get running quick and fast.

I started looking at PhP- installed apache server, made a page or two

It was good- but I wanted something quick

Sudhir (owner of chalowalkin.com – a very innovative site on job) introduced me to wordpress. I was quickly sold to the idea of making my website on wordpress.

Then the forums… I tried PHPBB2 and SFM- both looked good

But it soon became obvious that I needed a CMS

Developing the Blog and Forum using different scripts would mean

- Extra effort to make the site look seamless

- Find a way to manage users across modules.

The two CMS I seriously considered were Drupal and Joomla

I finally settled for Drupal

The final product

Drupal is a neat system.Infact it’s more of an engine to make sites

Few clicks here and there and trials- and I was done.

Time taken:

The design: 1 hour

Selecting the Host: 4 hours

Experimenting and deciding: 12 hours

Making the final site on Drupal: 6 hours

Finishing touches: 2 hours

And so now I invite you to come and pay Sanjeev a visit on his new home on the web.
sanjeevnarula.com

© 2011 Technology Cafe Suffusion theme by Sayontan Sinha