<style type="text/css">a[data-mtli~="mtli_filesize2602kB"]:after {content:" (26.02 kB)"}a[data-mtli~="mtli_filesize79246kB"]:after {content:" (792.46 kB)"}</style>{"id":4917,"date":"2018-11-14T23:01:04","date_gmt":"2018-11-14T22:01:04","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=4917"},"modified":"2018-11-14T15:20:05","modified_gmt":"2018-11-14T14:20:05","slug":"single-responsibility-principle","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2018\/11\/14\/single-responsibility-principle\/","title":{"rendered":"Single Responsibility Principle"},"content":{"rendered":"<h1><em><a href=\"https:\/\/medium.com\/datadriveninvestor\/writing-flexible-code-with-the-single-responsibility-principle-b71c4f3f883f\">Writing Flexible Code with the Single Responsibility Principle<\/a><\/em><\/h1>\n<p>Mhmhmm&#8230; where have I heard of that SOLID principle? Ah, Eiffel!<\/p>\n<p><!--more--><!--nextpage--><\/p>\n<blockquote>\n<header class=\"container u-maxWidth740\">\n<div class=\"uiScale uiScale-ui--regular uiScale-caption--regular u-paddingBottom10 row postMetaHeader\">\n<div class=\"col u-size12of12 js-postMetaLockup\">\n<div class=\"uiScale uiScale-ui--regular uiScale-caption--regular postMetaLockup postMetaLockup--authorLockupForPost u-flexCenter js-postMetaLockup\">\n<div class=\"u-flex0\">\n<div class=\"u-relative u-inlineBlock u-flex0\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"avatar-image avatar-image--small\" src=\"https:\/\/i0.wp.com\/cdn-images-1.medium.com\/fit\/c\/62\/62\/1%2AQsVxhN14jwpYx3RDoLR5bA.jpeg?w=910&#038;ssl=1\" alt=\"Go to the profile of Severin Perez\"\/><\/p>\n<div class=\"avatar-halo u-absolute u-textColorGreenNormal svgIcon\"><\/div>\n<\/div>\n<\/div>\n<div class=\"u-flex1 u-paddingLeft15 u-overflowHidden\">\n<div class=\"u-lineHeightTightest\"><a class=\"ds-link ds-link--styleSubtle ui-captionStrong u-inlineBlock link link--darken link--darker\" dir=\"auto\" href=\"https:\/\/medium.com\/@severinperez?source=post_header_lockup\" data-action=\"show-user-card\" data-action-source=\"post_header_lockup\" data-action-value=\"c867c0804e76\" data-action-type=\"hover\" data-user-id=\"c867c0804e76\">Severin Perez<\/a><\/div>\n<div class=\"ui-caption ui-xs-clamp2 postMetaInline\">Writer | Developer | Information Specialist<\/div>\n<div class=\"ui-caption postMetaInline js-testPostMetaInlineSupplemental\"><time datetime=\"2018-09-07T19:48:42.889Z\">Sep 7<\/time><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/header>\n<div class=\"postArticle-content js-postField js-notesSource js-trackedPost\" data-post-id=\"b71c4f3f883f\" data-source=\"post_page\" data-collection-id=\"32881626c9c9\" data-tracking-context=\"postPage\" data-scroll=\"native\">\n<section class=\"section section--body section--first\">\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<h1 id=\"ec47\" class=\"graf graf--h3 graf--leading graf--title\">Writing Flexible Code with the Single Responsibility Principle<\/h1>\n<h2 id=\"d8c6\" class=\"graf graf--h4 graf-after--h3 graf--subtitle\">SOLID Principles and Maintainable Code<\/h2>\n<\/div>\n<div class=\"section-inner sectionLayout--fullWidth\">\n<figure id=\"97f5\" class=\"graf graf--figure graf--layoutFillWidth graf-after--h4\" data-scroll=\"native\">\n<div class=\"aspectRatioPlaceholder is-locked\">\n<div class=\"aspectRatioPlaceholder-fill\"><\/div>\n<div class=\"progressiveMedia js-progressiveMedia graf-image is-imageLoaded is-canvasLoaded\" data-image-id=\"0*nkk_88qGMXlzsHHN\" data-width=\"4928\" data-height=\"3280\" data-scroll=\"native\"><\/div>\n<\/div>\n<\/figure>\n<\/div>\n<\/div>\n<\/section>\n<\/div>\n<section class=\"section section--body section--first\">\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--fullWidth\">\n<figure id=\"97f5\" class=\"graf graf--figure graf--layoutFillWidth graf-after--h4\" data-scroll=\"native\">\n<div class=\"aspectRatioPlaceholder is-locked\">\n<div class=\"progressiveMedia js-progressiveMedia graf-image is-imageLoaded is-canvasLoaded\" data-image-id=\"0*nkk_88qGMXlzsHHN\" data-width=\"4928\" data-height=\"3280\" data-scroll=\"native\"><img decoding=\"async\" class=\"progressiveMedia-image js-progressiveMedia-image\" src=\"https:\/\/cdn-images-1.medium.com\/max\/2000\/0*nkk_88qGMXlzsHHN\" data-src=\"https:\/\/cdn-images-1.medium.com\/max\/2000\/0*nkk_88qGMXlzsHHN\"\/><\/div>\n<\/div><figcaption class=\"imageCaption\">Photo by <a class=\"markup--anchor markup--figure-anchor\" href=\"https:\/\/unsplash.com\/@nasa?utm_source=medium&amp;utm_medium=referral\" target=\"_blank\" rel=\"photo-creator nofollow noopener\" data-href=\"https:\/\/unsplash.com\/@nasa?utm_source=medium&amp;utm_medium=referral\">NASA<\/a> on&nbsp;<a class=\"markup--anchor markup--figure-anchor\" href=\"https:\/\/unsplash.com?utm_source=medium&amp;utm_medium=referral\" target=\"_blank\" rel=\"photo-source nofollow noopener\" data-href=\"https:\/\/unsplash.com?utm_source=medium&amp;utm_medium=referral\">Unsplash<\/a><\/figcaption><\/figure>\n<\/div>\n<div class=\"section-inner sectionLayout--insetColumn\">\n<p id=\"201c\" class=\"graf graf--p graf--hasDropCapModel graf--hasDropCap graf-after--figure\"><span class=\"graf-dropCap\">If<\/span> you\u2019ve been around software for a while, then you\u2019ve almost certainly heard of the SOLID principles. In short, these are a set of principles intended to help developers write clean, well-structured, and easily-maintainable code. In software, as in any intellectual and creative endeavor, there is quite a bit of debate about the \u201cright\u201d way to do things. Different practitioners have different ideas about what is \u201cright\u201d, depending on their individual experiences and inclinations. However, the ideas prescribed by SOLID adherents have been widely adopted in the software community, and agree with them or not, they\u2019re a useful set of principles from which to draw best practices. Moreover, SOLID has been thoroughly integrated into a broader set of Agile development practices and understanding them is thus a virtual requirement in the modern software industry.<\/p>\n<p id=\"09d5\" class=\"graf graf--p graf-after--p graf--trailing\">Developers and bloggers have written seemingly ad infinitum about SOLID in various places across the web. In researching this article, I encountered many such resources, some of which are cited at the bottom of this article for your reference. So, if the SOLID principles are well-covered elsewhere, why write yet another article about them? In short, for my own edification. Writing about complex topics is one of the best ways to learn them yourself. And for that reason, I am planning a series of five articles\u200a\u2014\u200aone on each of the SOLID principles. What follows is the first such article and focuses on the <em class=\"markup--em markup--p-em\">single responsibility principle<\/em>. Although I don\u2019t expect my addition to the corpus on this topic will be particularly unique, I hope that it will prove useful to some readers. And with that, let\u2019s dive in.<\/p>\n<\/div>\n<\/div>\n<\/section>\n<section class=\"section section--body\">\n<div class=\"section-divider\">\n<hr class=\"section-divider\"\/>\n<\/div>\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<h4 id=\"d9ce\" class=\"graf graf--h4 graf--leading\"><strong class=\"markup--strong markup--h4-strong\">A Quick Refresher on&nbsp;SOLID<\/strong><\/h4>\n<p id=\"9832\" class=\"graf graf--p graf-after--h4\">SOLID is an acronym for a set of five software development principles, which if followed, are intended to help developers create flexible and clean code. The five principles are:<\/p>\n<ol class=\"postList\">\n<li id=\"9b6a\" class=\"graf graf--li graf-after--p\">The <strong class=\"markup--strong markup--li-strong\">S<\/strong>ingle Responsibility Principle\u200a\u2014\u200aClasses should have a single responsibility and thus only a single reason to change.<\/li>\n<li id=\"1519\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/medium.com\/@severinperez\/maintainable-code-and-the-open-closed-principle-b088c737262\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/medium.com\/@severinperez\/maintainable-code-and-the-open-closed-principle-b088c737262\">The <strong class=\"markup--strong markup--li-strong\">O<\/strong>pen\/Closed Principle<\/a>\u200a\u2014\u200aClasses and other entities should be open for extension but closed for modification.<\/li>\n<li id=\"6e55\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/medium.com\/@severinperez\/making-the-most-of-polymorphism-with-the-liskov-substitution-principle-e22609866429\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/medium.com\/@severinperez\/making-the-most-of-polymorphism-with-the-liskov-substitution-principle-e22609866429\">The <strong class=\"markup--strong markup--li-strong\">L<\/strong>iskov Substitution Principle\u200a<\/a>\u2014\u200aObjects should be replaceable by their subtypes.<\/li>\n<li id=\"49f5\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/medium.com\/@severinperez\/avoiding-interface-pollution-with-the-interface-segregation-principle-5d3859c21013\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/medium.com\/@severinperez\/avoiding-interface-pollution-with-the-interface-segregation-principle-5d3859c21013\">The <strong class=\"markup--strong markup--li-strong\">I<\/strong>nterface Segregation Principle<\/a>\u200a\u2014\u200aInterfaces should be client specific rather than general.<\/li>\n<li id=\"123c\" class=\"graf graf--li graf-after--li graf--trailing\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/medium.com\/@severinperez\/effective-program-structuring-with-the-dependency-inversion-principle-2d5adf11f863\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/medium.com\/@severinperez\/effective-program-structuring-with-the-dependency-inversion-principle-2d5adf11f863\">The <strong class=\"markup--strong markup--li-strong\">D<\/strong>ependency Inversion Principle<\/a>\u200a\u2014\u200aDepend on abstractions rather than concretions.<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/section>\n<section class=\"section section--body\">\n<div class=\"section-divider\">\n<hr class=\"section-divider\"\/>\n<\/div>\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<h4 id=\"6abf\" class=\"graf graf--h4 graf--leading\"><strong class=\"markup--strong markup--h4-strong\">The Single Responsibility Principle<\/strong><\/h4>\n<p id=\"6492\" class=\"graf graf--p graf-after--h4\">The single responsibility principle (SRP) states that every class or module in a program should have responsibility for just a single piece of that program\u2019s functionality. Further, the elements of that responsibility should be encapsulated by the responsible class rather than spread out in unrelated classes. The developer and chief evangelist of the SRP, Robert C. Martin, describes a responsibility as a \u201creason to change.\u201d So, another way of putting the SRP is to say, as Martin does, that \u201c<span class=\"markup--quote markup--p-quote is-other\" data-creator-ids=\"anon\">a class should have only one reason to change.<\/span>\u201d<\/p>\n<p id=\"76d3\" class=\"graf graf--p graf-after--p\">Before going any further it\u2019s worth taking a look at the history of the SRP. Martin originally introduced the term in as part of his <em class=\"markup--em markup--p-em\">Principles of Object Oriented Design<\/em> [1]. According to Martin, the SRP has origins in Tom Demarco\u2019s idea of <em class=\"markup--em markup--p-em\">cohesion<\/em>, which describes the extent to which elements in a given class\/module are related and relevant to one another. Furthermore, it builds on David Parnas\u2019 description of <em class=\"markup--em markup--p-em\">encapsulation<\/em>, or <em class=\"markup--em markup--p-em\">information hiding<\/em>, which says that attributes and behavior relevant to a given object should be bundled together and hidden from outside access [2]. Taken together, these ideas lead naturally to the principle that a given piece of software functionality (aka, a responsibility) should be bundled into a single class and hidden from other elements of the program\u200a\u2014\u200aexposing only those pieces necessary to the functionality of the program as a whole.<\/p>\n<p id=\"3464\" class=\"graf graf--p graf-after--p graf--trailing\">On its face, this seems relatively straightforward. Individual pieces of a program\u2019s functionality should be distributed to distinct entities that are capable of handling them without outside assistance. But how do you define an \u201cindividual piece\u201d of a program? What, exactly, is a \u201cresponsibility\u201d and how do you reason about it from a business perspective? Martin, popularly known as \u201cUncle Bob,\u201d clarified just this concern in a 2014 blog article where he tied \u201cresponsibility\u201d to the idea of interested actors [3]. Martin\u2019s article is well worth a read, but to summarize, he argues that if a piece of software has several different kinds of users (aka, actors), then the disparate interests of each of those users defines a piece of that software\u2019s responsibilities. Martin uses the example of C-Suite executives (COO, CTO, CFO), each of whom uses some piece of business software for different reasons. Moreover, when considering how software should be changed, each of those actors should be able to dictate changes in the software without affecting the interests of the other actors.<\/p>\n<\/div>\n<\/div>\n<\/section>\n<section class=\"section section--body\">\n<div class=\"section-divider\">\n<hr class=\"section-divider\"\/>\n<\/div>\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<h4 id=\"d751\" class=\"graf graf--h4 graf--leading\"><strong class=\"markup--strong markup--h4-strong\">The \u201cGod&nbsp;Object\u201d<\/strong><\/h4>\n<p id=\"5fdc\" class=\"graf graf--p graf-after--h4\">Per usual, probably the best way to learn about the SRP is to see it in action. But to do that, we should perhaps first see what a program looks like when it does <em class=\"markup--em markup--p-em\">not<\/em> adhere to the SRP. Let\u2019s take a look at a simple program and see if we can break down its responsibilities. What follows is a brief Ruby program that outlines a class that describes the behaviors and attributes of space stations. Read through it and see if you can identify: a) the various responsibilities of objects instantiated by the SpaceStation class; and, b) the types of actors who might have an interest in an a space station\u2019s activities.<\/p>\n<figure id=\"908b\" class=\"graf graf--figure graf--iframe graf-after--p\">\n<div class=\"aspectRatioPlaceholder is-locked\">\n<div class=\"aspectRatioPlaceholder-fill\"><\/div>\n<div class=\"progressiveMedia js-progressiveMedia is-canvasLoaded is-imageLoaded\" data-scroll=\"native\"><\/div>\n<\/div>\n<\/figure>\n<\/div>\n<\/div>\n<\/section>\n<section class=\"section section--body\">\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<figure id=\"908b\" class=\"graf graf--figure graf--iframe graf-after--p\">\n<div class=\"aspectRatioPlaceholder is-locked\">\n<div class=\"progressiveMedia js-progressiveMedia is-canvasLoaded is-imageLoaded\" data-scroll=\"native\">\n<div class=\"iframeContainer\"><iframe loading=\"lazy\" class=\"progressiveMedia-iframe js-progressiveMedia-iframe\" src=\"https:\/\/medium.com\/media\/a165b33093e4c542d1391492fa3cfeb3?postId=b71c4f3f883f\" allowfullscreen=\"allowfullscreen\" data-src=\"\/media\/a165b33093e4c542d1391492fa3cfeb3?postId=b71c4f3f883f\" data-media-id=\"a165b33093e4c542d1391492fa3cfeb3\" data-thumbnail=\"https:\/\/i.embed.ly\/1\/image?url=https%3A%2F%2Favatars3.githubusercontent.com%2Fu%2F23282344%3Fs%3D400%26v%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07\" data-mce-fragment=\"1\" width=\"700\" height=\"250\" frameborder=\"0\"><\/iframe><\/div>\n<\/div>\n<\/div>\n<\/figure>\n<p id=\"6c69\" class=\"graf graf--p graf-after--figure\">Admittedly, our space stations aren\u2019t particularly capable (I guess NASA won\u2019t be calling on me any time soon); however, there is still quite a bit to unpack here. Immediately we can see that the <code class=\"\" data-line=\"\">SpaceStation<\/code> class has several disparate responsibilities. Roughly, we might say that space station operations can be broken down into four areas: sensors; supplies; fuel; and, thrusters. Although personnel are not specified in the class, we can easily imagine different actors who might care about these operational areas. Perhaps a scientist who manages the sensors, a logistical officer who handles supplies, an engineer who manages fuel, and a pilot who manages the thrusters. Given this variety of different operational areas and interested actors, might we say that this class is violating the SRP? Absolutely.<\/p>\n<p id=\"9c01\" class=\"graf graf--p graf-after--p\">Currently, our <code class=\"\" data-line=\"\">SpaceStation<\/code> class is a classic example of a so-called \u201cGod object\u201d\u200a\u2014\u200athat is, an object that knows about and does <em class=\"markup--em markup--p-em\">everything<\/em>. This is a major anti-pattern in object-oriented programming and should be avoided. But why? What\u2019s wrong with a \u201cGod object\u201d? Well, for starters, such objects are extremely hard to maintain. Our program is very simple right now but imagine what would happen if we added in some new functionality. Maybe our space station will need crew quarters, or a medical area, or a communications bay. As we added in such functionality, the <code class=\"\" data-line=\"\">SpaceStation<\/code> class would grow to immense size. Worse yet, each piece of functionality would be inextricably tied to all the others. If we want to change how the fuel tank is managed we might inadvertently break thruster operations. If the station scientist requests changes to sensor operations, those changes could have trickle-down effects to the communications bay.<\/p>\n<p id=\"e6cc\" class=\"graf graf--p graf-after--p graf--trailing\">Violating the SRP may be convenient in the beginning but the short-term benefits are not worth the long-term maintenance costs. Not only do we have to worry about how changes in one place affect another (due to our failure to separate concerns), but the code itself becomes unwieldy and unpleasant to deal with. Breaking program functionality down into encapsulated pieces is a much better option. Given that, let\u2019s make some changes to our <code class=\"\" data-line=\"\">SpaceStation<\/code> class.<\/p>\n<\/div>\n<\/div>\n<\/section>\n<section class=\"section section--body\">\n<div class=\"section-divider\">\n<hr class=\"section-divider\"\/>\n<\/div>\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<h4 id=\"60db\" class=\"graf graf--h4 graf--leading\"><strong class=\"markup--strong markup--h4-strong\">Breaking Down Responsibilities<\/strong><\/h4>\n<p id=\"56a2\" class=\"graf graf--p graf-after--h4\">Earlier, we identified four rough areas of operation that our <code class=\"\" data-line=\"\">SpaceStation<\/code> class was managing. Those seem like a good place to start as we refactor our code to be more in line with the SRP.<\/p>\n<figure id=\"3353\" class=\"graf graf--figure graf--iframe graf-after--p\">\n<div class=\"aspectRatioPlaceholder is-locked\">\n<div class=\"aspectRatioPlaceholder-fill\"><\/div>\n<div class=\"progressiveMedia js-progressiveMedia is-canvasLoaded is-imageLoaded\" data-scroll=\"native\"><\/div>\n<\/div>\n<\/figure>\n<\/div>\n<\/div>\n<\/section>\n<figure id=\"3353\" class=\"graf graf--figure graf--iframe graf-after--p\">\n<div class=\"aspectRatioPlaceholder is-locked\">\n<div class=\"progressiveMedia js-progressiveMedia is-canvasLoaded is-imageLoaded\" data-scroll=\"native\">\n<div class=\"iframeContainer\"><iframe loading=\"lazy\" class=\"progressiveMedia-iframe js-progressiveMedia-iframe\" src=\"https:\/\/medium.com\/media\/fda3c5548dc785a425145391e78ab20a?postId=b71c4f3f883f\" allowfullscreen=\"allowfullscreen\" data-src=\"\/media\/fda3c5548dc785a425145391e78ab20a?postId=b71c4f3f883f\" data-media-id=\"fda3c5548dc785a425145391e78ab20a\" data-thumbnail=\"https:\/\/i.embed.ly\/1\/image?url=https%3A%2F%2Favatars3.githubusercontent.com%2Fu%2F23282344%3Fs%3D400%26v%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07\" data-mce-fragment=\"1\" width=\"700\" height=\"250\" frameborder=\"0\"><\/iframe><\/div>\n<\/div>\n<\/div>\n<\/figure>\n<p id=\"fa55\" class=\"graf graf--p graf-after--figure\">Phew! That was a lot of changes, but things are already looking a lot better. Now, our <code class=\"\" data-line=\"\">SpaceStation<\/code> class is mostly just a container for subservient parts that manage individual operations, namely: a supply hold; a set of sensors; a fuel tank; and, thrusters. Each of these takes the form of an instance variable that is set during space station initialization. For each variable there is a corresponding class: <code class=\"\" data-line=\"\">Sensors<\/code>; <code class=\"\" data-line=\"\">SupplyHold<\/code>; <code class=\"\" data-line=\"\">FuelTank<\/code>; and, <code class=\"\" data-line=\"\">Thrusters<\/code>.<\/p>\n<p id=\"a0b2\" class=\"graf graf--p graf-after--p\">As you look through this version of the code, you will note several important differences with the first version. Not only are particular pieces of functionality encapsulated in their own classes but they are organized in a manner that is both predictable and consistent. The idea is to group like pieces of functionality in an attempt to follow the <em class=\"markup--em markup--p-em\">cohesion<\/em> principle and to isolate data such that it is only accessible to relevant actors. Now, if we wanted to change how supplies are managed from a hash structure to an array, we could do so very easily in the <code class=\"\" data-line=\"\">SupplyHold<\/code> class without affecting anything else in the program. Put another way, if the station logistics officer requests changes to her section\u2019s functionality, then we can do that without affecting work being done by the station science officer. Meanwhile, the <code class=\"\" data-line=\"\">SpaceStation<\/code> class has no idea how supplies are being stored and nor does it care!<\/p>\n<p id=\"e68d\" class=\"graf graf--p graf-after--p\">Our users (science officer, pilot, etc.) are probably reasonably happy now with how their relevant parts are broken out, and they can request changes as needed; however, there is still more work we can do. Note, for example, the <code class=\"\" data-line=\"\">report_supplies<\/code> method in the <code class=\"\" data-line=\"\">SupplyHold<\/code> class and the <code class=\"\" data-line=\"\">report_fuel<\/code> method in the <code class=\"\" data-line=\"\">FuelTank<\/code> class. What happens if flight control back on Earth asks for a change in the way reports are submitted? Well, we would have to change the <code class=\"\" data-line=\"\">SupplyHold<\/code> and <code class=\"\" data-line=\"\">FuelTank<\/code> classes. But what now if flight control decides to change how supplies or fuel are loaded on the station? No problem, we\u2019ll once again change the relevant methods on these classes. Hmm\u2026 it would seem then that we have <em class=\"markup--em markup--p-em\">multiple reasons for change<\/em> on these particular classes. That sounds to me like a violation of the SRP! Let\u2019s see if we can make a few more adjustments.<\/p>\n<figure id=\"7820\" class=\"graf graf--figure graf--iframe graf-after--p\">\n<div class=\"aspectRatioPlaceholder is-locked\">\n<div class=\"aspectRatioPlaceholder-fill\"><\/div>\n<div class=\"progressiveMedia js-progressiveMedia is-canvasLoaded is-imageLoaded\" data-scroll=\"native\"><\/div>\n<\/div>\n<\/figure>\n<section class=\"section section--body\">\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<figure id=\"7820\" class=\"graf graf--figure graf--iframe graf-after--p\">\n<div class=\"aspectRatioPlaceholder is-locked\">\n<div class=\"progressiveMedia js-progressiveMedia is-canvasLoaded is-imageLoaded\" data-scroll=\"native\">\n<div class=\"iframeContainer\"><iframe loading=\"lazy\" class=\"progressiveMedia-iframe js-progressiveMedia-iframe\" src=\"https:\/\/medium.com\/media\/7c552e0897e27b0def0f91cda4e973f9?postId=b71c4f3f883f\" allowfullscreen=\"allowfullscreen\" data-src=\"\/media\/7c552e0897e27b0def0f91cda4e973f9?postId=b71c4f3f883f\" data-media-id=\"7c552e0897e27b0def0f91cda4e973f9\" data-thumbnail=\"https:\/\/i.embed.ly\/1\/image?url=https%3A%2F%2Favatars3.githubusercontent.com%2Fu%2F23282344%3Fs%3D400%26v%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07\" data-mce-fragment=\"1\" width=\"700\" height=\"250\" frameborder=\"0\"><\/iframe><\/div>\n<\/div>\n<\/div>\n<\/figure>\n<p id=\"5766\" class=\"graf graf--p graf-after--figure\">In this final version of our program, we have broken out reporting responsibilities into a <code class=\"\" data-line=\"\">FuelReporter<\/code> class and a <code class=\"\" data-line=\"\">SupplyReporter<\/code> class, both of which inherit from a parent <code class=\"\" data-line=\"\">Reporter<\/code> class. We then add instance variables to our <code class=\"\" data-line=\"\">SpaceStation<\/code> class to initialize relevant reporters for it to use. Now, if flight control asks for changes to reporting procedures, we can make the relevant changes in the <code class=\"\" data-line=\"\">Reporter<\/code> subclasses without affecting the classes that they are reporting on.<\/p>\n<p id=\"5334\" class=\"graf graf--p graf-after--p graf--trailing\">Of course, there is still some coupling taking place between our various classes. A <code class=\"\" data-line=\"\">SupplyReporter<\/code> object depends on being handed a <code class=\"\" data-line=\"\">SupplyHold<\/code> object, as does a <code class=\"\" data-line=\"\">FuelReporter<\/code> object depend on a <code class=\"\" data-line=\"\">FuelTank<\/code> object. Necessarily, the <code class=\"\" data-line=\"\">Thrusters<\/code> too require a <code class=\"\" data-line=\"\">FuelTank<\/code> to draw upon. All of this seems reasonable to me, as some linkage is unavoidable, and we could still alter the operations of one object without drastically affecting the others. However, there is still room to improve on this program and to propose changes that would increase flexibility and maintainability (and indeed, I welcome such proposals in the comments!) What\u2019s important for now is that this version of the code is a fairly significant improvement over our first \u201cGod object\u201d version. We have effectively separated responsibilities into individual classes and thus reduced the chances of code changes in one place breaking operations in another. It\u2019s also much nicer to work with when updates are needed.<\/p>\n<\/div>\n<\/div>\n<\/section>\n<section class=\"section section--body\">\n<div class=\"section-divider\">\n<hr class=\"section-divider\"\/>\n<\/div>\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<h4 id=\"2e95\" class=\"graf graf--h4 graf--leading\"><strong class=\"markup--strong markup--h4-strong\">TL;DR<\/strong><\/h4>\n<p id=\"4083\" class=\"graf graf--p graf-after--h4 graf--trailing\">The <em class=\"markup--em markup--p-em\">Single Responsibility Principle<\/em> (SRP) is one of the five so-called SOLID principles, developed and promoted by Robert C. Martin to help developers produce flexible and maintainable code. In short, the SRP says that a given module or class should have responsible for a single element of a program\u2019s functionality, and thus have just a single reason to change. The benefits of adhering to the SRP include: clearly defined boundaries as to where a piece of functionality is implemented; information-hiding practices that protect the integrity of data; separation of concerns that ensures changes in one location do not affect others; and, ease of code maintenance. In practice, it can be useful to think of your programs in terms of interested users and whether changes they request to one piece of functionality might inadvertently affect others. In the long run, following the SRP will both save you time and result in more effective code.<\/p>\n<\/div>\n<\/div>\n<\/section>\n<section class=\"section section--body\">\n<div class=\"section-divider\">\n<hr class=\"section-divider\"\/>\n<\/div>\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<p id=\"f14a\" class=\"graf graf--p graf--leading\">That\u2019s all for our discussion of the SRP. Stay tuned for articles on the remaining four SOLID principles. Part 2 on the Open-Closed Principle is available <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/medium.com\/@severinperez\/maintainable-code-and-the-open-closed-principle-b088c737262\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/medium.com\/@severinperez\/maintainable-code-and-the-open-closed-principle-b088c737262\">here<\/a>. If you have any comments or questions, leave them below\u200a\u2014\u200aI would love to hear what you think.<\/p>\n<p id=\"df10\" class=\"graf graf--p graf-after--p graf--trailing\">If you would like alerts when a new article is published you can follow me here on Medium, on <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/twitter.com\/SeverinJPerez\" target=\"_blank\" rel=\"noopener nofollow\" data-href=\"https:\/\/twitter.com\/SeverinJPerez\">Twitter<\/a>, or subscribe on my <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/severinperez.com\/blog\/\" target=\"_blank\" rel=\"noopener nofollow\" data-href=\"https:\/\/severinperez.com\/blog\/\">personal blog<\/a> where these articles are cross-published. Happy coding!<\/p>\n<\/div>\n<\/div>\n<\/section>\n<section class=\"section section--body section--last\">\n<div class=\"section-divider\">\n<hr class=\"section-divider\"\/>\n<\/div>\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<h4 id=\"0e41\" class=\"graf graf--h4 graf--leading\"><strong class=\"markup--strong markup--h4-strong\">References<\/strong><\/h4>\n<ol class=\"postList\">\n<li id=\"8612\" class=\"graf graf--li graf-after--h4\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/web.archive.org\/web\/20150202200348\/http:\/\/www.objectmentor.com\/resources\/articles\/srp.pdf\" data-mtli=\"mtli_filesize2602kB\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/web.archive.org\/web\/20150202200348\/http:\/\/www.objectmentor.com\/resources\/articles\/srp.pdf\" data-mtli=\"mtli_filesize2602kB\">Paper: SRP: The Single Responsibility Principle (objectmentor)<\/a><\/li>\n<li id=\"a8b8\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/prl.ccs.neu.edu\/img\/p-tr-1971.pdf\" data-mtli=\"mtli_filesize79246kB\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/prl.ccs.neu.edu\/img\/p-tr-1971.pdf\" data-mtli=\"mtli_filesize79246kB\">Paper: On the Criteria to be Used in Decomposing Systems into Modules (1971; Parnas)<\/a><\/li>\n<li id=\"777a\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/8thlight.com\/blog\/uncle-bob\/2014\/05\/08\/SingleReponsibilityPrinciple.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/8thlight.com\/blog\/uncle-bob\/2014\/05\/08\/SingleReponsibilityPrinciple.html\">Article: The Single Responsibility Principle (20140508; Martin, R.)<\/a><\/li>\n<li id=\"2ebd\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/hackernoon.com\/the-secret-behind-the-single-responsibility-principle-e2f3692bae25\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/hackernoon.com\/the-secret-behind-the-single-responsibility-principle-e2f3692bae25\">Article: The Secret Behind the Single Responsibility Principle (20170830)<\/a><\/li>\n<li id=\"4264\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/code.tutsplus.com\/tutorials\/solid-part-1-the-single-responsibility-principle--net-36074\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/code.tutsplus.com\/tutorials\/solid-part-1-the-single-responsibility-principle--net-36074\">Article: SOLID: Part 1\u200a\u2014\u200aThe Single Responsibility Principle<\/a><\/li>\n<li id=\"ba0d\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/codeburst.io\/understanding-solid-principles-single-responsibility-b7c7ec0bf80\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/codeburst.io\/understanding-solid-principles-single-responsibility-b7c7ec0bf80\">Article: Understanding SOLID Principles: Single Responsibility<\/a><\/li>\n<li id=\"f78f\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/hackernoon.com\/you-dont-understand-the-single-responsibility-principle-abfdd005b137\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/hackernoon.com\/you-dont-understand-the-single-responsibility-principle-abfdd005b137\">Article: Think You Understand the Single Responsibility Principle?<\/a><\/li>\n<li id=\"9d41\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/www.toptal.com\/software\/single-responsibility-principle\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/www.toptal.com\/software\/single-responsibility-principle\">Article: Single Responsibility Principle: A Recipe for Great Code<\/a><\/li>\n<li id=\"505a\" class=\"graf graf--li graf-after--li graf--trailing\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/en.wikipedia.org\/wiki\/Single_responsibility_principle\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/en.wikipedia.org\/wiki\/Single_responsibility_principle\">Wikipedia: Single Responsibility Principle<\/a><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/section>\n<\/blockquote>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Writing Flexible Code with the Single Responsibility Principle Mhmhmm&#8230; where have I heard of that SOLID principle? Ah, Eiffel!<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2018\/11\/14\/single-responsibility-principle\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-4917","post","type-post","status-publish","format-standard","hentry","category-senza-categoria"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-1hj","jetpack-related-posts":[{"id":7385,"url":"https:\/\/monodes.com\/predaelli\/2020\/07\/13\/the-s-o-l-i-d-principles-in-pictures\/","url_meta":{"origin":4917,"position":0},"title":"The S.O.L.I.D Principles in Pictures","author":"Paolo Redaelli","date":"2020-07-13","format":"link","excerpt":"If you are familiar with Object-Oriented Programming, then you\u2019ve probably heard about the SOLID principles. Ugonna Thelma explains The S.O.L.I.D Principles in Pictures and she explains them very well. In fact I shall admit that while I've interiorized the S, the O, the L and the I, I always tend\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"Ugonna Thelma","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2020\/07\/2bveoh12R-5thGMYGTkV74A.jpeg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":4948,"url":"https:\/\/monodes.com\/predaelli\/2018\/11\/19\/maintainable-code-and-the-open-closed-principle-severin-perez-medium\/","url_meta":{"origin":4917,"position":1},"title":"Maintainable Code and the Open-Closed Principle \u2013 Severin Perez \u2013 Medium","author":"Paolo Redaelli","date":"2018-11-19","format":false,"excerpt":"A Look at the Open-Closed Principle in JavaScript Maintainable Code and the Open-Closed Principle \u2013 Severin Perez \u2013 Medium Oh, another interesting article that tries to educate the \"unliterate\" JavaScript masses about the basic principle behind Eiffel..... Maintainable Code and the Open-Closed Principle Abstraction and the Open-Closed Principle in JavaScript\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2340,"url":"https:\/\/monodes.com\/predaelli\/2017\/04\/02\/modules-vs-microservices\/","url_meta":{"origin":4917,"position":2},"title":"Modules vs. microservices","author":"Paolo Redaelli","date":"2017-04-02","format":"link","excerpt":"Apply modular system design principles while avoiding the operational complexity of microservices. Modules vs. microservices Modules vs. microservices Apply modular system design principles while avoiding the operational complexity of microservices. By Sander Mak March 24, 2017 Container (source: Antranias). Register for the O'Reilly Software Architecture Conference in New York, April\u2026","rel":"","context":"In &quot;Senza categoria&quot;","block_context":{"text":"Senza categoria","link":"https:\/\/monodes.com\/predaelli\/category\/senza-categoria\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5033,"url":"https:\/\/monodes.com\/predaelli\/2018\/12\/10\/sans-forgetica-rmit\/","url_meta":{"origin":4917,"position":3},"title":"Sans Forgetica &#8211; RMIT","author":"Paolo Redaelli","date":"2018-12-10","format":false,"excerpt":"Sans Forgetica is a typeface designed using the principles of cognitive psychology to help you to better remember your study notes. Source: Sans Forgetica - RMIT","rel":"","context":"In &quot;Fonts&quot;","block_context":{"text":"Fonts","link":"https:\/\/monodes.com\/predaelli\/category\/fonts\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1500,"url":"https:\/\/monodes.com\/predaelli\/2016\/05\/05\/object-oriented-and-functional-programming\/","url_meta":{"origin":4917,"position":4},"title":"Object oriented and functional programming","author":"Paolo Redaelli","date":"2016-05-05","format":false,"excerpt":"\u00a0Object oriented and functional programming Learn how functional programming can help you implement the SOLID design principles. I shall attend this as soon as possible \u00a0 \u00a0 \u00a0","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5347,"url":"https:\/\/monodes.com\/predaelli\/2019\/03\/12\/sugarizer-the-leading-learning-platform-for-children\/","url_meta":{"origin":4917,"position":5},"title":"Sugarizer | The leading learning platform for children","author":"Paolo Redaelli","date":"2019-03-12","format":false,"excerpt":"Sugarizer - The leading learning platform for children Sugarizer is a free\/libre learning platform. The Sugarizer UI use ergonomic principles from Sugar platform, developed for the One Laptop per Child project and used every day by more than 2 million children around the world.","rel":"","context":"In &quot;Software Libero&quot;","block_context":{"text":"Software Libero","link":"https:\/\/monodes.com\/predaelli\/category\/software\/software-libero\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/4917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=4917"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/4917\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=4917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=4917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=4917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}