{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blog/why-you-should-be-following-the-single-responsibility-principle/","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":"984e46ca-f296-5009-83d0-3a32e8b2f23d","excerpt":"Repost of https://dev.to/huzaifa99/why-you-should-be-following-the-single-responsibility-principle-1h2a For those of you already familiar with the SOLID Design…","html":"<blockquote>\n<p>Repost of <a href=\"https://dev.to/huzaifa99/why-you-should-be-following-the-single-responsibility-principle-1h2a\">https://dev.to/huzaifa99/why-you-should-be-following-the-single-responsibility-principle-1h2a</a></p>\n</blockquote>\n<p>For those of you already familiar with the SOLID Design Principles, this won’t be much of a new thing. For newbies however</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>They are more like best practices rather than a rule. </p>\n<h1>SINGLE RESPONSIBILITY</h1>\n<p>The S in SOLID.</p>\n<p>It says</p>\n<blockquote>\n<p>A class should have one, and only one reason to change.</p>\n</blockquote>\n<p>The goal is to separate behaviors or concerns. Everything has its own place and it should be placed there.</p>\n<p>Basically </p>\n<ol>\n<li>Make smaller code modules (class/function etc) </li>\n<li>Module does a specific thing only and is liable for that only.</li>\n<li>It is named precisely for what it does.</li>\n</ol>\n<p>As opposed to Large Generic classes with different responsibilities and behaviors.</p>\n<p>This pattern can be seen in other <strong>SOLID</strong> principles like <strong><a href=\"/blog/is-it-practical-to-use-interface-segregation-principle\">Interface Segregation</a></strong> where we focus on making role-specific interfaces.</p>\n<h2>A SIMPLE USE CASE</h2>\n<p>Let’s say we are saving a random user’s data to some database. This is what we would normally do inside a function(pseudocode-ish).</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">function</span> <span class=\"token function\">createUser</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">userData</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// 1. validate user data for email and password keys</span>\n   \n    <span class=\"token comment\">// 2. check if the email is already registered</span>\n\n    <span class=\"token comment\">// 3. save the user to DB </span>\n\n    <span class=\"token comment\">// 4. return saved user as a response</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>With the <strong>SINGLE RESPONSIBILITY</strong> approach we will make dedicated functions that would handle specific logic and be responsible for it only</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">function</span> <span class=\"token function\">validateUser</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">user</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// will validate userObject for email and password keys</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">isEmailRegistered</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">email</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// will check if the email is already taken </span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">saveUserData</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">user</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// will save user data to db</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">createUser</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">userData</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n    <span class=\"token function\">validateUser</span><span class=\"token punctuation\">(</span>userData<span class=\"token punctuation\">)</span>\n   \n    <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span><span class=\"token function\">isEmailTaken</span><span class=\"token punctuation\">(</span>userData<span class=\"token punctuation\">.</span>email<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n      <span class=\"token comment\">// throw some error here</span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token keyword\">var</span> user <span class=\"token operator\">=</span> <span class=\"token function\">saveUserData</span><span class=\"token punctuation\">(</span>userData<span class=\"token punctuation\">)</span>\n    \n    <span class=\"token keyword\">return</span> user\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>This is a simple example but when dealing with multiple validations and complex logic this type of approach is really handy.</p>\n<h1>WHY USE IT</h1>\n<p>Consider these </p>\n<ol>\n<li>Easy to follow and Understand</li>\n<li>Debugging made easy</li>\n<li>Removing and refactoring is much simpler than before. </li>\n<li>You can make bigger changes without fear. </li>\n<li>It is scalable and maintainable</li>\n</ol>\n<p>Also when there is a time you need-to/have-to/want-to/are-forced-to make changes and have followed <strong>SINGLE RESPONSIBILITY</strong>, then it will be much quick and easy. </p>\n<p>What if you break something or there is a bug? Well, it is Easily traceable following this principle and if you break something, you break that one thing only, not an entire system.</p>\n<p>Not to mention that this rule helps a lot in the implementation of other SOLID principles like <strong><a href=\"/blog/why-the-dependency-inversion-principle-is-worth-using\">Dependency Inversion</a></strong> and the <strong><a href=\"/blog/is-the-liskov-substitution-principle-really-useful\">Liskov Substitution Principle.</a></strong></p>\n<h3>Tip</h3>\n<p>If you are someone that writes tests for their project then you would surely love this rule, it gives you predictable behavior and control.</p>\n<hr>\n<p>Here it is guys, I hope I explained it in a simple and easy way. Do you use SOLID principles in your projects? Be sure to comment below.</p>\n<h3>What’s Next</h3>\n<p>Laern about <strong><a href=\"/blog/explained-open-close-principle-in-2-minutes\">Open/Close Principle</a></strong> Principle in 2 Minutes.</p>","frontmatter":{"title":"WHY YOU SHOULD BE FOLLOWING THE SINGLE RESPONSIBILITY PRINCIPLE","date":"March 28, 2021","description":"Discover the Single Responsibility Principle of SOLID design, which ensures that change in a OOP class has only one reason."}}},"pageContext":{"slug":"/blog/why-you-should-be-following-the-single-responsibility-principle/","previous":{"fields":{"slug":"/blog/top-5-node-express-boilerplates-for-building-restful-apis-in-2021/"},"frontmatter":{"title":"TOP 5 Node-Express Boilerplates For Building RESTful API's In 2021"}},"next":{"fields":{"slug":"/blog/is-the-liskov-substitution-principle-really-useful/"},"frontmatter":{"title":"Is The Liskov Substitution Principle Really Useful?"}}}},"staticQueryHashes":["2276319502"]}