{"id":584,"date":"2025-11-14T20:32:43","date_gmt":"2025-11-15T01:32:43","guid":{"rendered":"https:\/\/www.ecahill.com\/staging\/3340\/?p=584"},"modified":"2026-01-27T19:42:38","modified_gmt":"2026-01-28T00:42:38","slug":"solid-coding-practices-an-interview-refresher","status":"publish","type":"post","link":"https:\/\/www.ecahill.com\/staging\/3340\/solid-coding-practices-an-interview-refresher\/","title":{"rendered":"SOLID Coding Practices: An Interview Refresher"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">SOLID Principles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><strong>S: Single Responsibility<\/strong><\/li>\n\n\n\n<li class=\"\"><strong>O: Open\/Closed<\/strong><\/li>\n\n\n\n<li class=\"\"><strong>L: Liskov Substitution<\/strong><\/li>\n\n\n\n<li class=\"\"><strong>I: Interface Segregation<\/strong><\/li>\n\n\n\n<li class=\"\"><strong>D: Dependency Inversion<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This post provides a concise summary of each principle with C# examples here and at <a href=\"https:\/\/github.com\/dotnetdiva\/SOLID\">https:\/\/github.com\/dotnetdiva\/SOLID<\/a> to refresh your memory before technical interviews or design discussions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Software design principles assist developers to write systems that are modular, scalable, and easier to maintain. Among the most widely recognised are the SOLID principles, a set of five guidelines introduced by Robert C. Martin to promote high-quality object-oriented design.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For interview preparation, understand each principle\u2019s intent and be able to discuss an example of implementation. The expected output is shown in GitHub for the code samples, but the key to understanding is to walk through the code and see how it applies to each principle.<\/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<h3 class=\"wp-block-heading\"><strong>Definition<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A class should have only one reason to change, meaning it should perform a single, well-defined task.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Consider a class that processes user data <em>and<\/em> writes it to a file. These are two distinct responsibilities. A better approach is to separate them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"language-csharp\">public class UserDataProcessor\n{\n    public void Process(User user)\n    {\n        \/\/ logic\n    }\n}\n\npublic class FileWriter\n{\n    public void SaveToFile(string data)\n    {\n        \/\/ logic\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why it matters<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This separation improves maintainability and reduces the likelihood of introducing unintended side effects when modifications occur.<\/p>\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<h3 class=\"wp-block-heading\"><strong>Definition<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Software entities should be open for extension but closed for modification.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of modifying an existing class to add new behaviour, use inheritance or composition to extend functionality:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"language-csharp\">public interface IShape\n{\n    double Area();\n}\n\npublic class Rectangle : IShape\n{\n    public double Width { get; set; }\n    public double Height { get; set; }\n    public double Area() =&gt; Width * Height;\n}\n\npublic class Circle : IShape\n{\n    public double Radius { get; set; }\n    public double Area() =&gt; Math.PI * Radius * Radius;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why it matters<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This principle supports flexibility and reduces the risk of regression when new features are introduced.<\/p>\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<h3 class=\"wp-block-heading\"><strong>Definition<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Objects of a derived class should be substitutable for objects of the base class without affecting program correctness.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This concept was introduced by <strong>Barbara Liskov and Jeannette Wing (1994)<\/strong>, who defined behavioural subtyping as a requirement that subtypes preserve correctness. Robert C. Martin later incorporated this into SOLID to ensure that inheritance models support predictable substitution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A violation of LSP occurs when a subclass contradicts the expectations set by the base class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"language-csharp\">public class Bird\n{\n    public virtual void Fly() =&gt; Console.WriteLine(\"Flying\");\n}\n\npublic class Penguin : Bird\n{\n    public override void Fly() =&gt;\n        throw new NotSupportedException(\"Penguins cannot fly\");\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, substituting a <code>Penguin<\/code> where a <code>Bird<\/code> is expected breaks behaviour. A better design might separate <code>FlyingBird<\/code> and <code>NonFlyingBird<\/code> abstractions.<\/p>\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<h3 class=\"wp-block-heading\"><strong>Definition<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Clients should not be forced to depend on interfaces they do not use.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Large, general-purpose interfaces should be divided into smaller, role-specific ones:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"language-csharp\">\npublic interface IPrinter\n{\n    void Print();\n}\n\npublic interface IScanner\n{\n    void Scan();\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why it matters<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This principle promotes cleaner, more modular interfaces and prevents unnecessary dependencies between unrelated functionalities.<\/p>\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<h3 class=\"wp-block-heading\"><strong>Definition<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">High-level modules should not depend on low-level modules. Both should depend on abstractions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A class should depend on an interface rather than a concrete implementation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"language-csharp\">\npublic interface IMessageService\n{\n    void Send(string message);\n}\n\npublic class EmailService : IMessageService\n{\n    public void Send(string message) =&gt;\n        Console.WriteLine($\"Email: {message}\");\n}\n\npublic class Notification\n{\n    private readonly IMessageService _service;\n\n    public Notification(IMessageService service)\n    {\n        _service = service;\n    }\n\n    public void SendAlert(string message) =&gt;\n        _service.Send(message);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why it matters<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This approach enhances testability, promotes loose coupling, and supports dependency injection \u2014 a core practice in modern software frameworks.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Summary<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding the SOLID principles strengthens your ability to design maintainable, scalable, and testable systems. During interviews, be prepared to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\">Explain the intent of each principle<\/li>\n\n\n\n<li class=\"\">Provide a concise code example<\/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\">Sources<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"\">Robert C. Martin, <em>Design Principles and Design Patterns<\/em>, Object Mentor (2000).<\/li>\n\n\n\n<li class=\"\">Barbara Liskov &amp; Jeannette Wing, <em>A Behavioral Notion of Subtyping<\/em>, ACM TOPLAS 16(6), 1811\u20131841 (1994). <a href=\"https:\/\/doi.org\/10.1145\/197320.197383\">https:\/\/doi.org\/10.1145\/197320.197383<\/a><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>SOLID Principles This post provides a concise summary of each principle with C# examples here and at https:\/\/github.com\/dotnetdiva\/SOLID to refresh your memory before technical interviews or design discussions. Software design principles assist developers to write systems that are modular, scalable, and easier to maintain. Among the most widely recognised are the SOLID principles, a set [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"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},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[38,41,42,39,40,20,37,43],"class_list":["post-584","post","type-post","status-publish","format-standard","hentry","category-software-development","tag-c-programming","tag-clean-code","tag-design-principles","tag-oop","tag-software-architecture","tag-software-engineering","tag-solid-principles","tag-technical-interviews"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p4aJd0-9q","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.ecahill.com\/staging\/3340\/wp-json\/wp\/v2\/posts\/584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ecahill.com\/staging\/3340\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ecahill.com\/staging\/3340\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecahill.com\/staging\/3340\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecahill.com\/staging\/3340\/wp-json\/wp\/v2\/comments?post=584"}],"version-history":[{"count":17,"href":"https:\/\/www.ecahill.com\/staging\/3340\/wp-json\/wp\/v2\/posts\/584\/revisions"}],"predecessor-version":[{"id":691,"href":"https:\/\/www.ecahill.com\/staging\/3340\/wp-json\/wp\/v2\/posts\/584\/revisions\/691"}],"wp:attachment":[{"href":"https:\/\/www.ecahill.com\/staging\/3340\/wp-json\/wp\/v2\/media?parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecahill.com\/staging\/3340\/wp-json\/wp\/v2\/categories?post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecahill.com\/staging\/3340\/wp-json\/wp\/v2\/tags?post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}