{"id":1190,"date":"2024-12-08T13:00:10","date_gmt":"2024-12-08T13:00:10","guid":{"rendered":"https:\/\/blogs.kcl.ac.uk\/proged\/?p=1190"},"modified":"2024-12-08T13:00:10","modified_gmt":"2024-12-08T13:00:10","slug":"designing-a-programming-language-dont-be-clever","status":"publish","type":"post","link":"https:\/\/blogs.kcl.ac.uk\/proged\/2024\/12\/08\/designing-a-programming-language-dont-be-clever\/","title":{"rendered":"Designing a programming language: Don&#8217;t Be Clever"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1191 size-full\" src=\"http:\/\/blogs.kcl.ac.uk\/proged\/files\/2024\/12\/JS.png\" alt=\"\" width=\"892\" height=\"552\" srcset=\"https:\/\/blogs.kcl.ac.uk\/proged\/files\/2024\/12\/JS.png 892w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2024\/12\/JS-200x124.png 200w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2024\/12\/JS-100x62.png 100w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2024\/12\/JS-768x475.png 768w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2024\/12\/JS-485x300.png 485w\" sizes=\"auto, (max-width: 892px) 100vw, 892px\" \/><\/p>\n<p>One lesson of programming language design? Don&#8217;t try to be too clever!<\/p>\n<p>Here is an example from JavaScript\u200b:<\/p>\n<p>When you compare<\/p>\n<p style=\"padding-left: 40px\"><strong>0 == &#8216;0&#8217;<\/strong><\/p>\n<p>the system tries to be clever: &#8220;Comparing a number and a string? You probably meant to see whether the string contains the character &#8216;0&#8217;, so let&#8217;s just say &#8216;yes&#8217;!&#8221;, it thinks. So the result is true.<\/p>\n<p>When you compare<\/p>\n<p style=\"padding-left: 40px\"><strong>0 == &#8221;<\/strong><\/p>\n<p>it tries to be even cleverer: &#8220;Ahh, perhaps you meant to check whether the length of the string is zero! Let&#8217;s just help the user here and not require them to call a &#8216;length&#8217; function. So tedious. It&#8217;s true!&#8221;<\/p>\n<p>But of course<\/p>\n<p style=\"padding-left: 40px\"><strong>&#8216;0&#8217; == &#8221;<\/strong><\/p>\n<p>&#8220;Comparing two strings &#8211; got it. Are they the same? No! Returning false.&#8221;<\/p>\n<p>The result of all this: The fundamental property of transitivity of equality is broken in JavaScript!<\/p>\n<p style=\"padding-left: 40px\"><strong>a == b and b == c, therefore a == c ?<\/strong><\/p>\n<p>Not in JavaScript, it isn&#8217;t! How can anyone voluntarily program in a language like this?!<\/p>\n<p>The lessons for me? Give me static typing any day. And don&#8217;t try to be clever when designing a language. Spelling things out is a good thing, not a bad thing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One lesson of programming language design? Don&#8217;t try to be too clever! Here is an example from JavaScript\u200b: When you compare 0 == &#8216;0&#8217; the system tries to be clever: &#8220;Comparing a number and a string? You probably meant to &hellip; <a href=\"https:\/\/blogs.kcl.ac.uk\/proged\/2024\/12\/08\/designing-a-programming-language-dont-be-clever\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":179,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[141],"tags":[],"class_list":["post-1190","post","type-post","status-publish","format-standard","hentry","category-programming-language-design"],"_links":{"self":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/1190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/users\/179"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/comments?post=1190"}],"version-history":[{"count":1,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/1190\/revisions"}],"predecessor-version":[{"id":1192,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/1190\/revisions\/1192"}],"wp:attachment":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/media?parent=1190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/categories?post=1190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/tags?post=1190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}