{"id":3197,"date":"2024-04-23T12:25:44","date_gmt":"2024-04-23T12:25:44","guid":{"rendered":"https:\/\/www.infobip.com\/developers\/?p=3197"},"modified":"2024-04-23T13:42:07","modified_gmt":"2024-04-23T13:42:07","slug":"we-dont-write-else-blocks","status":"publish","type":"post","link":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks","title":{"rendered":"We don&#8217;t write &#8216;else&#8217; blocks"},"content":{"rendered":"<div class=\"post__image\" style=\"--aspect-ratio: 1.85\/1\"><figure class=\"wp-block-post-featured-image\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"512\" src=\"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png\" class=\"attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" style=\"object-fit:cover;\" srcset=\"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png 800w, https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block-300x192.png 300w, https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block-768x492.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure><div class=\"post-image__fullscreen\"><img decoding=\"async\" src=\"https:\/\/www.infobip.com\/developers\/wp-content\/themes\/infobip_developers\/assets\/images\/icons\/fullscreen.svg\" alt=\"\"><\/div><\/div>\n\n\n<p>When I joined Infobip, I was introduced to the team&#8217;s coding rules. One of them emphasized using &#8216;else&#8217; blocks sparingly.<\/p>\n\n\n\n<p>The rule seemed interesting, but understandable at the same time. <strong>Conditionals are essential, but overuse can clutter the code<\/strong>.<\/p>\n\n\n\n<p>So, I aimed to keep my conditional blocks concise.&nbsp;I opened my first pull request with a code similar to this one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void performTextAnalysis(String text) {\n    AnalysisResult result = analyzer.analyze(text);\n     \n    if (result.isSuccessful()) {\n        successfulAnalysisCounter.increment();\n    } else {    \n        failedAnalysisCounter.increment();\n    }\n     \n    results.save(result);\n}\n\n<\/code><\/pre>\n\n\n\n<p>After completing some analysis, we increased the relevant counter and saved the results.<\/p>\n\n\n\n<p>But then, <strong>a colleague suggested I rewrite the code without using the &#8216;else&#8217; block<\/strong>. I was confused, so I asked for clarification. Their response? We don&#8217;t write &#8216;else&#8217; blocks!?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>A voice of reason<\/strong><\/h2>\n\n\n\n<p>This response got under my skin. I believed the code I had written was crystal clear, far more so than the alternative proposed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void performTextAnalysis(String text) {\n    AnalysisResult result = analyzer.analyze(text);\n     \n    recordAnalysisMetrics(result);\n     \n    results.save(result);\n}\n \nprivate void recordAnalysisMetrics(AnalysisResult result) {\n    if (result.isSuccessful()) {\n        successfulAnalysisCounter.increment();\n        return;\n    }\n \n    failedAnalysisCounter.increment();\n}\n<\/code><\/pre>\n\n\n\n<p>I readied my sword and shield, prepared to defend my pristine code at any cost.<\/p>\n\n\n\n<p>Just as I was about to dive into battle, my senior peer stepped in with a voice of reason. He acknowledged that my code was&nbsp;indeed&nbsp;readable, perhaps even slightly more than the alternative. Yet, he advocated for the no-else option, not because it made the code&nbsp;prettier,&nbsp;but because&nbsp;<strong>it simplified our coding standards<\/strong>.&nbsp;<\/p>\n\n\n\n<p>This argument sat well with me because I could imagine\u00a0a lot of\u00a0pull request arguments (coding preferences) resolved\u00a0easily. So\u00a0I\u00a0agreed with him and decided to concede this time.<\/p>\n\n\n\n<p>Despite the resulting code looking acceptable, I remained convinced that there would be instances where avoiding the &#8216;else&#8217; statement would lead to a convoluted mess. I would triumphantly point to my peers and&nbsp;say&nbsp;&#8216;See!&nbsp;This&nbsp;is why we should not avoid else statements&#8217;!&nbsp;<\/p>\n\n\n\n<p>So, <strong>let&#8217;s find a code example where using &#8216;else&#8217; really matters<\/strong>!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>No-else code examples<\/strong><\/h2>\n\n\n\n<p>Let&#8217;s start with some simple cases where &#8216;else&#8217; blocks are used.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Input validation<\/strong><\/p>\n\n\n\n<p>One example involves checking input validity. For instance, suppose the performTextAnalysis function expects non-trivial text as input.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void performTextAnalysis(String text) {\n    if (!isNullOrEmpty(text)) {\n        AnalysisResult result = analyzer.analyze(text);\n         \n        recordAnalysisMetrics(result);\n     \n       results.save(result);\n    } else {\n        throw new IllegalStateException(\"Text is empty\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>The current code, although straightforward, is readable. However, as requirements evolve and more code is added to the core of the method<strong>, the &#8216;else&#8217; block may become separated from its corresponding &#8216;if&#8217; condition<\/strong>.&nbsp;<\/p>\n\n\n\n<p>This could lead to the need for scrolling back and forth between the &#8216;else&#8217; block and the &#8216;if&#8217; condition to understand the code. By avoiding the use of &#8216;else&#8217;, you&nbsp;have no choice&nbsp;but to handle the exceptional branch first before delving into the core logic of the method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void performTextAnalysis(String text) {\n    if (isNullOrEmpty(text)) {\n        throw new IllegalStateException(\"Text is empty\");\n    }\n \n    AnalysisResult result = analyzer.analyze(text);\n     \n    recordAnalysisMetrics(result);\n     \n    results.save(result);\n}\n<\/code><\/pre>\n\n\n\n<p>It is convenient that throwing an exception automatically closes a conditional branch. What if we wanted to simply <strong>skip the analysis in case the text is empty<\/strong>? In that case, we would simply return instead of throwing an exception.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void performTextAnalysis(String text) {\n    if (isNullOrEmpty(text)) {\n        log.info(\"Text is empty. Skipping analysis...\");\n        return;\n    }\n \n    AnalysisResult result = analyzer.analyze(text);\n \n    recordAnalysisMetrics(result);\n \n    results.save(result);\n}\n<\/code><\/pre>\n\n\n\n<p>One could argue that the previous example simply demonstrates how&nbsp;<a href=\"https:\/\/refactoring.com\/catalog\/replaceNestedConditionalWithGuardClauses.html\">input validation should be done at the start of a method<\/a>, which is a valid point. Enforcing a rule like &#8216;validate input at the beginning of the function&#8217; could achieve the same result. A more general principle might be to handle simpler branches before delving into complex ones. However, none of these rules can match the straightforwardness of the &#8216;no else&#8217; rule.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Conditional business logic<\/strong><\/p>\n\n\n\n<p>In this scenario, there isn&#8217;t a &#8216;less important&#8217; branch that we can easily close early. All conditional branches are equally important. For example, there might be multiple analyses we can perform on a given text, depending on the alphabet used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void performTextAnalysis(String text) {\n    if (isNullOrEmpty(text)) {\n        throw new IllegalStateException(\"Text is empty\");\n    }\n \n    AnalysisResult result;\n    if (isLatin(text)) {\n        result = analyzer.analyzeLatin(text);\n    } else if (isCyrillic(text)) {\n        result = analyzer.analyzeCyrillic(text); \n    } else if (isChinese(text)) {\n        result = analyzer.analyzeChinese(text); \n    } else if (isArabic(text)) {\n        result = analyzer.analyzeArabic(text); \n    } else {\n        throw new IllegalArgumentException(\"Unrecognized alphabet\");\n    }\n     \n    recordAnalysisMetrics(result);\n     \n    results.save(result);\n}\n<\/code><\/pre>\n\n\n\n<p>In this case, <strong>we&nbsp;cannot&nbsp;exit the method in the if-block&nbsp;anymore<\/strong>&nbsp;since we would be bypassing all the subsequent logic.&nbsp;When the &#8216;no else&#8217; rule&nbsp;is enforced&nbsp;you do not&nbsp;have&nbsp;a&nbsp;choice,&nbsp;but to&nbsp;extract this block into a new method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void performTextAnalysis(String text) {\n    if (isNullOrEmpty(text)) {\n        throw new IllegalStateException(\"Text is empty\");\n    }\n \n    AnalysisResult result = doAnalyze(text);\n     \n    recordAnalysisMetrics(result);\n     \n    results.save(result);\n}\n \nprivate void doAnalyze(String text) {\n    if (isLatin(text)) {\n        return analyzer.analyzeLatin(text);\n    }\n    if (isCyrillic(text)) {\n        return analyzer.analyzeCyrillic(text); \n    }\n    if (isChinese(text)) {\n        return analyzer.analyzeChinese(text); \n    }\n    if (isArabic(text)) {\n        return analyzer.analyzeArabic(text); \n    }\n \n    throw new IllegalArgumentException(\"Unrecognized alphabet\");\n}\n<\/code><\/pre>\n\n\n\n<p>One could argue that <strong>this code could have been cleaner by<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using the latest Java pattern-matching syntax<\/li>\n\n\n\n<li>Separating language recognition from text analysis strategy<\/li>\n<\/ul>\n\n\n\n<p>However, the&nbsp;key&nbsp;point of this example was to demonstrate that <strong>the code became cleaner simply by adhering to&nbsp;a very basic&nbsp;rule<\/strong>. Why is this approach cleaner than having it inlined? Because the list of alphabets isn&#8217;t exhaustive, adding more cases could overwhelm the method, resulting in decreased readability.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>No else \u2192 No choice<\/strong><\/h2>\n\n\n\n<p>The more I worked with the \u2018no else\u2019 rule, the more I appreciated its benefits. <strong>It inherently guides you towards writing simpler methods<\/strong>. Sure,&nbsp;you can still create complex methods using only &#8216;if&#8217; blocks, but it requires deliberate effort and thought.&nbsp;<\/p>\n\n\n\n<p>As developers, we cherish having choices and&nbsp;tend to&nbsp;resist any form of constraint. However, constraints can be beneficial. Consider this:<\/p>\n\n\n\n<p>Why do modern languages no longer support GO TO statements?<\/p>\n\n\n\n<p>&nbsp;Why do developers dread friend functions in C++?<\/p>\n\n\n\n<p>Constraints&nbsp;serve to&nbsp;prevent developers from writing unreadable code while&nbsp;still&nbsp;enabling them to write good code.<\/p>\n\n\n\n<p>There are only&nbsp;<strong>two techniques&nbsp;required to write code without &#8216;else&#8217; blocks<\/strong>.<\/p>\n\n\n\n<p>1.&nbsp;&nbsp;&nbsp;&nbsp;Closing&nbsp;simple&nbsp;conditional branches early<\/p>\n\n\n\n<p>2.&nbsp;&nbsp;&nbsp;&nbsp;Extract more complex conditional blocks into new methods<\/p>\n\n\n\n<p>With these strategies,&nbsp;any code can be written&nbsp;without the need for &#8216;else&#8217;.&nbsp;However, there&#8217;s a scenario where&nbsp;<strong>avoiding &#8216;else&#8217; can noticeably impair code readability<\/strong>.&nbsp;<\/p>\n\n\n\n<p>Extracting methods incur a cost &#8211; the flow is interrupted, and the reader must navigate back and forth to comprehend the flow.&nbsp;This&nbsp;is why we&nbsp;typically keep higher-level concepts together while extracting lower-level ones into methods. In both examples mentioned earlier, we extracted lower-level concepts into new methods.<\/p>\n\n\n\n<p>But&nbsp;<strong>what if having a conditional is an integral part of the core business logic<\/strong>? For instance, when an analysis fails, we&nbsp;need to&nbsp;persist the text into a database for troubleshooting and re-processing purposes. When an analysis is successful, we need to&nbsp;send an&nbsp;email that the&nbsp;analysis&nbsp;has&nbsp;been completed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void performTextAnalysis(String text) {\n    if (isNullOrEmpty(text)) {\n        throw new IllegalStateException(\"Text is empty\");\n    }\n \n    AnalysisResult result = doAnalyze(String text);\n    recordAnalysisMetrics(result);\n     \n    if (result.isSuccessful()) {\n        emailService.notifyAnalysisComplete();\n    } else {     \n        notAnalyzableTexts.save(text);\n    }\n \n    results.save(result);\n}\n<\/code><\/pre>\n\n\n\n<p>In scenarios where a conditional is fundamental to the core business logic, finding an elegant solution can be challenging. We could:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Extract the code into a vaguely named method like &#8216;handleResult&#8217;<\/li>\n\n\n\n<li>Extract the code into an over-descriptive method like &#8216;recordAnalysisMetricsAndRetryIfAnalysisFailed&#8217;<\/li>\n<\/ol>\n\n\n\n<p><strong>Both options disrupt the flow of reading and can make the code less intuitive<\/strong>. While there are workarounds, they aren&#8217;t as straightforward as the two listed. This highlights that the &#8216;no else&#8217; rule isn&#8217;t flawless. Without actively considering the code we write, we can still end up with clunky solutions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The alternative<\/strong><\/h2>\n\n\n\n<p>Let&#8217;s examine the alternative approach and consider the kind of clunky code that <strong>could result without adhering to the &#8216;no else&#8217; rule<\/strong>.<\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void performTextAnalysis(String text) {\n    if (!isNullOrEmpty(text)) {\n           \n        AnalysisResult result;\n        if (isLatin(text)) {\n            result = analyzer.analyzeLatin(text);\n        } else if (isCyrillic(text)) {\n            result = analyzer.analyzeCyrillic(text); \n        } else if (isChinese(text)) {\n            result = analyzer.analyzeChinese(text); \n        } else if (isArabic(text)) {\n            result = analyzer.analyzeArabic(text); \n        } else {\n            throw new IllegalArgumentException(\"Unrecognized alphabet\");\n        }\n     \n        if (result.isSuccessful()) {\n            successfulAnalysisCounter.increment();\n            emailService.notifyAnalysisComplete();\n        } else {    \n            failedAnalysisCounter.increment();\n            notAnalyzableTexts.save(text);\n        }\n     \n       results.save(result);\n    } else {\n        throw new IllegalStateException(\"Text is empty\");\n    }\n}<\/code><\/pre>\n\n\n\n<p>While the code snippet provided may not seem overly cumbersome with its 27 lines, anyone who has dealt with legacy code knows<strong> the perils of encountering monstrous methods with overwhelming cyclomatic complexity<\/strong>. Each developer likely began with good intentions, but as the code evolved, so did its size and complexity.<\/p>\n\n\n\n<p>You might be confident in your coding practices, thinking, &#8216;I would never write methods like these.&#8217; And you might be correct; perhaps you wouldn&#8217;t. But can you make the same guarantee for your coworkers?<\/p>\n\n\n\n<p>I&#8217;m fortunate to work in a team where everyone shares my passion for clean coding, and I&#8217;m confident that we&#8217;ve outgrown the need for the &#8216;no else&#8217; rule. However, not every team or workplace enjoys such a luxury. <strong>Writing code without &#8216;else&#8217; provides a foolproof way to ensure clarity and maintainability<\/strong>, and you can&#8217;t go wrong with it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I joined Infobip, I was introduced to the team&#8217;s coding rules. One of them emphasized using &#8216;else&#8217; blocks sparingly.<\/p>\n","protected":false},"author":36,"featured_media":3219,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[28],"tags":[256],"coauthors":[174],"class_list":["post-3197","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-programming"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>We don&#039;t write &#039;else&#039; blocks<\/title>\n<meta name=\"description\" content=\"When I joined Infobip, I was introduced to the team&#039;s coding rules. One of them emphasized using &#039;else&#039; blocks sparingly.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"We don&#039;t write &#039;else&#039; blocks\" \/>\n<meta property=\"og:description\" content=\"When I joined Infobip, I was introduced to the team&#039;s coding rules. One of them emphasized using &#039;else&#039; blocks sparingly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks\" \/>\n<meta property=\"og:site_name\" content=\"Infobip Developers Hub\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/infobip\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-23T12:25:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-23T13:42:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Alen Kosanovic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@InfobipDev\" \/>\n<meta name=\"twitter:site\" content=\"@InfobipDev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alen Kosanovic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks\"},\"author\":{\"name\":\"Alen Kosanovic\",\"@id\":\"https:\/\/www.infobip.com\/developers\/#\/schema\/person\/31b120f4d24c734e6fd27681b0a0510d\"},\"headline\":\"We don&#8217;t write &#8216;else&#8217; blocks\",\"datePublished\":\"2024-04-23T12:25:44+00:00\",\"dateModified\":\"2024-04-23T13:42:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks\"},\"wordCount\":1254,\"publisher\":{\"@id\":\"https:\/\/www.infobip.com\/developers\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png\",\"keywords\":[\"programming\"],\"articleSection\":[\"Blog Post\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks\",\"url\":\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks\",\"name\":\"We don't write 'else' blocks\",\"isPartOf\":{\"@id\":\"https:\/\/www.infobip.com\/developers\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png\",\"datePublished\":\"2024-04-23T12:25:44+00:00\",\"dateModified\":\"2024-04-23T13:42:07+00:00\",\"description\":\"When I joined Infobip, I was introduced to the team's coding rules. One of them emphasized using 'else' blocks sparingly.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#primaryimage\",\"url\":\"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png\",\"contentUrl\":\"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png\",\"width\":800,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.infobip.com\/developers\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"We don&#8217;t write &#8216;else&#8217; blocks\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.infobip.com\/developers\/#website\",\"url\":\"https:\/\/www.infobip.com\/developers\/\",\"name\":\"Infobip Developers Hub\",\"description\":\"Build meaningful customer relationships across any channel\",\"publisher\":{\"@id\":\"https:\/\/www.infobip.com\/developers\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.infobip.com\/developers\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.infobip.com\/developers\/#organization\",\"name\":\"Infobip Developers Hub\",\"url\":\"https:\/\/www.infobip.com\/developers\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.infobip.com\/developers\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2023\/03\/Infobip_logo_favicon.png\",\"contentUrl\":\"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2023\/03\/Infobip_logo_favicon.png\",\"width\":696,\"height\":696,\"caption\":\"Infobip Developers Hub\"},\"image\":{\"@id\":\"https:\/\/www.infobip.com\/developers\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/infobip\/\",\"https:\/\/x.com\/InfobipDev\",\"https:\/\/www.youtube.com\/channel\/UCUPSTy53VecI5GIir3J3ZbQ\",\"https:\/\/github.com\/infobip-community\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.infobip.com\/developers\/#\/schema\/person\/31b120f4d24c734e6fd27681b0a0510d\",\"name\":\"Alen Kosanovic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.infobip.com\/developers\/#\/schema\/person\/image\/20b7763781ec59811d4a07b57b88def4\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1f800e46e256ff03fcb75817f407fa284411cbdaac405ad6a2dc234bf7537c76?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1f800e46e256ff03fcb75817f407fa284411cbdaac405ad6a2dc234bf7537c76?s=96&d=mm&r=g\",\"caption\":\"Alen Kosanovic\"},\"description\":\"Alen is a senior software developer at Infobip, with almost a decade of experience in pursuing clean code. When he's not at the keyboard, he wanders around the world with his family.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/kosani\/\"],\"url\":\"https:\/\/www.infobip.com\/developers\/blog\/author\/alen-kosanovic\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"We don't write 'else' blocks","description":"When I joined Infobip, I was introduced to the team's coding rules. One of them emphasized using 'else' blocks sparingly.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks","og_locale":"en_US","og_type":"article","og_title":"We don't write 'else' blocks","og_description":"When I joined Infobip, I was introduced to the team's coding rules. One of them emphasized using 'else' blocks sparingly.","og_url":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks","og_site_name":"Infobip Developers Hub","article_publisher":"https:\/\/www.facebook.com\/infobip\/","article_published_time":"2024-04-23T12:25:44+00:00","article_modified_time":"2024-04-23T13:42:07+00:00","og_image":[{"width":800,"height":512,"url":"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png","type":"image\/png"}],"author":"Alen Kosanovic","twitter_card":"summary_large_image","twitter_creator":"@InfobipDev","twitter_site":"@InfobipDev","twitter_misc":{"Written by":"Alen Kosanovic","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#article","isPartOf":{"@id":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks"},"author":{"name":"Alen Kosanovic","@id":"https:\/\/www.infobip.com\/developers\/#\/schema\/person\/31b120f4d24c734e6fd27681b0a0510d"},"headline":"We don&#8217;t write &#8216;else&#8217; blocks","datePublished":"2024-04-23T12:25:44+00:00","dateModified":"2024-04-23T13:42:07+00:00","mainEntityOfPage":{"@id":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks"},"wordCount":1254,"publisher":{"@id":"https:\/\/www.infobip.com\/developers\/#organization"},"image":{"@id":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#primaryimage"},"thumbnailUrl":"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png","keywords":["programming"],"articleSection":["Blog Post"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks","url":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks","name":"We don't write 'else' blocks","isPartOf":{"@id":"https:\/\/www.infobip.com\/developers\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#primaryimage"},"image":{"@id":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#primaryimage"},"thumbnailUrl":"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png","datePublished":"2024-04-23T12:25:44+00:00","dateModified":"2024-04-23T13:42:07+00:00","description":"When I joined Infobip, I was introduced to the team's coding rules. One of them emphasized using 'else' blocks sparingly.","breadcrumb":{"@id":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#primaryimage","url":"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png","contentUrl":"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2024\/04\/Else-block.png","width":800,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.infobip.com\/developers\/blog\/we-dont-write-else-blocks#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.infobip.com\/developers\/"},{"@type":"ListItem","position":2,"name":"We don&#8217;t write &#8216;else&#8217; blocks"}]},{"@type":"WebSite","@id":"https:\/\/www.infobip.com\/developers\/#website","url":"https:\/\/www.infobip.com\/developers\/","name":"Infobip Developers Hub","description":"Build meaningful customer relationships across any channel","publisher":{"@id":"https:\/\/www.infobip.com\/developers\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.infobip.com\/developers\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.infobip.com\/developers\/#organization","name":"Infobip Developers Hub","url":"https:\/\/www.infobip.com\/developers\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.infobip.com\/developers\/#\/schema\/logo\/image\/","url":"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2023\/03\/Infobip_logo_favicon.png","contentUrl":"https:\/\/www.infobip.com\/developers\/wp-content\/uploads\/2023\/03\/Infobip_logo_favicon.png","width":696,"height":696,"caption":"Infobip Developers Hub"},"image":{"@id":"https:\/\/www.infobip.com\/developers\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/infobip\/","https:\/\/x.com\/InfobipDev","https:\/\/www.youtube.com\/channel\/UCUPSTy53VecI5GIir3J3ZbQ","https:\/\/github.com\/infobip-community"]},{"@type":"Person","@id":"https:\/\/www.infobip.com\/developers\/#\/schema\/person\/31b120f4d24c734e6fd27681b0a0510d","name":"Alen Kosanovic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.infobip.com\/developers\/#\/schema\/person\/image\/20b7763781ec59811d4a07b57b88def4","url":"https:\/\/secure.gravatar.com\/avatar\/1f800e46e256ff03fcb75817f407fa284411cbdaac405ad6a2dc234bf7537c76?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1f800e46e256ff03fcb75817f407fa284411cbdaac405ad6a2dc234bf7537c76?s=96&d=mm&r=g","caption":"Alen Kosanovic"},"description":"Alen is a senior software developer at Infobip, with almost a decade of experience in pursuing clean code. When he's not at the keyboard, he wanders around the world with his family.","sameAs":["https:\/\/www.linkedin.com\/in\/kosani\/"],"url":"https:\/\/www.infobip.com\/developers\/blog\/author\/alen-kosanovic"}]}},"_links":{"self":[{"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/posts\/3197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/comments?post=3197"}],"version-history":[{"count":16,"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/posts\/3197\/revisions"}],"predecessor-version":[{"id":3224,"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/posts\/3197\/revisions\/3224"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/media\/3219"}],"wp:attachment":[{"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/media?parent=3197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/categories?post=3197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/tags?post=3197"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.infobip.com\/developers\/wp-json\/wp\/v2\/coauthors?post=3197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}