{"id":2353,"date":"2015-08-14T17:27:55","date_gmt":"2015-08-14T05:27:55","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=2353"},"modified":"2015-08-14T17:30:37","modified_gmt":"2015-08-14T05:30:37","slug":"formatting-case-statements","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/2353\/","title":{"rendered":"Formatting Case Statements"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span><p>I&#8217;m afraid I have been guilty of editorialising on StackOverflow.  It was sheer laziness really.  A question prompted me to respond by sharing a personal preference and instead of &#8220;context switching&#8221; to my blog I posted an answer that even at the time I acknowledged was <em>not<\/em> in fact an answer (though as transgression go I have to say I have seen far worse, but, that&#8217;s no excuse).<\/p>\n<p>So, with (further) apologies to StackOverflow, I have withdrawn that answer and instead present that editorial here, a more appropriate forum.<\/p>\n<p><!--more--><\/p>\n<p>The question involved <a href=\"http:\/\/stackoverflow.com\/questions\/31999397\/how-to-make-rad-studio-format-case-statements-appropriately\">how to cajole, coerce or otherwise convince the source formatting tools in RAD Studio to format a <strong>case<\/strong> statement in a manner pleasing to the OP<\/a>.  For illustration in the question they posted an example of how RAD Studio was currently formatting the statement (I hope they don&#8217;t mind me reproducing their examples here):<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n    case SomeEnum of\r\n      seOne: begin\r\n\r\n        end;\r\n      seTwo: begin\r\n\r\n        end;\r\n      seThree:\r\n        ;\r\n      seFour:\r\n        DoSomething;\r\n    end;\r\n<\/pre>\n<p>And helpfully then illustrated how they would prefer to have it formatted:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n    case SomeEnum of\r\n      seOne: begin\r\n\r\n      end;\r\n      seTwo: begin\r\n\r\n      end;\r\n      seThree: ;\r\n      seFour: DoSomething;\r\n    end;\r\n<\/pre>\n<p>It just happens that <strong>case<\/strong> statements were among the last aspects of my own personal Delphi coding style to really settle down over the past <em>mumble<\/em> years.  But in more recent years I felt I finally cracked it, having finally settled on a consistent formatting style (keyword: &#8220;<em>style<\/em>&#8220;, <strong>not<\/strong> &#8220;<em>solution<\/em>&#8220;) that once alighted upon has stood the test of time in a way that my previous efforts did not.<\/p>\n<p>I may even have first come across this style &#8211; or something significantly similar &#8211; in someone else&#8217;s code, but if so I have long since forgotten.  If you recognise this style and feel it has been appropriated without proper attribution, apologies.  It is not intentional.<\/p>\n<p>And so with too much ado already, here it is&#8230;<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n    case SomeEnum of\r\n\r\n      seOne   : begin\r\n\r\n                end;\r\n\r\n      seTwo   : begin\r\n \r\n                end;\r\n\r\n      seThree : { NO-OP };\r\n\r\n      seFour  : DoSomething;\r\n\r\n      seFive,\r\n      seSix   : DoSomethingElse;\r\n\r\n    else\r\n      \/\/ TODO: How to handle the unhandled.. ?\r\n    end;\r\n<\/pre>\n<p>As you can see, I took the original code and added slightly to it, allowing me to illustrate how I deal with multiple cases having common behaviour (<strong>seFive<\/strong> and <strong>seSix<\/strong>) and (for this revised and extended blog post) an <strong>else<\/strong> statement.<\/p>\n<p>Among the reasons this formatting finds favour with me are:<\/p>\n<ol>\n<li>The conditional statements have appropriately equal indentation with respect to each other, the single exception to this being any &#8220;else&#8221; case, where present.<\/li>\n<li>High visibility of the case values, often key to understanding such code when you come to read it months\/years later.<\/li>\n<li>It&#8217;s clean and clear &#8211; highly readable (to me).<\/li>\n<\/ol>\n<p>In addition to the case formatting I also demonstrate my practice of providing a specific comment to indicate a deliberate and explicit <strong>NO-OP<\/strong> as distinct from an equally explicit <strong>TODO<\/strong> (in the <strong>else<\/strong>).  You could choose to be explicit about either one and interpret the absence of such any explicit indication as implying the other, but this leaves open the possibility that what is then interpreted as &#8220;implied&#8221; is in reality an <em>oversight<\/em> (is it a <em>no-op<\/em> or an unannotated <em>to-do<\/em> ?  or vice-versa contrariwise).<\/p>\n<p>In ay event, no amount of tweaking will convince RAD Studio to determine the difference, let alone add the appropriate comments for you (though some static analysis tools may draw attention to &#8220;null&#8221; statements and some may even be able to be configured to identify explicitly deliberately null statements, where commented as such).<\/p>\n<p>But as for the case statement in general, I&#8217;m not presenting this as <em>the<\/em> or even <em>a<\/em> ideal or perfect approach, merely sharing my personal, individual preference that has evolved over the years.<\/p>\n<p>At the end of the day, whatever works for you works for you. \ud83d\ude42<\/p>\n<h2>Footnote<\/h2>\n<p>Sertac Akyuz provided Jerry with the answer he was looking for.  \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span> I&#8217;m afraid I have been guilty of editorialising on StackOverflow. It was sheer laziness really. A question prompted me to respond by sharing a personal preference and instead of &#8220;context switching&#8221; to my blog I posted an answer that even at the time I acknowledged was not in fact an answer (though as transgression go [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[4],"tags":[125,291,13,173,290],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-BX","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":653,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/653\/","url_meta":{"origin":2353,"position":0},"title":"RAD Studio &#8220;XE&#8221;","date":"11 Aug 2010","format":false,"excerpt":"News is starting to come out about the upcoming release of Delphi, RAD Studio XE. So what can we expect? Well, based on what has been revealed so far, anyone thinking that the time that has passed since we were supposed to have received 64-bit support has been well spent\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":507,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/507\/","url_meta":{"origin":2353,"position":1},"title":"Mind the Gap(s)","date":"14 Sep 2009","format":false,"excerpt":"This is what I love about Delphi. \u00a0After almost 15 years of Delphi'ing there's still new things to learn, and I don't just mean new features in the latest releases. \u00a0I mean, basic, fundamental things that have been there for years, just undiscovered (by me). \u00a0In this case the discovery\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1945,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1945\/","url_meta":{"origin":2353,"position":2},"title":"Unit Aliases and Build Configurations","date":"17 Oct 2013","format":false,"excerpt":"Over on stackoverflow, Ann Gossard had a question about using an $ifdef in the project (DPR) uses list to use one unit in preference over another, specifically in debug builds. In this situation, it immediately occurred to me that unit aliases might be creatively deployed. What follows is a slightly\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-17-at-15.25.51.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2511,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2511\/","url_meta":{"origin":2353,"position":3},"title":"Anonymous Classes: Implementing Interfaces","date":"11 Feb 2017","format":false,"excerpt":"A few years ago (2011 to be precise) someone asked a question on StackOverflow about support for anonymous classes in Delphi. The reason for the question was that the poster was trying to use Delphi to develop for Android and on that platform the widespread use of callback interfaces makes\u2026","rel":"","context":"In &quot;Android&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1233,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1233\/","url_meta":{"origin":2353,"position":4},"title":"ReverseBytes()","date":"20 Sep 2012","format":false,"excerpt":"In some comments on StackOverflow, Jeroen asked me to post my code for reversing bytes. Rather than posting code into that question\/answer that wasn't directly relevant to the question\/answer, I decided to quickly throw the code up on here. The intent with ReverseBytes() is - as the name says -\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":582,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/582\/","url_meta":{"origin":2353,"position":5},"title":"On the Qt and all very Hush, Hush&#8230;","date":"14 Oct 2009","format":false,"excerpt":"A commenter on my blog suggested that Cross Platform could be a big win for Delphi, making it \u201cthe first\u201d to achieve this. \u00a0This I think says a lot about the awareness and expectation of (some) of the people asking for cross platform, because far from being first Delphi would\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2353"}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=2353"}],"version-history":[{"count":7,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2353\/revisions"}],"predecessor-version":[{"id":2360,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2353\/revisions\/2360"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=2353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=2353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=2353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}