
{"id":2334,"date":"2017-02-23T14:24:53","date_gmt":"2017-02-23T19:24:53","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=2334"},"modified":"2017-02-23T14:24:53","modified_gmt":"2017-02-23T19:24:53","slug":"dont-give-me-apples-that-are-not-red","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=2334","title":{"rendered":"Don&#8217;t give me apples that are not red!"},"content":{"rendered":"<p>I have just stumbled on a beautiful example of a double negative in our code. We have a function that returns a list of object, and it takes a &#8220;filterFunction&#8221; as a parameter:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nList&lt;Apple&gt; GetApples(Function&lt;Apple,bool&gt; filterFunction)\r\n<\/pre>\n<p>The trouble is, <strong>filterFunction<\/strong>\u00a0works as <em>exclusion\u00a0<\/em>function:\u00a0is is supposed to return\u00a0<strong>true<\/strong> for objects to be thrown out, and\u00a0<strong>false<\/strong> for objects to be kept in the list. So, effectively, instead of saying &#8220;give me only red apples&#8221; you have to say &#8220;don&#8217;t give me apples that are not red&#8221;. Logically it&#8217;s the same thing, but human brain is not\u00a0designed to easily handle double negatives.<\/p>\n<p>For this reason, I always ask people not to use variable names like &#8220;IsNotValidUser&#8221; where &#8220;false&#8221; designates a valid user, and &#8220;true&#8221; means an invalid user. A flag named &#8220;IsValidUser&#8221; with reversed values is much easier to understand. However, I&#8217;ve never seen the &#8220;is not&#8221; logic applied to filter functions before. Maybe it has something to do with popularity of functional programming&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have just stumbled on a beautiful example of a double negative in our code. We have a function that returns a list of object, and it takes a &#8220;filterFunction&#8221; <a href=\"https:\/\/ikriv.com\/blog\/?p=2334\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[1],"tags":[],"class_list":["entry","author-ikriv","post-2334","post","type-post","status-publish","format-standard","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2334"}],"version-history":[{"count":1,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2334\/revisions"}],"predecessor-version":[{"id":2335,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2334\/revisions\/2335"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}