{"id":330,"date":"2003-02-15T13:36:48","date_gmt":"2003-02-15T17:36:48","guid":{"rendered":"\/?p=330"},"modified":"2007-03-17T16:50:25","modified_gmt":"2007-03-17T20:50:25","slug":"assignment-and-comparison","status":"publish","type":"post","link":"https:\/\/www.godofthemachine.com\/?p=330","title":{"rendered":"Assignment and Comparison"},"content":{"rendered":"<p><a href=\"http:\/\/volokh.blogspot.com\/2003_02_09_volokh_archive.html#90326291\">Eugene Volokh<\/a> complains that his C programs used to crash, and his JavaScript still does, because the languages distinguish comparison (<code>==<\/code>) from assignment (<code>=<\/code>). (Volokh, at 14 <a href=\"http:\/\/www1.law.ucla.edu\/~volokh\/\">the world&#8217;s youngest law professor<\/a>, also worked as a programmer for several years as a preadolescent.) On the one hand it&#8217;s a lousy idea to use the single equal sign for assignment. The best-known operator should be reserved for the best-known operation, which is comparison. Many other languages keep the equal sign for comparison and use other symbols (<code>:=, .=, =:<\/code>) for assignment instead. Java inherits much of its syntax from C, including the equal and double equal signs, with the annoying consequence that if we assign a boolean variable, say <code>x<\/code>, the value <code>false<\/code>, then in this code snippet:<\/p>\n<p><code>if (x == true)<br \/>\n  doSomething();<\/code><\/p>\n<p><code>doSomething()<\/code> will not execute, whereas in this one:<\/p>\n<p><code>if (x = true)<br \/>\n  doSomething();<\/code><\/p>\n<p>it will. The second snippet, though shorter, contains two bits of business: we assign the value true to x, and we then evaluate x. In Java this is done right to left, so by the time we arrive at the if, x is true.<br \/>\nThis is a smaller problem in Java, however, because Java is strongly typed. If x is a String, or an Integer, or anything but a primitive boolean, the line <code>if  (x = true)<\/code> will not compile.<\/p>\n<p>But annoying and confusing as this is, it still beats languages, like Visual Basic, that use the same symbol for comparison and assignment. Consider this legal statement: <\/p>\n<p><code>a = b = c<\/code><\/p>\n<p>Even if we assume an execution order of right to left this is ambiguous. It might mean &#8220;assign the value of c to b, then assign the value of b to a.&#8221; Or it might mean &#8220;assign true to a if b and c are equal, otherwise assign false to a.&#8221; No way to tell.  <\/p>\n<p>The first language, to my knowledge, to distinguish comparison from assignment was APL (A Programming Language) in the 1950s, in which a left arrow (<\u00e2 \u20ac\u201d) indicates assignment. There is a famous, perhaps apocryphal, story of Ken Iverson, APL's inventor, watching some hapless Fortran programmer increment a loop counter by typing <code>a = a + 1. &#8220;But that&#8217;s false,&#8221; Iverson said.<\/\u00e2><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Eugene Volokh complains that his C programs used to crash, and his JavaScript still does, because the languages distinguish comparison (==) from assignment (=). (Volokh, at 14 the world&#8217;s youngest law professor, also worked as a programmer for several years as a preadolescent.) On the one hand it&#8217;s a lousy idea to use the single <a href='https:\/\/www.godofthemachine.com\/?p=330' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-330","post","type-post","status-publish","format-standard","hentry","category-code","category-5-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/www.godofthemachine.com\/index.php?rest_route=\/wp\/v2\/posts\/330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.godofthemachine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.godofthemachine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.godofthemachine.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.godofthemachine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=330"}],"version-history":[{"count":0,"href":"https:\/\/www.godofthemachine.com\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.godofthemachine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.godofthemachine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.godofthemachine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}