{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blog/2-simple-tips-to-clean-nested-if-else-conditions/","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":"971e9e44-c670-52b7-bfc5-60c3eab0ef8e","excerpt":"Repost of https://dev.to/rhuzaifa/2-simple-tips-to-clean-nested-if-else-conditions-2kn4 There might be a time when you have your if/else conditions like this…","html":"<blockquote>\n<p>Repost of <a href=\"https://dev.to/rhuzaifa/2-simple-tips-to-clean-nested-if-else-conditions-2kn4\">https://dev.to/rhuzaifa/2-simple-tips-to-clean-nested-if-else-conditions-2kn4</a></p>\n</blockquote>\n<p>There might be a time when you have your if/else conditions like this</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>age<span class=\"token operator\">></span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;</span> <span class=\"token number\">18</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Not an Adult\"</span>\n\t<span class=\"token punctuation\">}</span>\n\t<span class=\"token keyword\">else</span> <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>age <span class=\"token operator\">>=</span> <span class=\"token number\">18</span> <span class=\"token operator\">&amp;&amp;</span> age<span class=\"token operator\">&lt;</span><span class=\"token number\">60</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"An Adult\"</span>\n\t<span class=\"token punctuation\">}</span>\n\t<span class=\"token keyword\">else</span><span class=\"token punctuation\">{</span>\n\t\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Senior Citizen\"</span>\n\t<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token keyword\">else</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Age must be a valid number\"</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>This is however a simple example, this can get messy as your code logic gets complex. </p>\n<p>To add more, Multiple nested if/else increases <strong><a href=\"https://en.wikipedia.org/wiki/Cyclomatic_complexity\">Cyclomatic complexity</a></strong> and it is better to avoid it whenever possible.</p>\n<blockquote>\n<p>Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program’s source code. <strong><em>Thomas J. McCabe, Sr.</em></strong></p>\n</blockquote>\n<p>So what can you do to avoid it? Simple, Try to have fewer branches of if/else conditions achievable with the following tips.</p>\n<h2>The Tips</h2>\n<ol>\n<li>Guard Clauses</li>\n<li>Ternary Operators</li>\n</ol>\n<p>There are others like Switch Statement, Dictionaries, Jump Tables, etc. but those are beyond this article’s scope.</p>\n<h3>1. Guard Clauses</h3>\n<p>In simple terms, return early if a condition is not met. Again taking our previous code, we can modify it like this</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token comment\">// Guard clause</span>\n<span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Age must be a valid number\"</span> \n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;</span> <span class=\"token number\">18</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"Not an Adult\"</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token keyword\">else</span> <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;</span> <span class=\"token number\">60</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"An Adult\"</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">return</span> <span class=\"token string\">\"Senior Citizen\"</span></code></pre></div>\n<p>Or you could even do this</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token comment\">// Guard clause</span>\n<span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span> <span class=\"token string\">\"Age must be a valid number\"</span>\n\n<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;</span> <span class=\"token number\">18</span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span> <span class=\"token string\">\"Not an Adult\"</span>\n<span class=\"token keyword\">else</span> <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;</span> <span class=\"token number\">60</span><span class=\"token punctuation\">)</span>\t<span class=\"token keyword\">return</span> <span class=\"token string\">\"An Adult\"</span>\n<span class=\"token keyword\">else</span> <span class=\"token keyword\">return</span> <span class=\"token string\">\"Senior Citizen\"</span></code></pre></div>\n<p>Use brackets or not, scoped only to clean code.</p>\n<h3>2. Ternary Operators</h3>\n<p>Most of you already use them, but anyways. Modifying our last code</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span> <span class=\"token string\">\"Age must be a valid number\"</span>\n\n<span class=\"token keyword\">var</span> res <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;</span> <span class=\"token number\">18</span> <span class=\"token operator\">?</span> <span class=\"token string\">\"Not an Adult\"</span> \n\t\t<span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;</span> <span class=\"token number\">60</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">?</span> <span class=\"token string\">\"An Adult\"</span> \n\t\t<span class=\"token operator\">:</span> <span class=\"token string\">\"Senior Citizen\"</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">return</span> res</code></pre></div>\n<p>or</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span> <span class=\"token string\">\"Age must be a valid number\"</span>\n\n<span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;</span> <span class=\"token number\">18</span> <span class=\"token operator\">?</span> <span class=\"token string\">\"Not an Adult\"</span> <span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span>age <span class=\"token operator\">&lt;</span> <span class=\"token number\">60</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">?</span> <span class=\"token string\">\"An Adult\"</span> <span class=\"token operator\">:</span> <span class=\"token string\">\"Senior Citizen\"</span><span class=\"token punctuation\">)</span></code></pre></div>\n<hr>\n<p><strong>Disclaimer</strong>: Clean code doesn’t always mean Performant code or Simple Code. Sometimes you have to decide between one or the other. There is however a limit to how clean you can make your code.</p>\n<hr>\n<p>So here it is guys, do you use any other method to clean nesting? Be sure to tell me in the comments. </p>","frontmatter":{"title":"2 Simple Tips To Clean Nested IF/ELSE Conditions.","date":"March 30, 2021","description":"Make sure your conditional logic is readable"}}},"pageContext":{"slug":"/blog/2-simple-tips-to-clean-nested-if-else-conditions/","previous":{"fields":{"slug":"/blog/explained-open-close-principle-in-2-minutes/"},"frontmatter":{"title":"Explained: OPEN/CLOSE Principle In 2 Minutes"}},"next":{"fields":{"slug":"/blog/is-it-practical-to-use-interface-segregation-principle/"},"frontmatter":{"title":"Is It Practical To Use Interface Segregation Principle?"}}}},"staticQueryHashes":["2276319502"]}