{"id":5435,"date":"2014-05-06T16:13:57","date_gmt":"2014-05-06T15:13:57","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5435"},"modified":"2014-05-07T09:30:06","modified_gmt":"2014-05-07T08:30:06","slug":"can-you-tell-a-computer-scientist-from-the-way-they-write-loops","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5435","title":{"rendered":"Can you tell a Computer Scientist from the way they write loops?"},"content":{"rendered":"<p>This is a guest article written by friend and colleague, Ian Cottam.<\/p>\n<p>This brief guest piece for Walking Randomly was inspired by reading about some of the Hackday outputs at the <a href=\"http:\/\/www.software.ac.uk\/collaborations-workshop-2014-cw14-software-your-reproducible-research\/hackday\">recent SSI collaborative workshop CW14 held in Oxford<\/a>. I wasn&#8217;t there, but I gather that some of the outputs from the day examined source code for various properties (perhaps a little tongue-in-cheek in some cases).<\/p>\n<p>So, my also slightly tongue-in-cheek question is <em><strong>&#8220;Given a piece of source code written in a language with &#8220;while loops&#8221;: how do you know if the author is a computer scientist by education\/training?&#8221;<\/strong><\/em><\/p>\n<p>I&#8217;ll use C as my language and note that &#8220;for loops&#8221; in C are basically syntactic sugar for while loops (allowing one to gather the initialisation, guard and increment parts neatly together). In other languages &#8220;for loops&#8221; are closer to Fortran&#8217;s original iterative &#8220;do loop&#8221;. Also, I will work with that subset of code fragments that obey traditional structured (one-entry, one-exit) programming constructs. If I didn&#8217;t, perhaps one could argue, as famously <a href=\"https:\/\/www.cs.utexas.edu\/users\/EWD\/ewd02xx\/EWD215.PDF\">Dijkstra originally did<\/a>, that the density of &#8220;goto&#8221; statements, even when spelt &#8220;break&#8221; or &#8220;continue&#8221;, etc., might be a deciding quality factor.<\/p>\n<p>(Purely as an aside, I note that Linux (and related free\/open source) contributors seem to use goto fairly freely as an exception case mechanism; and they might well have a justification. The density of gotos in Apple&#8217;s SSL code was illustrated recently by the so-called <a href=\"http:\/\/nakedsecurity.sophos.com\/2014\/02\/24\/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch\/\">&#8220;goto fail&#8221; bug<\/a>. See also Knuth&#8217;s <a href=\"http:\/\/cs.sjsu.edu\/~mak\/CS185C\/KnuthStructuredProgrammingGoTo.pdf\">famous article on this subject<\/a>.)<\/p>\n<p>In my own programming, I know from experience that if I use a goto, I find it so much more difficult to reason logically (and non-operationally) about my code that I avoid them. Whenever I have used a programming language without the goto statement, I have never missed it.<\/p>\n<p>Now, finally to the point at hand, suppose one is processing the elements of an array of single dimension and of length N. The C convention is that the index goes from 0 to N-1. Code fragment A below is written by a non computer scientist, whereas B is.<\/p>\n<pre>\/* Code fragment A *\/\r\nfor (i= 0; i &lt; N; ++i) {\r\n\r\n\/* do stuff with a[i] *\/\r\n\r\n}<\/pre>\n<pre>\/* Code fragment B *\/\r\nfor (i= 0; i != N; ++i) {\r\n\r\n\/* do stuff with a[i] *\/\r\n\r\n}<\/pre>\n<p>The only difference is the loop&#8217;s guard:\u00a0i&lt;N versus i!=N.<\/p>\n<p><strong>As a computer scientist by training I would always write B; which would you write?<\/strong><\/p>\n<p>I would &#8211; and will in a follow-up &#8211; argue that B is better even though I am not saying that code fragment A is incorrect. Also in the follow-up I will acknowledge the computer scientist who first pointed this out &#8211; at least to me &#8211; some 33 years ago.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a guest article written by friend and colleague, Ian Cottam. This brief guest piece for Walking Randomly was inspired by reading about some of the Hackday outputs at the recent SSI collaborative workshop CW14 held in Oxford. I wasn&#8217;t there, but I gather that some of the outputs from the day examined source [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[65,49,7],"tags":[],"class_list":["post-5435","post","type-post","status-publish","format-standard","hentry","category-cc","category-guest-posts","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1pF","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5435"}],"version-history":[{"count":7,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5435\/revisions"}],"predecessor-version":[{"id":5441,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5435\/revisions\/5441"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}