{"id":10980,"date":"2022-11-28T13:08:24","date_gmt":"2022-11-28T18:08:24","guid":{"rendered":"https:\/\/irreal.org\/blog\/?p=10980"},"modified":"2022-11-28T13:08:24","modified_gmt":"2022-11-28T18:08:24","slug":"use-closed-open-intervals-for-programming","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=10980","title":{"rendered":"Use Closed\/Open Intervals For Programming"},"content":{"rendered":"<p>In Mathematics, especially Real Analysis and Topology, the use of open and closed intervals is endemic. The open interval \\((a,b)\\) is the interval \\(a &lt; x &lt; b\\) while the closed interval \\([a,b]\\) includes the endpoints: \\(a \\le x \\le b\\). There is also the notion of a closed\/open interval \\([a,b)\\): \\(a \\le x &lt; b\\).<\/p>\n<p>You\u2019ve probably noticed that operators in most programming languages operate on closed\/open intervals. A common example is the sub string operator, <code>substr<\/code> say, found in many languages. They all work pretty much the same. The function <code>substr(a,b)<\/code> specifies the string of bytes starting at byte \\(a\\) up to but not including byte \\(b\\). If you\u2019re like me, you\u2019ve probably occasionally found this annoying. If you want bytes 2, 3, and 4, it seems natural to write <code>substr(2,4)<\/code> rather than <code>substr(2,5)<\/code>.<\/p>\n<p>Fernando Hurtado Cardenas has a post that <a href=\"https:\/\/fhur.me\/posts\/always-use-closed-open-intervals\">explains this seeming anomaly<\/a>. Although using a closed interval seems more logical and has a nice symmetry, it often leads to unexpected edge cases and brittle code with a lot of special cases. The worst thing is that the closed interval works most of the time. It\u2019s those easy to overlook corner cases that trip you up. Cardenas discusses some of those edge cases and offers a pretty good argument that the annoying closed\/open interval is the right thing. Take a look at his post. If nothing else, you\u2019ll feel better about those operators using closed\/open intervals.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Mathematics, especially Real Analysis and Topology, the use of open and closed intervals is endemic. The open interval \\((a,b)\\) is the interval \\(a &lt; x &lt; b\\) while the closed interval \\([a,b]\\) includes the endpoints: \\(a \\le x \\le &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=10980\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-10980","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/10980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10980"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/10980\/revisions"}],"predecessor-version":[{"id":10981,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/10980\/revisions\/10981"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}