{"id":113,"date":"2011-07-18T23:00:16","date_gmt":"2011-07-19T06:00:16","guid":{"rendered":"http:\/\/www.ryotakahashi.com\/blog\/?p=113"},"modified":"2011-07-18T23:00:16","modified_gmt":"2011-07-19T06:00:16","slug":"ternary-terrors","status":"publish","type":"post","link":"http:\/\/www.ryotakahashi.com\/blog\/archives\/113","title":{"rendered":"Ternary Terrors"},"content":{"rendered":"<p>I don&#8217;t often talk about programming (but when I do, I get <a href=\"http:\/\/www.ryotakahashi.com\/blog\/archives\/24\">attacked by random nerds online<\/a> [insert Dos Equis Man meme here]). Since my job as a software engineer does involve a fair bit of it, though, I have, over the years, developed some likes and dislikes. Oh, not about anything that smart engineers would consider important, of course. It&#8217;s mostly about&#8230; prettiness.<\/p>\n<p>One thing that bothers me is when people put &#8220;true&#8221; or &#8220;false&#8221; in a ternary expression. If you don&#8217;t know what that is, it&#8217;s one of those things that looks like<\/p>\n<pre>\r\np ? x : y\r\n<\/pre>\n<p>that just means &#8220;if p, then x; otherwise y.&#8221;<\/p>\n<p>OK, so it really bugs me when I see things like<\/p>\n<pre>\r\nx ? y : false\r\n<\/pre>\n<p>I tear my hair out. I have to read this and think to myself that they&#8217;re trying to say &#8220;if x, then y; otherwise false.&#8221; But then that&#8217;s equivalent to saying that the expression is true if x and y are both true, otherwise it&#8217;s false, right?<\/p>\n<p>It always seems to me that putting a &#8220;true&#8221; or &#8220;false&#8221; in a ternary expression creates extra cognitive burden that I just don&#8217;t want. A ternary expression is sort of like a conditional, but a disjunction or conjunction is just so much more natural to understand, in my opinion.<\/p>\n<p>So here are some easy conversions!<\/p>\n<pre>\r\nx ? y : false   =>   x && y\r\nx ? y : true    =>   !x || y\r\nx ? false : y   =>   !x && y\r\nx ? true : y    =>   x || y\r\n<\/pre>\n<p>Shorter and more legible. At least in my opinion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t often talk about programming (but when I do, I get attacked by random nerds online [insert Dos Equis Man meme here]). Since my job as a software engineer does involve a fair bit of it, though, I have, &hellip; <a href=\"http:\/\/www.ryotakahashi.com\/blog\/archives\/113\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[201,200],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-programming","tag-code-readability","tag-ternary-expressions"],"_links":{"self":[{"href":"http:\/\/www.ryotakahashi.com\/blog\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ryotakahashi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ryotakahashi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ryotakahashi.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ryotakahashi.com\/blog\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":0,"href":"http:\/\/www.ryotakahashi.com\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.ryotakahashi.com\/blog\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ryotakahashi.com\/blog\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ryotakahashi.com\/blog\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}