{"id":2383,"date":"2021-04-20T22:55:58","date_gmt":"2021-04-21T05:55:58","guid":{"rendered":"http:\/\/zackmdavis.net\/blog\/?p=2383"},"modified":"2021-04-20T22:55:58","modified_gmt":"2021-04-21T05:55:58","slug":"beauty-is-truthiness-truthiness-beauty","status":"publish","type":"post","link":"http:\/\/zackmdavis.net\/blog\/2021\/04\/beauty-is-truthiness-truthiness-beauty\/","title":{"rendered":"Beauty Is Truthiness, Truthiness Beauty?"},"content":{"rendered":"<p>Imagine reviewing Python code that looks something like this.<\/p>\n<pre><code>has_items = items is not None and len(items) &gt; 0\nif has_items:\n    ...\n\n...\ndo_stuff(has_items=has_items)<\/code><\/pre>\n<p>You might look at the conditional, and disapprove: <code>None<\/code> and empty collections are both falsey, so there's no reason to define that <code>has_items<\/code> variable; you could just say <code>if items:<\/code>.<\/p>\n<p>But, wouldn't it be weird for <code>do_stuff<\/code>'s <code>has_items<\/code> kwarg to take a collection rather than a boolean? I think it would be weird: even if the function's internals can <em>probably<\/em> rely on mere truthiness rather than needing an actual boolean type for some reason, why leave it to chance?<\/p>\n<p>So, <em>maybe<\/em> it's okay to define the <code>has_items<\/code> variable for the sake of the function kwarg\u2014and, having done so anyway, to use it as an <code>if<\/code> condition.<\/p>\n<p>You might object further: but, but, <code>None<\/code> and the empty collection are <em>still both falsey<\/em>. Even if we've somehow been conned into defining a whole variable, shouldn't we say <code>has_items = bool(items)<\/code> rather than spelling out <code>is not None and len(items) &gt; 0<\/code> like some <em>rube<\/em> <a href=\"https:\/\/stackoverflow.com\/a\/23068894\">(or Rubyist)<\/a> who doesn't know Python?!<\/p>\n<p>Actually\u2014maybe not. Much of Python's seductive charm comes from its friendly readability (\"executable pseudocode\"): it's <em>intuitive<\/em> for <code>if not items<\/code> to mean \"if <code>items<\/code> is empty\". English, and not the formal truthiness rules, are all ye need to know. In contrast, it's only if you <em>already<\/em> know the rules that <code>bool(items)<\/code> becomes meaningful. Since we care about good code and don't care about testing the reader's Python knowledge, spelling out <code>items is not None and len(items) &gt; 0<\/code> is very arguably the <em>right thing to do<\/em> here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine reviewing Python code that looks something like this. has_items = items is not None and len(items) &gt; 0 if has_items: ... ... do_stuff(has_items=has_items) You might look at the conditional, and disapprove: None and empty collections are both falsey, so &hellip; <a href=\"http:\/\/zackmdavis.net\/blog\/2021\/04\/beauty-is-truthiness-truthiness-beauty\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[21],"_links":{"self":[{"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/posts\/2383"}],"collection":[{"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/comments?post=2383"}],"version-history":[{"count":1,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/posts\/2383\/revisions"}],"predecessor-version":[{"id":2384,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/posts\/2383\/revisions\/2384"}],"wp:attachment":[{"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/media?parent=2383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/categories?post=2383"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/tags?post=2383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}