{"id":390,"date":"2023-08-14T13:53:36","date_gmt":"2023-08-14T19:53:36","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=390"},"modified":"2023-08-21T09:55:42","modified_gmt":"2023-08-21T15:55:42","slug":"solid-principles-in-c-with-analogies","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/solid-principles-in-c-with-analogies\/","title":{"rendered":"SOLID principles in C# with analogies"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>SOLID principles are a set of five important guidelines in software design that help developers create more maintainable and flexible code. <\/p>\n\n\n\n<p>Each principle focuses on a different aspect of writing good software. A break down of each principle in simple terms, is as follows:<\/p>\n\n\n\n<ol>\n<li><strong>Single Responsibility Principle (SRP):<\/strong><br>Imagine you&#8217;re building a house. The Single Responsibility Principle suggests that each worker should have a specific job. In code, it means that a class should have just one main job or responsibility. If you&#8217;re making a C# program to manage books, you&#8217;d have a class for managing books and another for displaying them. This way, if something changes in one area, it won&#8217;t affect the other unnecessarily.<\/li>\n\n\n\n<li><strong>Open\/Closed Principle (OCP):<\/strong><br>Think of a puzzle piece that fits perfectly into a slot. The Open\/Closed Principle tells us that our code should be like that puzzle piece \u2013 open for extension but closed for modification. In C#, it means that you should design your classes so that you can add new features without changing the existing code. If you&#8217;re creating shapes, you&#8217;d create a base shape class and then extend it to add new shapes, without changing how the existing shapes work.<\/li>\n\n\n\n<li><strong>Liskov Substitution Principle (LSP):<\/strong><br>Imagine a remote control for a TV. You&#8217;d expect that any remote control works seamlessly with any TV. In code, the Liskov Substitution Principle states that objects of a derived class should be able to replace objects of the base class without affecting the program&#8217;s correctness. In C#, if you have a base class for animals and derived classes for cats and dogs, you&#8217;d want to be able to treat them as animals interchangeably without breaking anything.<\/li>\n\n\n\n<li><strong>Interface Segregation Principle (ISP):<\/strong><br>Picture a menu in a restaurant \u2013 it only lists dishes from a specific category. The Interface Segregation Principle suggests that classes should not be forced to implement interfaces they don&#8217;t need. In C#, if you&#8217;re creating a reporting system with multiple report types, you&#8217;d create separate interfaces for each report type, so that a class implementing one report doesn&#8217;t have to implement unnecessary methods from other report types.<\/li>\n\n\n\n<li><strong>Dependency Inversion Principle (DIP):<\/strong><br>Think of a plug and socket \u2013 they fit together regardless of whether the power source is a generator or a power plant. The Dependency Inversion Principle says that high-level modules (like your main application logic) should not depend on low-level modules (like specific implementations). Instead, they should both depend on abstractions (like interfaces or abstract classes). In C#, if you&#8217;re creating a logging system, your main application shouldn&#8217;t directly depend on a specific logging library; it should depend on an abstraction that various logging libraries can implement.<\/li>\n<\/ol>\n\n\n\n<p>These principles might seem a bit abstract at first, but applying them to C# code can lead to software that&#8217;s easier to understand, maintain, and adapt as projects evolves.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SOLID principles are a set of five important guidelines in software design that help developers create more maintainable and flexible code. Each principle focuses on a different aspect of writing good software. A break down of each principle in simple terms, is as follows: These principles might seem a bit abstract at first, but applying [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":459,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/390"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=390"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/390\/revisions"}],"predecessor-version":[{"id":391,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/390\/revisions\/391"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/459"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}