{"id":92,"date":"2020-01-12T23:54:31","date_gmt":"2020-01-12T23:54:31","guid":{"rendered":"https:\/\/reghif.co.uk\/teach\/?p=92"},"modified":"2020-01-12T23:54:31","modified_gmt":"2020-01-12T23:54:31","slug":"break-statements-and-why-ill-dock-marks-for-them","status":"publish","type":"post","link":"https:\/\/reghif.co.uk\/teach\/2020\/01\/12\/break-statements-and-why-ill-dock-marks-for-them\/","title":{"rendered":"Break statements and why I&#8217;ll dock marks for them"},"content":{"rendered":"\n<p>Right, break statements and why they&#8217;re bad.<\/p>\n\n\n\n<p>Generally it&#8217;s indicating you&#8217;ve got a problem with the flow of your program and you may need to restructure.<\/p>\n\n\n\n<p>Imagine the following that hunts for a number, it&#8217;s an odd program, but it&#8217;s an illustration.<\/p>\n\n\n\n<p><code># x = some number you're hunting for<br>x = 56<br>for i in range(100):<br>    if i == x:<\/code><br><code>       print(\"Found x!\")<\/code><br><code>       break<\/code><br><code>    else:<\/code><br><code>       print(i, \"isn't x\")<\/code><\/p>\n\n\n\n<p>This program will loop up until 56. If you&#8217;re trying to debug a \nprogram 100x more complex then this and it has a similar format over 100\n lines, you&#8217;ve got a loop which says it&#8217;s counting from 0 to 100, yet \nit&#8217;s only going to 56. To figure out why you&#8217;ve got to hunt through and \nfind a break statement and figure out what it applies to &#8211; which bit \nit&#8217;s &#8216;break&#8217;ing.<\/p>\n\n\n\n<p>Consider the alternative:<\/p>\n\n\n\n<p><code>x = 56<br>i = -1<br>while i != x:<br>    i = i + 1<br>    if i == x:<br>        print(\"Found x!\")<br>    else:<\/code><br><code>       print(i, \"isn't x\")<\/code><\/p>\n\n\n\n<p>These programs are identical, the second has a bit more faff with \ndeclaring and incrementing i, but it&#8217;s a conditional loop. The first one\n is an unconditional loop you&#8217;re trying to hack into a conditional loop.<\/p>\n\n\n\n<p>If you&#8217;re in a sodding hotel in Africa and hunting through lines of \ncode at 3 in the morning trying to find an issue so you can fix the \nproblem and actually go home but the developers have gone home because \n&#8220;it&#8217;s gone 4:30pm and they wanted to miss the traffic&#8221; then you will \nappreciate the second being easier to understand and debug than the \nfirst.<\/p>\n\n\n\n<p>Look at the condition on the first, it&#8217;s looping to 100&#8230;.but the result is that it doesn&#8217;t.<\/p>\n\n\n\n<p>Look at the condition on the first, it&#8217;s looping to x&#8230;..and that&#8217;s what&#8217;s happening. Clarity.<\/p>\n\n\n\n<p>Hopefully that&#8217;s fairly clear. Generally I appreciate people finding  solutions, but break is one command that I think shouldn&#8217;t exist as it&#8217;s  just a plaster for bad structure.<\/p>\n\n\n\n<p>People will have different view points, people will say that I&#8217;m wrong, and I respect that, provided there&#8217;s justification. I can accept there are some circumstances where a break statement is good, but they&#8217;re specifics, I don&#8217;t believe it should be used as a general tool to be wielded with frequency.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Right, break statements and why they&#8217;re bad. Generally it&#8217;s indicating you&#8217;ve got a problem with the flow of your program and you may need to restructure. Imagine the following that hunts for a number, it&#8217;s an odd program, but it&#8217;s an illustration. # x = some number you&#8217;re hunting forx = 56for i in range(100): &#8230;.&nbsp;&nbsp;<a class=\" special\" href=\"https:\/\/reghif.co.uk\/teach\/2020\/01\/12\/break-statements-and-why-ill-dock-marks-for-them\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[8,7,10],"class_list":["post-92","post","type-post","status-publish","format-standard","hentry","category-a-level","tag-a-level","tag-essays","tag-programming"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":1,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"predecessor-version":[{"id":93,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts\/92\/revisions\/93"}],"wp:attachment":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}