{"id":211,"date":"2025-10-25T11:36:18","date_gmt":"2025-10-25T09:36:18","guid":{"rendered":"https:\/\/www.insync.co.za\/blog\/?p=211"},"modified":"2025-10-25T11:46:16","modified_gmt":"2025-10-25T09:46:16","slug":"solid-principles-in-c-clean-code-made-easy","status":"publish","type":"post","link":"https:\/\/www.insync.co.za\/blog\/2025\/10\/25\/solid-principles-in-c-clean-code-made-easy\/","title":{"rendered":"SOLID Principles in C#: Clean Code Made Easy"},"content":{"rendered":"\n<h1 class=\"wp-block-heading has-medium-font-size\"><strong>Writing maintainable, flexible, and scalable software is every developer\u2019s goal. The <strong>SOLID principles<\/strong> are a cornerstone of <strong>object-oriented programming (OOP)<\/strong> that help achieve this.<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s a breakdown with <strong>bad vs good examples<\/strong>, <strong>pros<\/strong>, and <strong>cons<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Single Responsibility Principle (SRP)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Definition:<\/strong><br>A class should have <strong>only one reason to change<\/strong> \u2014 it should handle a single responsibility.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Bad Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-f68d5dca12b7eaceb6d841ce0fb205ea\"><code>public class InvoiceProcessor\n{\n    public void GenerateInvoice(Invoice invoice) { \/* generate logic *\/ }\n    public void SaveInvoiceToDatabase(Invoice invoice) { \/* save logic *\/ }\n    public void SendInvoiceEmail(Invoice invoice) { \/* email logic *\/ }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Good Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-12131903f32921874eab8a2ed9466fa1\"><code>public class InvoiceGenerator\n{\n    public void Generate(Invoice invoice) { \/* generate logic *\/ }\n}\n\npublic class InvoiceRepository\n{\n    public void Save(Invoice invoice) { \/* save logic *\/ }\n}\n\npublic class InvoiceMailer\n{\n    public void Send(Invoice invoice) { \/* email logic *\/ }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Pros:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clear code responsibility<\/li>\n\n\n\n<li>Easier to <strong>maintain and test<\/strong><\/li>\n\n\n\n<li>Changes in one area don\u2019t affect others<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Cons:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>More classes to manage<\/li>\n\n\n\n<li>Slightly more complex initial design<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Open\/Closed Principle (OCP)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Definition:<\/strong><br>Software entities should be <strong>open for extension but closed for modification<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Bad Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-ae89032c4e854d43e8935a011fe24575\"><code>public class DiscountCalculator\n{\n    public double CalculateDiscount(Customer customer)\n    {\n        if (customer.Type == \"VIP\") return 0.2;\n        if (customer.Type == \"Regular\") return 0.1;\n        return 0;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Good Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-3540a031f82ca6b0d83948b773387c3a\"><code>public interface IDiscountStrategy\n{\n    double GetDiscount();\n}\n\npublic class VipDiscount : IDiscountStrategy { public double GetDiscount() =&gt; 0.2; }\npublic class RegularDiscount : IDiscountStrategy { public double GetDiscount() =&gt; 0.1; }\n\npublic class DiscountCalculator\n{\n    public double CalculateDiscount(IDiscountStrategy strategy) =&gt; strategy.GetDiscount();\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Pros:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Easy to add new features<\/li>\n\n\n\n<li>Reduces risk of <strong>breaking existing code<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Cons:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>More upfront <strong>design and interfaces<\/strong><\/li>\n\n\n\n<li>Slightly more complex for small projects<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Liskov Substitution Principle (LSP)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Definition:<\/strong><br>Objects of a subclass should be <strong>replaceable with objects of the parent class<\/strong> without breaking functionality.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Bad Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-520f3a0e92dab390cded0184586d09d5\"><code>public class Bird { public virtual void Fly() { } }\npublic class Ostrich : Bird { public override void Fly() { throw new NotImplementedException(); } }\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Good Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-80401ec86e45651cff4188318de0fb2e\"><code>public interface IFlyable { void Fly(); }\n\npublic class Sparrow : IFlyable { public void Fly() { \/* flying logic *\/ } }\n\npublic class Ostrich { \/* no Fly method since it cannot fly *\/ }\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Pros:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Correct abstraction<\/li>\n\n\n\n<li>Avoids <strong>runtime errors<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Cons:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Requires <strong>careful interface design<\/strong><\/li>\n\n\n\n<li>Can lead to <strong>more interfaces<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Interface Segregation Principle (ISP)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Definition:<\/strong><br>No client should be forced to depend on methods it <strong>does not use<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Bad Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-cc67b640df99002f3cac0cd460c733d5\"><code>public interface IMachine\n{\n    void Print();\n    void Scan();\n    void Fax();\n}\n\npublic class OldPrinter : IMachine\n{\n    public void Print() { \/* ok *\/ }\n    public void Scan() { throw new NotImplementedException(); }\n    public void Fax() { throw new NotImplementedException(); }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Good Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-b503de467ab61abee208dc69448710b8\"><code>public interface IPrinter { void Print(); }\npublic interface IScanner { void Scan(); }\npublic interface IFax { void Fax(); }\n\npublic class OldPrinter : IPrinter { public void Print() { \/* ok *\/ } }\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Pros:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Cleaner and <strong>focused interfaces<\/strong><\/li>\n\n\n\n<li>Easier to maintain and implement<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Cons:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>More interfaces to track<\/li>\n\n\n\n<li>Slightly higher design effort<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. Dependency Inversion Principle (DIP)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Definition:<\/strong><br>High-level modules should not depend on low-level modules; both should depend on <strong>abstractions<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Bad Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-49afbfe6b5862356247b5353ec5664e3\"><code>public class FileLogger { public void Log(string message) { } }\n\npublic class UserService\n{\n    private FileLogger _logger = new FileLogger();\n    public void CreateUser(string name) { _logger.Log(\"User created: \" + name); }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Good Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-4db8be4ebd231df5e7312952a27c869e\"><code>public interface ILogger { void Log(string message); }\n\npublic class FileLogger : ILogger { public void Log(string message) { } }\n\npublic class UserService\n{\n    private readonly ILogger _logger;\n    public UserService(ILogger logger) { _logger = logger; }\n    public void CreateUser(string name) { _logger.Log(\"User created: \" + name); }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Pros:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Promotes <strong>decoupling<\/strong><\/li>\n\n\n\n<li>Easy to swap implementations<\/li>\n\n\n\n<li>Improves <strong>testability<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Cons:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Requires <strong>dependency injection setup<\/strong><\/li>\n\n\n\n<li>Slightly more boilerplate code<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How Good or Bad is SOLID in Real Projects?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SOLID principles are widely regarded as <strong>best practices for writing maintainable, flexible, and testable code<\/strong>. They help developers design systems that can <strong>adapt to change without breaking existing functionality<\/strong>, making long-term maintenance much easier. Projects that follow SOLID tend to have <strong>clearer responsibilities, decoupled modules, and more reusable components<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, SOLID is not a silver bullet. In <strong>small projects or simple scripts<\/strong>, strictly following SOLID can lead to <strong>unnecessarily complex designs<\/strong>, with many classes and interfaces that add overhead without real benefit. Additionally, improper application\u2014like over-abstraction or creating too many tiny interfaces\u2014can make the code harder to understand for new developers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In practice, SOLID works best when <strong>applied thoughtfully<\/strong>, balancing <strong>clean design with pragmatism<\/strong>. It\u2019s a guideline, not a strict rulebook\u2014developers should <strong>adapt the principles to the size and complexity of the project<\/strong> rather than following them dogmatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>References:<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/archive\/msdn-magazine\/2014\/may\/csharp-best-practices-dangers-of-violating-solid-principles-in-csharp\">SOLID Principles in C# \u2013 Microsoft Docs<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Writing maintainable, flexible, and scalable software is every developer\u2019s goal. The SOLID principles are a cornerstone of object-oriented programming (OOP) that help achieve this. Here\u2019s a breakdown with bad vs good examples, pros, and cons. 1. Single Responsibility Principle (SRP) Definition:A class should have only one reason to change \u2014 it should handle a single [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":214,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[24,18],"tags":[37,30,20,19,28],"class_list":["post-211","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-business-2","category-software-development","tag-net","tag-c","tag-coding","tag-software","tag-software-development"],"_links":{"self":[{"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":2,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/posts\/211\/revisions\/216"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/media\/214"}],"wp:attachment":[{"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}