{"id":81,"date":"2024-10-12T00:25:52","date_gmt":"2024-10-11T20:25:52","guid":{"rendered":"https:\/\/abthoughts.com\/?p=81"},"modified":"2024-10-12T00:46:11","modified_gmt":"2024-10-11T20:46:11","slug":"solid-principles-single-responsibility-principle-srp","status":"publish","type":"post","link":"https:\/\/abthoughts.com\/?p=81","title":{"rendered":"SOLID Principles &#8211; Single Responsibility Principle\u00a0 &#8211; SRP"},"content":{"rendered":"\n<p>The Single Responsibility Principle (SRP) ensures code coherence by preventing a software component from performing multiple tasks or functions and its states that<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"block-49cb2079-5c45-470b-be86-d8dd4875ea41\">Each module should do exactly only one thing, and solve only one problem.<\/p>\n\n\n\n<p id=\"block-49cb2079-5c45-470b-be86-d8dd4875ea41\">Robert C. Martin<\/p>\n<\/blockquote>\n\n\n\n<p><\/p>\n\n\n\n<p>A software module is a group of lines of code that are organized in a form of a class, method, package, microservice, or package. Making the module focus on only one task to achieve makes software easier to test and maintain. A module should have only one reason to be modified. It is not desirable to modify the same module for different reasons or requirements. This would indicate poor architecture and a violation of the SRP principle.<\/p>\n\n\n\n<p>Let&#8217;s imagine a class that performs two features. If the business team requests a change in feature one, any client uses feature two will have to test its code and use the new version of the mentioned class, however that client was not using the changed feature.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"536\" height=\"303\" src=\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/image-11.png\" alt=\"\" class=\"wp-image-87\" srcset=\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/image-11.png 536w, https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/image-11-300x170.png 300w\" sizes=\"auto, (max-width: 536px) 100vw, 536px\" \/><figcaption class=\"wp-element-caption\">Dijkstra, separation of concerns.<\/figcaption><\/figure>\n\n\n\n<p><strong>SRP origin is separation of concerns.<\/strong><\/p>\n\n\n\n<p>Single responsibility principle is based on a well known scientific rule that is called, separation of concerns. Separation of concerns as a term was mentioned and explained Dijkstra on his publication &#8220;The role of scientific thought&#8221; on the year 1974. When we study a subject we should not study multiple variables at the same time. We study one variable and assume other variables are constant, then we study other variable separately and again assume other variables are constant till we have a complete idea about all the variable on the studied system. Another example when we solve two equations in 2 variables, we do not solve the 2 variables at the same time, we first try to remove one of the variables from one equation, once we get the value of the first variable we can substitute its value in the second equation to get the value of the second variable. The below text being quoted directly from Dijkstra explaining the separation of concerns concept.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"585\" src=\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-12-00.42.44-An-image-illustrating-the-concept-of-Separation-of-Concerns-or-Single-Responsibility-Principle-using-various-workshop-tools.-Each-tool-represents-a-sp-1024x585.webp\" alt=\"\" class=\"wp-image-92\" srcset=\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-12-00.42.44-An-image-illustrating-the-concept-of-Separation-of-Concerns-or-Single-Responsibility-Principle-using-various-workshop-tools.-Each-tool-represents-a-sp-1024x585.webp 1024w, https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-12-00.42.44-An-image-illustrating-the-concept-of-Separation-of-Concerns-or-Single-Responsibility-Principle-using-various-workshop-tools.-Each-tool-represents-a-sp-300x171.webp 300w, https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-12-00.42.44-An-image-illustrating-the-concept-of-Separation-of-Concerns-or-Single-Responsibility-Principle-using-various-workshop-tools.-Each-tool-represents-a-sp-768x439.webp 768w, https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-12-00.42.44-An-image-illustrating-the-concept-of-Separation-of-Concerns-or-Single-Responsibility-Principle-using-various-workshop-tools.-Each-tool-represents-a-sp-1536x878.webp 1536w, https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-12-00.42.44-An-image-illustrating-the-concept-of-Separation-of-Concerns-or-Single-Responsibility-Principle-using-various-workshop-tools.-Each-tool-represents-a-sp.webp 1792w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"block-73dc7d10-e4fb-44f6-948f-1bf96c468018\">&#8220;Let me try to explain to you, what to my taste is characteristic for all intelligent thinking. It is, that one is willing to study in depth an aspect of one&#8217;s subject matter in isolation for the sake of its own consistency, all the time knowing that one is occupying oneself only with one of the aspects. We know that a program must be correct and we can study it from that viewpoint only; we also know that it should be efficient and we can study its efficiency on another day, so to speak. In another mood we may ask ourselves whether, and if so: why, the program is desirable. But nothing is gained \u2014on the contrary!\u2014 by tackling these various aspects simultaneously. It is what I sometimes have called &#8220;the separation of concerns&#8221;<\/p>\n\n\n\n<p id=\"block-73dc7d10-e4fb-44f6-948f-1bf96c468018\"><a href=\"https:\/\/www.cs.utexas.edu\/users\/EWD\/transcriptions\/EWD04xx\/EWD447.html\">EW Dijkstra 1974 on the role of scientific thought<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>The Single Responsibility Principle (SRP) ensures code coherence by preventing a software component from performing multiple tasks or<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","category-solid"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SOLID Principles - Single Responsibility Principle\u00a0 - SRP - ABthoughts<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/abthoughts.com\/?p=81\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SOLID Principles - Single Responsibility Principle\u00a0 - SRP - ABthoughts\" \/>\n<meta property=\"og:description\" content=\"The Single Responsibility Principle (SRP) ensures code coherence by preventing a software component from performing multiple tasks or\" \/>\n<meta property=\"og:url\" content=\"https:\/\/abthoughts.com\/?p=81\" \/>\n<meta property=\"og:site_name\" content=\"ABthoughts\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-11T20:25:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-11T20:46:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/image-11.png\" \/>\n<meta name=\"author\" content=\"Abdelrahman Fathy Bassiouny\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Abdelrahman Fathy Bassiouny\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/abthoughts.com\/?p=81\",\"url\":\"https:\/\/abthoughts.com\/?p=81\",\"name\":\"SOLID Principles - Single Responsibility Principle\u00a0 - SRP - ABthoughts\",\"isPartOf\":{\"@id\":\"https:\/\/abthoughts.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/abthoughts.com\/?p=81#primaryimage\"},\"image\":{\"@id\":\"https:\/\/abthoughts.com\/?p=81#primaryimage\"},\"thumbnailUrl\":\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/image-11.png\",\"datePublished\":\"2024-10-11T20:25:52+00:00\",\"dateModified\":\"2024-10-11T20:46:11+00:00\",\"author\":{\"@id\":\"https:\/\/abthoughts.com\/#\/schema\/person\/2b4a7aed9d893435d974f7c1205df9e6\"},\"breadcrumb\":{\"@id\":\"https:\/\/abthoughts.com\/?p=81#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/abthoughts.com\/?p=81\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/abthoughts.com\/?p=81#primaryimage\",\"url\":\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/image-11.png\",\"contentUrl\":\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/image-11.png\",\"width\":536,\"height\":303},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/abthoughts.com\/?p=81#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/abthoughts.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SOLID Principles &#8211; Single Responsibility Principle\u00a0 &#8211; SRP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/abthoughts.com\/#website\",\"url\":\"https:\/\/abthoughts.com\/\",\"name\":\"ABthoughts\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/abthoughts.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/abthoughts.com\/#\/schema\/person\/2b4a7aed9d893435d974f7c1205df9e6\",\"name\":\"Abdelrahman Fathy Bassiouny\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/abthoughts.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/bcf8cb261478ad1011536386498157a3d506f31ae253acdb83591046974c192f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/bcf8cb261478ad1011536386498157a3d506f31ae253acdb83591046974c192f?s=96&d=mm&r=g\",\"caption\":\"Abdelrahman Fathy Bassiouny\"},\"sameAs\":[\"http:\/\/abthoughts.com\",\"https:\/\/www.linkedin.com\/in\/abdelrahman-fathy-43a14553\"],\"url\":\"https:\/\/abthoughts.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SOLID Principles - Single Responsibility Principle\u00a0 - SRP - ABthoughts","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/abthoughts.com\/?p=81","og_locale":"en_US","og_type":"article","og_title":"SOLID Principles - Single Responsibility Principle\u00a0 - SRP - ABthoughts","og_description":"The Single Responsibility Principle (SRP) ensures code coherence by preventing a software component from performing multiple tasks or","og_url":"https:\/\/abthoughts.com\/?p=81","og_site_name":"ABthoughts","article_published_time":"2024-10-11T20:25:52+00:00","article_modified_time":"2024-10-11T20:46:11+00:00","og_image":[{"url":"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/image-11.png"}],"author":"Abdelrahman Fathy Bassiouny","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Abdelrahman Fathy Bassiouny","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/abthoughts.com\/?p=81","url":"https:\/\/abthoughts.com\/?p=81","name":"SOLID Principles - Single Responsibility Principle\u00a0 - SRP - ABthoughts","isPartOf":{"@id":"https:\/\/abthoughts.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/abthoughts.com\/?p=81#primaryimage"},"image":{"@id":"https:\/\/abthoughts.com\/?p=81#primaryimage"},"thumbnailUrl":"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/image-11.png","datePublished":"2024-10-11T20:25:52+00:00","dateModified":"2024-10-11T20:46:11+00:00","author":{"@id":"https:\/\/abthoughts.com\/#\/schema\/person\/2b4a7aed9d893435d974f7c1205df9e6"},"breadcrumb":{"@id":"https:\/\/abthoughts.com\/?p=81#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/abthoughts.com\/?p=81"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/abthoughts.com\/?p=81#primaryimage","url":"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/image-11.png","contentUrl":"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/image-11.png","width":536,"height":303},{"@type":"BreadcrumbList","@id":"https:\/\/abthoughts.com\/?p=81#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/abthoughts.com\/"},{"@type":"ListItem","position":2,"name":"SOLID Principles &#8211; Single Responsibility Principle\u00a0 &#8211; SRP"}]},{"@type":"WebSite","@id":"https:\/\/abthoughts.com\/#website","url":"https:\/\/abthoughts.com\/","name":"ABthoughts","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/abthoughts.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/abthoughts.com\/#\/schema\/person\/2b4a7aed9d893435d974f7c1205df9e6","name":"Abdelrahman Fathy Bassiouny","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/abthoughts.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/bcf8cb261478ad1011536386498157a3d506f31ae253acdb83591046974c192f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/bcf8cb261478ad1011536386498157a3d506f31ae253acdb83591046974c192f?s=96&d=mm&r=g","caption":"Abdelrahman Fathy Bassiouny"},"sameAs":["http:\/\/abthoughts.com","https:\/\/www.linkedin.com\/in\/abdelrahman-fathy-43a14553"],"url":"https:\/\/abthoughts.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abthoughts.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=81"}],"version-history":[{"count":9,"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":97,"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/posts\/81\/revisions\/97"}],"wp:attachment":[{"href":"https:\/\/abthoughts.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abthoughts.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abthoughts.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}