{"id":972,"date":"2010-06-28T09:37:30","date_gmt":"2010-06-28T16:37:30","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=972"},"modified":"2016-07-14T14:58:24","modified_gmt":"2016-07-14T18:58:24","slug":"do-we-need-if-blocks","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/","title":{"rendered":"Do We Need If Blocks?"},"content":{"rendered":"<p>I&#8217;ve been contemplating this for a while now.\u00a0 I thought I would finally write on the subject and see what other people thought.<\/p>\n<p>Do you really need blocks after <em>if<\/em> statements?<\/p>\n<p>It has always been a best practice, in my opinion, to put opening and closing brackets after an <em>if<\/em> statement.\u00a0 Even if it has only one statement following.<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:8c01e598-9ded-48da-82c7-d78cd410726b\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/12d296500d1acb7a09bd1e5c5d703171.js\"><\/script><\/p>\n<\/div>\n<p>The reasoning behind this best practice goes something like this: If someone later modifies this code and adds a line inside the <em>if<\/em>, having the block will prevent them from doing something like this:<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:47c5c653-7298-459f-a3f0-a684a66f0f6b\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/771295e28ceaebe3a3e29c36af3e96e5.js\"><\/script><\/p>\n<\/div>\n<p>Which, of course, will result in way too much love making, even on stormy days when the sky is mostly gray.<\/p>\n<h2>All those { } in if blocks add up to a lot of overhead<\/h2>\n<p>I\u2019m not sure it is worth it anymore.<\/p>\n<p>Most of the code I write now has one statement after an <em>if<\/em> block.<\/p>\n<p>If I have some code like:<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:777acc1a-ef7d-451e-aa1d-1da0fef3847f\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/746792f7d8f7117503342cea7ec9121e.js\"><\/script><\/p>\n<\/div>\n<p>It is pretty much always going to get refactored to:<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:2e4eb6ff-992f-46ef-b5f6-c9f657bd5e6e\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/12d296500d1acb7a09bd1e5c5d703171.js\"><\/script><\/p>\n<\/div>\n<p>So, what is the real value of the { } here?\u00a0 If someone modifies this code, they are most likely going to modify the method DanceALittleDance; they really shouldn\u2019t be adding anything to the <em>if<\/em> block.\u00a0 Even if they do, I think I\u2019m paying a pretty high curly bracket tax all over my code to prevent someone from doing something stupid later on.<\/p>\n<p>Is this really that bad?<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:cd7f40b3-ffd1-472b-aa1e-a97ae1e1c0f1\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/ec62dc0efea5c83b518423afb89385ad.js\"><\/script><\/p>\n<\/div>\n<h2>Perhaps there is a better best practice?<\/h2>\n<p>How about if we forgo all the extra curly braces and instead say:<\/p>\n<blockquote><p><strong>Whenever possible replace an if block with a single statement.<\/strong><\/p><\/blockquote>\n<p>Hmm, I think I like that better.\u00a0 We won\u2019t run the risk anymore of accidentally adding a statement after the if block if we are used to thinking of an if block itself as a code smell.<\/p>\n<p>Almost always you can describe what is being done in an if block with a better name and encapsulate it in a method.<\/p>\n<p>Of course there are exceptions here.\u00a0 In a method with a void return type when you early return in an if block you can\u2019t really replace that with a single statement.\u00a0 (Although sometimes you can avoid the early return by using an if else statement instead.)<\/p>\n<p>We can apply the same thinking to anything that has a block statement.<\/p>\n<p>Strangely though, try and catch require block statements to follow.\u00a0 You cannot have a single statement following a try.<\/p>\n<p>Anyway, at this point I am pretty well convinced that including the { } &#8220;just because&#8221; is a pretty archaic practice and is unnecessarily cluttering up our code.\u00a0 If I am wrong, tell me why I am wrong, but as far as I can tell, if we prefer to extract statement blocks into methods, we can avoid the curly bracket tax everywhere.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been contemplating this for a while now.\u00a0 I thought I would finally write on the subject and see what other people thought. Do you really need blocks after if statements? It has always been a best practice, in my opinion, to put opening and closing brackets after an if statement.\u00a0 Even if it has&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[2185,2426,1017,1934],"tags":[],"class_list":["post-972","post","type-post","status-publish","format-standard","hentry","category-best-practices","category-c","category-java","category-language"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Do We Need If Blocks?<\/title>\n<meta name=\"description\" content=\"An explanation of why brackets following the if statement should no longer be needed, or rarely needed, with modern day coding practices. (If blocks)\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Do We Need If Blocks?\" \/>\n<meta property=\"og:description\" content=\"An explanation of why brackets following the if statement should no longer be needed, or rarely needed, with modern day coding practices. (If blocks)\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2010-06-28T16:37:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-07-14T18:58:24+00:00\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-we-need-if-blocks\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-we-need-if-blocks\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Do We Need If Blocks?\",\"datePublished\":\"2010-06-28T16:37:30+00:00\",\"dateModified\":\"2016-07-14T18:58:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-we-need-if-blocks\\\/\"},\"wordCount\":469,\"commentCount\":26,\"articleSection\":[\"Best Practices\",\"C#\",\"Java\",\"Language\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/do-we-need-if-blocks\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-we-need-if-blocks\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-we-need-if-blocks\\\/\",\"name\":\"Do We Need If Blocks?\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"datePublished\":\"2010-06-28T16:37:30+00:00\",\"dateModified\":\"2016-07-14T18:58:24+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"description\":\"An explanation of why brackets following the if statement should no longer be needed, or rarely needed, with modern day coding practices. (If blocks)\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-we-need-if-blocks\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/do-we-need-if-blocks\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-we-need-if-blocks\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Do We Need If Blocks?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Do We Need If Blocks?","description":"An explanation of why brackets following the if statement should no longer be needed, or rarely needed, with modern day coding practices. (If blocks)","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:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/","og_locale":"en_US","og_type":"article","og_title":"Do We Need If Blocks?","og_description":"An explanation of why brackets following the if statement should no longer be needed, or rarely needed, with modern day coding practices. (If blocks)","og_url":"https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/","og_site_name":"Simple Programmer","article_published_time":"2010-06-28T16:37:30+00:00","article_modified_time":"2016-07-14T18:58:24+00:00","author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Do We Need If Blocks?","datePublished":"2010-06-28T16:37:30+00:00","dateModified":"2016-07-14T18:58:24+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/"},"wordCount":469,"commentCount":26,"articleSection":["Best Practices","C#","Java","Language"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/","url":"https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/","name":"Do We Need If Blocks?","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"datePublished":"2010-06-28T16:37:30+00:00","dateModified":"2016-07-14T18:58:24+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"description":"An explanation of why brackets following the if statement should no longer be needed, or rarely needed, with modern day coding practices. (If blocks)","breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/do-we-need-if-blocks\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Do We Need If Blocks?"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=972"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/972\/revisions"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}