{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blog/is-the-liskov-substitution-principle-really-useful/","result":{"data":{"site":{"siteMetadata":{"name":"Huzaifa Rasheed","title":"Huzaifa Rasheed","description":"Software Engineer","about":"\n      Hey, I'm Huzaifa.\n      <br/><br/>\n      Engineer by trade, builder by instinct - I believe in owning my stack, shipping fast, and occasionally running on chai (tea) and stubbornness. I work best in that sweet spot between deep focus and fast feedback - solo or in sync with a good team.\n      <br/><br/>\n      This site's my little corner of the internet - part portfolio, part lab - where I document what I build, break, or learn.\n      <br/><br/>\n      Outside work, I'm into long walks, pixel-perfect headshots in FPS games (eventually... maybe), plants I probably overwater, and the occasional \"classified\" hobby to stay curious.\n      <br/><br/>\n      Reach out anytime - my digital door's always open. 👋\n    ","twitter":"https://twitter.com/huzRasheed","github":"https://github.com/huzaifa-99","linkedin":"https://www.linkedin.com/in/huzaifa-rasheed/","devto":"https://dev.to/huzaifa99","stackoverflow":"https://stackoverflow.com/users/12579290/huzaifa","leetcode":"https://leetcode.com/rhuzaifa","discord":"https://discordapp.com/users/rhuzaifa","email":"dev@rhuzaifa.com","projects":[{"name":"FFMpeg Web","description":"An experimental browser-based terminal that runs FFmpeg using WebAssembly, enabling media processing directly in the browser.","link":"https://ffmpeg-web.rhuzaifa.com/","github":"https://github.com/huzaifa-99/ffmpeg-web"},{"name":"Feed base 2","description":"A mini browser game where players manipulate 4-bit binary blocks to match target BCD values - part puzzle, part binary logic trainer.","link":"https://feedbase2.rhuzaifa.com/","github":"https://github.com/huzaifa-99/feed-base-2"},{"name":"Fabric browser extension","description":"A Chrome extension that injects engineered Fabric prompts directly into the ChatGPT interface for enhanced workflow automation.","link":"https://github.com/huzaifa-99/fabric-browser-extension","github":"https://github.com/huzaifa-99/fabric-browser-extension"},{"name":"Pure Cinema","description":"An experimental, tongue-in-cheek text-to-video generator that stitches together footage, synthesized voiceovers, and background music with a Node.js + ffmpeg pipeline. Not quite Hollywood, but it renders.","link":"https://cinema.rhuzaifa.com","github":null},{"name":"Aria2c Packload","description":"A Bash script for bulk downloading magnet links or torrents using aria2c - optimized for series or list-based transfers.","link":"https://github.com/huzaifa-99/aria2c-packload","github":"https://github.com/huzaifa-99/aria2c-packload"},{"name":"RSS Watchdog","description":"A lightweight Bash script that watches RSS/Atom feeds and compiles a Markdown-based reading checklist for Unix systems.","link":"https://github.com/huzaifa-99/rss-watchdog","github":"https://github.com/huzaifa-99/rss-watchdog"},{"name":"QuoteGen","description":"A quote graphic generator that produces stylized quote images with random selection and a built-in editor for customization.","link":"https://quotegen.rhuzaifa.com/","github":null},{"name":"WebRTC Video Chat","description":"A basic WebRTC-powered app enabling peer-to-peer video and audio calls between two users.","link":"https://webrtc-video-chat.rhuzaifa.com/","github":null}],"experience":null,"skills":[{"name":"Languages & Frameworks","description":"JavaScript, TypeScript, Python, Bash - Frameworks include Node.js, React, Next.js, Vue, React Native, FastAPI."},{"name":"Databases & Storage","description":"PostgreSQL, MySQL, MongoDB - Experience with schema design, indexing, query optimization, and migrations."},{"name":"Cloud & Infrastructure","description":"AWS (EC2, RDS, S3, Lambda), Vercel, Netlify, Heroku - Comfortable with serverless, autoscaling, and cost optimization."},{"name":"DevOps & Tooling","description":"Docker, Git, CI/CD pipelines (GitHub Actions, GitLab CI) - Experience with observability, containerization, and release workflows."},{"name":"Testing & QA Automation","description":"Jest, Playwright, Puppeteer, Selenium - Focus on E2E testing, mocking APIs, and maintaining test coverage."}]}},"markdownRemark":{"id":"35eb0c96-b56a-59da-b27a-dffdc412bcb2","excerpt":"Repost of https://dev.to/rhuzaifa/is-the-liskov-substitution-principle-really-useful-3oce Liskov Substitution is part of SOLID Design. SOLID? SOLID are…","html":"<blockquote>\n<p>Repost of <a href=\"https://dev.to/rhuzaifa/is-the-liskov-substitution-principle-really-useful-3oce\">https://dev.to/rhuzaifa/is-the-liskov-substitution-principle-really-useful-3oce</a></p>\n</blockquote>\n<p>Liskov Substitution is part of SOLID Design. <strong>SOLID?</strong></p>\n<blockquote>\n<p>SOLID are 5 software development principles or guidelines based on Object-Oriented design making it easier for you to make your projects scalable and maintainable.</p>\n</blockquote>\n<p>Think of them as best practices.</p>\n<h3>Now What is Liskov Substitution</h3>\n<p>You see the <strong>L</strong> in SOLID stands for this principle.</p>\n<p>It says</p>\n<blockquote>\n<p>Let Φ(x) be a property provable about objects x of type T. Then Φ(y) should be true for objects y of type S where S is a subtype of T. <strong><a href=\"https://en.wikipedia.org/wiki/Barbara_Liskov\">Barbara Liskov</a></strong></p>\n</blockquote>\n<p>Honestly, Too scientific.</p>\n<p>In simple terms</p>\n<blockquote>\n<p>Replacing an instance of a class with its child class should not produce any negative side effects or broken codebase.</p>\n</blockquote>\n<h4>Meaning<br/></h4>\n<p>✔️ Can use the subclass of a parent class just the same as using the parent class without breaking anything.<br/>\n✔️ Subclasses can modify/override parent class methods.<br/>\n❌ Subclasses can modify the parent’s method signature like arguments, return type, and exceptions.<br/>\n❌ Subclasses can define a new function not present in the parent class.<br/>\n❌ Parent class can be modified.<br/></p>\n<h3>Why do this?</h3>\n<p>The Goal of this principle is to basically prevent our old codebase from breaking due to new code. This is also in line with the <strong><a href=\"/blog/why-you-should-be-following-the-single-responsibility-principle\">Single Responsibility</a></strong> and the <strong><a href=\"/blog/explained-open-close-principle-in-2-minutes\">Open Close Principle.</a></strong></p>\n<p>We will use a simple example for explanation.</p>\n<h3>A Simple Use Case</h3>\n<p>The following example violates the rule.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">Animal</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">eat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// common functionality</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Eating Now\"</span>\t<span class=\"token comment\">// return type string</span>\n\t<span class=\"token punctuation\">}</span>\n\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">sleep</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// common functionality</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"I am sleeping\"</span>\t<span class=\"token comment\">// return type string</span>\n\t<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">Cat</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">Animal</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">eat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// ... cat specific code</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Meow, whatever human\"</span>\t<span class=\"token comment\">// return type string</span>\n\t<span class=\"token punctuation\">}</span>\n\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">sleep</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// ... cat specific code</span>\n\n\t\t<span class=\"token comment\">//  voilating LSP: parnet sleep() does not return boolean</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token boolean\">true</span> \n\t<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">Dog</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">Animal</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">eat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// ... dog specific code</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Woof, It was tasty.\"</span>\t<span class=\"token comment\">// return type string</span>\n\t<span class=\"token punctuation\">}</span>\n\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">sleep</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// ... dog specific code</span>\n\t\t\n\t\t<span class=\"token comment\">//  voilating LSP: parent sleep() doesn't use Error Exception</span>\n\t\t<span class=\"token keyword\">throw</span> <span class=\"token function\">Error</span><span class=\"token punctuation\">(</span><span class=\"token string\">'I just slept'</span><span class=\"token punctuation\">)</span> \n\t<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<hr>\n<p>With the Liskov Substitution Principle, we would modify our code as follow</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">Animal</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">eat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// common functionality</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Eating Now\"</span>\t<span class=\"token comment\">// return type string</span>\n\t<span class=\"token punctuation\">}</span>\n\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">sleep</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// common functionality</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"I am sleeping\"</span>\t<span class=\"token comment\">// return type string</span>\n\t<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">Cat</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">Animal</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">eat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// ... cat specific code</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Meow, whatever human\"</span>\t<span class=\"token comment\">// return type string</span>\n\t<span class=\"token punctuation\">}</span>\n\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">sleep</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// ... cat specific code</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"I am already sleeping\"</span>\t<span class=\"token comment\">// return type string</span>\n\t<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">Dog</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">Animal</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">eat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// ... dog specific code</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Woof, It was actually tasty.\"</span>\t<span class=\"token comment\">// return type string</span>\n\t<span class=\"token punctuation\">}</span>\n\n\t<span class=\"token keyword\">function</span> <span class=\"token function\">sleep</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token comment\">// ... dog specific code</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Zzzzzzzz\"</span>\t<span class=\"token comment\">// return type string</span>\n\t<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>With this approach, we can swap parent and child classes without breaking the code.</p>\n<h3>So Is it Helpful?</h3>\n<p>It is in most cases, but there are those cases where you might want to add some more that does not quite fit in like the <strong>Birds</strong> example below</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">Bird</span><span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">function</span> <span class=\"token function\">fly</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">Duck</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">Bird</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">Ostrich</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">Bird</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span> <span class=\"token comment\">// Duck can fly but ostrich cant:</span></code></pre></div>\n<p>So yeah, it really depends. If it’s getting over-complicated/over-engineered or is not making sense(like bird example) then it’s best to do your own thing.</p>\n<h4>Tip</h4>\n<p>It’s easy to extend old code with new code. You just have to make a new class and extend it with parent/base class without the fear of breaking the already working code. We also get this benefit from <strong><a href=\"/blog/why-the-dependency-inversion-principle-is-worth-using\">Dependency Inversion</a></strong> principle.</p>\n<hr>\n<p>So how do you see this? Do you think it’s really useful? Be sure to tell me your opinion in the comments.</p>","frontmatter":{"title":"Is The Liskov Substitution Principle Really Useful?","date":"March 29, 2021","description":"Discover the Liskov Substitution Principle of SOLID design, which ensures that subclasses can replace their parent classes without causing errors."}}},"pageContext":{"slug":"/blog/is-the-liskov-substitution-principle-really-useful/","previous":{"fields":{"slug":"/blog/why-you-should-be-following-the-single-responsibility-principle/"},"frontmatter":{"title":"WHY YOU SHOULD BE FOLLOWING THE SINGLE RESPONSIBILITY PRINCIPLE"}},"next":{"fields":{"slug":"/blog/explained-open-close-principle-in-2-minutes/"},"frontmatter":{"title":"Explained: OPEN/CLOSE Principle In 2 Minutes"}}}},"staticQueryHashes":["2276319502"]}