{"id":27,"date":"2009-02-27T04:40:40","date_gmt":"2009-02-27T11:40:40","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=24"},"modified":"2009-02-27T04:40:40","modified_gmt":"2009-02-27T11:40:40","slug":"how-not-to-name-a-boolean-variable","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2009\/02\/27\/how-not-to-name-a-boolean-variable\/","title":{"rendered":"How not to name a boolean variable"},"content":{"rendered":"<p>Out of personal experience and reading other people&#8217;s code,\u00a0 I have come to realize a small but very tricky issue about how NOT to name boolean variables or any kind of variable where the code to evaluate is usually in the form of a boolean expression.<\/p>\n<p>The pitfall to avoid is :<\/p>\n<p style=\"text-align: center;\"><strong>Do not use a name with a negative meaning or connotation<\/strong><\/p>\n<p>Instead of naming it <em>notpassed <\/em>call it <em>failed . <\/em>Instead of calling it <em>no_data_present <\/em>call it <em>data_absent<\/em><\/p>\n<p>The reason is simple. While reading the code, a negative sounding boolean variable causes more confusion than required.<\/p>\n<p>Example:<\/p>\n<p><em>bool not_done = false;<\/em><\/p>\n<p>Various ways of checking it :<\/p>\n<p><em>if (not_done) { }<\/em><\/p>\n<p><em>if {!not_done) {}<\/em><\/p>\n<p><em>if (not not_done) then \/\/VB style<\/em><\/p>\n<p><em>while (not_done\u00a0 == false)<\/em><\/p>\n<p>Try reading the above lines to yourself and you will see sometimes it gets confusing to actually understand what the condition is checking for really.<\/p>\n<p>By common programming convention when we use a conditional operator , we are generally trying to see something positive has happened, so if the variable itself sounds like it stands for something positive it makes for easier understanding of the code.<\/p>\n<p>Lets try the above example with a positive sounding variable:<\/p>\n<p><em>bool done = false;<\/em><\/p>\n<p>Various ways of checking it :<\/p>\n<p><em>if (done) { }<\/em><\/p>\n<p><em>if {!done) {}<\/em><\/p>\n<p><em>if (not done) then \/\/VB style<\/em><\/p>\n<p><em>while (done\u00a0 == false)<\/em><\/p>\n<p>I think the above statements are easier to understand now.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Out of personal experience and reading other people&#8217;s code,\u00a0 I have come to realize a small but very tricky issue about how NOT to name <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2009\/02\/27\/how-not-to-name-a-boolean-variable\/\" title=\"How not to name a boolean variable\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[55,257,261],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-programming","tag-boolean-amit","tag-truelogic","tag-variable"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":0,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}