
So, how do we make the Employee class SRP-compliant? We’ll look into that next time.

When an HR minion finally tracks down the problem, months have passed, and the Operations department is millions of dollars over budget! Ouch. Unfortunately, this change has also affected ReportHours() used by the Operations department. A developer goes ahead and makes the change to GetRegularHours() but forgets to check where else GetRegularHours() is called. The Head of Accounting requests developers make a minor tweak to the treatment of non-overtime hours. A resposnibility can be thought of a reason to change, so the class should. So what? No big deal, right? Maybe, maybe not. It states that every class in your project should only have a single responsibility.

Unsurprisingly, Employee violates the SRP. We have three different actors demanding changes in the Employee class. What about CalculatePay()? Payment of employees is a financial concern and falls within the realm of the Accounting department and their accountants.Īnd ReportHours()? Reporting on the number of hours an employee has worked is an HR function. We are the ones who decide to change how employee state is persisted: Maybe we want to migrate from stored procedures to an ORM, or even swap out an SQL database for a non-SQL one. Robert Cecil Martin¹ (widely known as Uncle Bob) introduces a number of principles in his.
#Srp single responsibility principle software#
You can apply it to classes, software components, and microservices. It is one of the principles of Object-Oriented Design. The single responsibility principle is one of the most common design principles in object-oriented programming. Saving Employee state falls within the responsibility of the IT department of the business. SRP stands for Single Responsibility Principle. Save() is probably the easiest to determine the actor for: It’s us – software developers and DBAs. Who are the actors that Employee’s public methods are responsible to? Therefore GetRegularHours() is used by both CalculatePay() and ReportHours(). If specifications of this job changes, developer makes changes to that.
#Srp single responsibility principle code#
The developers were careful and kept the code DRY (Don’t Repeat Yourself). SRP helps developers write code that are decoupled, where each class has its own job. Martin uses the following Employee class to demonstrate a typical SRP violation:Ĭlass Employee has three public methods (+ signs): In his excellent book, ‘Clean Architecture’, Robert C. Given that terms like stakeholders don’t relate directly to programming, Martin falls back to the UML term ‘Actor’.Ī better definition for the SRP would then beĮach module should only be responsible to one actor. He made the point that the SRP is concerned with responsibilities to people, or better, a group of people or stakeholders. Martin, claims that this principle is not about each module having only a single responsibility per se. What about a reason to change ? The ‘discoverer’ of the SRP, Robert C. In some procedural languages, modules may be as simple as files containing functions that belong together (e.g. In object-oriented (OO) languages, modules are classes (e.g. The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate classes or modules. A rule of thumb is a class or method should change for one reason only, if it changes for multiple reasons, it needs to be broken down into smaller parts. Reading this definition, a couple of questions come to mind:Ī module is a mid-level programming construct, combining functions with data structures. SRP in this context basically means each class and method should only be responsible for a single behaviour/feature. The Single Responsibility Principle states thatĮach module should only have a single reason to change. The S in SOLID represents the Single Responsibility Principle (SRP). SOLID is an acronym, made up from the initial letters of the five principles that compose it. The only job of this class should be manipulating text.We are continuing from yesterday’s article on the purpose of the SOLID Principles.

Let’s consider a class that contains code that changes the text in some way.
