{"id":2662,"date":"2023-07-16T09:19:44","date_gmt":"2023-07-16T07:19:44","guid":{"rendered":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2662"},"modified":"2023-07-18T10:35:09","modified_gmt":"2023-07-18T08:35:09","slug":"single-responsibility","status":"publish","type":"post","link":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2662","title":{"rendered":"Single Responsibility Principle"},"content":{"rendered":"<p style=\"text-align: justify;\">Under this statement hides principle which developers should be familiar with, since creating functions\/methods. This principle states, that each class should be responsible for <strong>one&nbsp;<\/strong>specified thing. Let&#8217;s have a look at code below:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nclass Person\n{\n    public string Name { get; set; }\n    public string LastName { get; set; }\n    public string City { get; set; }\n    public string Street { get; set; }\n    public int HouseNumber { get; set; }\n    public string Email { get; set; }\n    \n    public Person(string name, string lastname, string email)\n    {\n        Name = name;\n        LastName = lastname;\n        Email = ValidateEmail(email);\n    }\n    \n    public static bool ValidateEmail(string email)\n    {\n        string pattern = @&quot;^&#91;a-zA-Z0-9._%+-]+@&#91;a-zA-Z0-9.-]+\\.&#91;a-zA-Z]{2,}$&quot;;\n\n       \n        bool isValid = Regex.IsMatch(email, pattern);\n\n        return isValid;\n    }\n}\n<\/pre><\/div>\n\n<p style=\"text-align: justify;\">What exactly is going on above? We have declared a class, which has tons of incompetently declared properties and <span style=\"background-color: #efefef;\">ValidateEmail<\/span> method which do not fit. Here comes the question \u2013 why is that?&nbsp;<\/p>\n<p style=\"text-align: justify;\">First of all \u2013 properties&nbsp;<span style=\"background-color: #efefef;\">City<\/span>, <span style=\"background-color: #efefef;\">Street<\/span>&nbsp;or&nbsp;<span style=\"background-color: #efefef;\">HouseNumber<\/span> from the beginning don\u2019t sound like Person&#8217;s property \u2013 they sound more like an address. We can assume, that given person may live there, but it is not certain &#8211; from programming point of view &#8211; these properties are not related with class name.&nbsp; Secondly \u2013 class <span style=\"background-color: #efefef;\">Person<\/span> contains <span style=\"background-color: #efefef;\">ValidateEmail<\/span>&nbsp;method which shouldn\u2019t be responsible for <span style=\"background-color: #efefef;\">Person<\/span> type.&nbsp;<\/p>\n<p style=\"text-align: justify;\">So what should be done? Crucial thing to achieve single responsibility principle is to split whole class to smaller classes, where every single class has it&#8217;s responsiblity :<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/creating Address class and re-creating properties\nclass Address\n{\n    public string City { get; set; }\n    public string Street { get; set; }\n    public int HouseNumber { get; set; }\n}\n\n\/\/creating Person class with Address's class properties\nclass Person\n{\n    public string Name { get; set; }\n    public string Lastname { get; set; }\n    public string Email { get; set; }\n    public Address PersonAddress { get; set; }\n    \n    public Person(string name, string lastname, string email)\n    {\n        Name = name;\n        Lastname = lastname;\n        Email = email;\n    }\n}\n\/\/separated ValidateEmail() method, converted to class with unchanged e-mail validation logic\nclass EmailValidator\n{\n    public static bool ValidateEmail(string email)\n    {\n        string pattern = @&quot;^&#91;a-zA-Z0-9._%+-]+@&#91;a-zA-Z0-9.-]+\\.&#91;a-zA-Z]{2,}$&quot;;\n\n       \n        bool isValid = Regex.IsMatch(email, pattern);\n\n        return isValid;\n    }\n}\n \/\/ running Main method\n public static void Main(string&#91;] args)\n    {\n        Console.WriteLine(&quot;Enter e-mail address&quot;);\n        string email = Console.ReadLine();\n\n        if (ValidateEmail(email))\n        {\n            Console.WriteLine(&quot;E-mail address correct&quot;);\n        }\n        else\n        {\n            Console.WriteLine(&quot;E-mail address incorrect&quot;);\n        }\n    }\n<\/pre><\/div>\n\n<p>But why is that principle so important?<\/p>\n<p style=\"text-align: justify;\">Lets assume, that our code is much bigger than shown above, and there somehow is a bug in our code. What would happen if there are many functionalities in our class that are responsible for things that these class shouldn&#8217;t be responsible for? Exactly &#8211; lots of code to review, overthinking what comes from etc. While having less functionalities <strong>but<\/strong>&nbsp;<strong>consistent<\/strong> with what class is responsible for, there is much less code to review and much less mess to clean up.<\/p>","protected":false},"excerpt":{"rendered":"<p>First of five SOLID principles explaining how important is Single Responsibility.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"elementor_theme","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[13],"tags":[],"class_list":["post-2662","post","type-post","status-publish","format-standard","hentry","category-solid-articles"],"_links":{"self":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2662"}],"version-history":[{"count":82,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2662\/revisions"}],"predecessor-version":[{"id":2835,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2662\/revisions\/2835"}],"wp:attachment":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}