{"id":26,"date":"2009-08-08T18:29:34","date_gmt":"2009-08-08T17:29:34","guid":{"rendered":"https:\/\/rwec.co.uk\/blog\/?p=26"},"modified":"2009-08-08T18:29:34","modified_gmt":"2009-08-08T17:29:34","slug":"the-case-for-a-better-switch","status":"publish","type":"post","link":"https:\/\/rwec.co.uk\/blog\/2009\/08\/the-case-for-a-better-switch\/","title":{"rendered":"The Case for a better Switch"},"content":{"rendered":"<p>The C-style <code>switch-case<\/code> construct &#8211; used in various programming languages, including PHP &#8211; is a curious beast. Most commonly used as a streamlined form of <code>if-elseif-else<\/code>, it actually has more in common with the oldest of control statements, the <code>goto<\/code>, since control jumps to the first match <em>and carries on until you tell it to stop<\/em>.<\/p>\n<p>Firstly, am I the only person in the world who thinks that the <code>break<\/code> should be indented to the same level as the <code>case<\/code>, not the level of the commands between? If you <strong>are<\/strong> using it as a series of separate <code>elseif<\/code> blocks, then surely the two form a matching pair, with the code block &#8220;enclosed&#8221; within them. Of course, you can generally <code>break<\/code> early inside the code, just like you can <code>return<\/code> early from a function, which is hard to make stand out &#8211; but that&#8217;s just an argument not to do it too often!<\/p>\n<p>And, of course, the fact that another <code>case<\/code> comes along <strong>does not<\/strong> mean that the code won&#8217;t carry on. Which brings me neatly to my next thought: is there any language that insists you declare when you&#8217;re falling through to the next case &#8211; and if not, why not? What if every time you put a <code>case<\/code>, you had to end it with either a <code>break<\/code> or a <code>continue<\/code>? It seems to me it would prevent a lot of bugs caused by cases <em>inadvertently<\/em> falling through and running completely the wrong code. And if overloading an existing term like <code>continue<\/code> is too confusing, lets just have a <code>fallthrough<\/code> &#8211; anyone got a case against? [No pun intended; probably&#8230;]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C-style switch-case construct &#8211; used in various programming languages, including PHP &#8211; is a curious beast. Most commonly used as a streamlined form of if-elseif-else, it actually has more in common with the oldest of control statements, the goto, since control jumps to the first match and carries on until you tell it to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[17,16,19,18],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-coding","tag-php","tag-programming-languages","tag-switch","post-preview"],"_links":{"self":[{"href":"https:\/\/rwec.co.uk\/blog\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rwec.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rwec.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rwec.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rwec.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":5,"href":"https:\/\/rwec.co.uk\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":31,"href":"https:\/\/rwec.co.uk\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions\/31"}],"wp:attachment":[{"href":"https:\/\/rwec.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rwec.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rwec.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}