Solid Design Patterns – Single responsibility principle

Solid design patterns – Single responsibility principle

Here we are going to discuss “Single responsibility principle”.

Single responsibility principle:
“A class should have one and only one reason to change, meaning that a class should only have one job.”

Recently I was reading a book “Clean Code” by Robert C Martin. It is a really good book to have for every software developer who always cares for his code, projects. While reading this book I came to very small things which we can take care of while day to day coding and designing a new system from scratch. Few of my learning sharing here.

SOLID is one of the most popular sets of design principles in object-oriented software development. SOLID stands for five design principles

S – Single responsibility principle
O – an Open closed principle
L – Liskov substitution principle
I – Interface segregation principle
D – Dependency Inversion principle

First, let’s think about why these design principles require:
1. Think about one fresh requirement came and you need to implement it. You can argue why not we just identify the requirement and slove it without apply or think about design. It saves the time. Yes, I agree, it will save your time.

2. but now think about this same requirement which you were solving needs to touch a lot of existing code. It has been already working code in production. Most importantly that person who wrote it originally also did not think about Clean, Neat, good code or using any design principle. now how much time you are going to invest in this.

Now your code is not maintainable and cannot adapt to changing requirements with relative ease is code just waiting to become obsolete.

You know the ratio of time spent reading vs. writing is well over 10:1. We are constantly reading old code as part of the effort to write new code.

Because this ratio is so high, we want the reading of code to be easy, even if it makes the writing harder. Of course, there’s no way to write code without reading it, so making it easy to read actually makes it easier to write. The code you are trying to write today will be hard or easy to write depending on how hard or easy the surrounding code, comment, variable names is to read.

Regardless of what we consider to be great code, it always requires one simple quality: the code must be maintainable. It should have Proper indentation, neat variable names, 100% test coverage, readable and can easily be enhanced by a developer other than its original author.

Design principles are guidelines by using them you can write good code, implementing them in your project show how much think through and care about the project which you build.

Now let’s talk about “Single responsibility principle”:

Since college we are reading about the decoupled system is good. Yes, SRP helps developers to write code that is decoupled, where each and every class has its own job. It is developed to an only specific job not doing multiple things. Now the advantage of this when some change request raise for a specific job only that change (Specific class to that particular to a job) not other code.

Easy to test coverage, easy for the developer, easy for QA and product person to validate only specific things and it dependent features.

Let’s take one example: We need to create a simple report.

// Single Responsibility Principle Violation
class Report
{
public function getTitle()
{
return 'Report Title';
}
public function getDate()
{
return '2016-04-21';
}
/**
* Generate report
*/
public function getContents()
{
return [
'title' => $this->getTitle(),
'date' => $this->getDate(),
];
}
/**
* Return report content in JSON format
*/
public function formatJson()
{
return json_encode($this->getContents());
}
}

Report class should generate the only report should bother about a format, but here we added JSON format. now in future, some other formats require then, it is an issue.

Let’s refactor it.

// Refactored
class Report
{
public function getTitle()
{
return 'Report Title';
}
public function getDate()
{
return '2016-04-21';
}
/**
* Generate report
*/
public function getContents()
{
return [
'title' => $this->getTitle(),
'date' => $this->getDate(),
];
}
}
/**
* Interface about formate
*/
interface ReportFormattable
{
public function format(Report $report);
}
/**
* Now formatter class which implement desired report formate behavior after getting report.
*/
class JsonReportFormatter implements ReportFormattable
{
public function format(Report $report)
{
return json_encode($report->getContents());
}
}

A couple of things which is not covered here but important to achieve Single Responsibility Principle and clean code is Dependency Injection and Event Driven Architecture. I will try to cover this in my next blog posts. one simple guidance that may help you is to use Dependency Injection for all your main activities within the application, and Event-driven approach for all secondary actions.

Coming blog is about “Open closed principle”

Hope this will help you if like please add your comment. Thanks