{"id":12082,"date":"2014-03-17T10:02:36","date_gmt":"2014-03-17T08:02:36","guid":{"rendered":"http:\/\/mozaicworks.com\/?p=6469"},"modified":"2014-03-17T10:02:36","modified_gmt":"2014-03-17T08:02:36","slug":"the-single-responsibility-principle","status":"publish","type":"post","link":"https:\/\/mozaicworks.com\/blog\/the-single-responsibility-principle","title":{"rendered":"The Single Responsibility Principle"},"content":{"rendered":"

Let me tell you the story of a developer. His name is John and he\u2019s working in a medium-size company, developing with 10 colleagues the internal tools for users from: accounting, customer relations, human resources, marketing, management reports and operations. Since John is mainly specialized in HR, he gets requests such as:<\/p>\n

We\u2019re introducing our first bonus: 5% for employees who get more than 9\/10 from the 360 feedback. We need an alert in the application to show when someone qualifies. The bonus will apply automatically for the next month\u2019s pay.<\/em><\/p>\n

John starts the task by looking at the following piece of code:<\/p>\n

class Employee{\r\n\u00a0\u00a0\u00a0 public Money calculatePay(){...}\r\n\u00a0\u00a0\u00a0 public void save(){...}\r\n\u00a0\u00a0\u00a0 public String reportHours(){...}\r\n}<\/pre>\n

and suddenly understands he has a big problem. Can you see it?<\/p>\n

Before we find out what the problem is, let\u2019s analyze the context.<\/p>\n

What is likely to change?<\/h2>\n

Software changes because users ask for changes. They ask for changes because something in their life has changed, and there is a gap between what they have and what they need.<\/p>\n

In terms of code, this means that\u00a0some changes are more likely to happen together<\/strong>. HR departments are more likely to ask for changes in the way the pay is computed. Managers are more likely to ask for changes in reporting. IT is more likely to ask changes for things such as the database server or the operating systems. These three types of users are more likely to ask the changes separately than together.<\/p>\n

Taking advantage of change patterns<\/h2>\n

Since we expect the software changes to come in a certain way, could we optimize the design of the code for it? Sure we can. The idea is simple and powerful:\u00a0separate the code that changes together from the code that changes separately<\/strong>. This idea is known as the\u00a0Single Responsibility Principle (SRP)<\/strong>. You might have seen it stated in different ways:<\/p>\n

Every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class<\/p><\/blockquote>\n

or<\/p>\n

Any class or method should have only one reason to change.<\/p><\/blockquote>\n

or<\/p>\n

Things that change for the same reason should be kept together.<\/p>\n

Things that change for different reasons should be separated.<\/p><\/blockquote>\n

What\u2019s John\u2019s problem?<\/h2>\n

Looking at the code, John probably has to change the\u00a0calculatePay()<\/em>\u00a0method to take into account the bonus. When doing so, the class\u00a0Employee<\/em>\u00a0obviously changes. This means anything that depends on the class Employee needs recompilation and redeployment. But the Operations department hasn\u2019t requested any change, why do we need to redeploy the Operations module that uses\u00a0Employee.reportHours()<\/em>?<\/p>\n

Moreover, if John works overtime to finish his task and inadvertently introduces a mistake in the\u00a0reportHours()\u00a0<\/em>method on Friday evening before the release (we know this happens often<\/a>), the situation gets very weird. A change requested by the HR department has caused all the Operations reports to fail. The Operations module wasn\u2019t retested, after all the change was in the HR module, right? John will be in a tough position, answering to at least two angry managers.<\/p>\n

Even if John has no idea of the application architecture and doesn\u2019t see this coming, he probably has to ask the IT team to deploy at least two modules. That implies lots of paper work that John would rather not do. Moreover, the IT team will be unhappy about the change.<\/p>\n

Briefly, John is in a lot of trouble because one class doesn\u2019t follow the\u00a0Single Responsibility Principle<\/strong>. TheEmployee<\/em>\u00a0class is responsible to at least three different roles:<\/p>\n