{"id":12,"date":"2000-12-09T16:43:01","date_gmt":"2000-12-09T06:43:01","guid":{"rendered":"http:\/\/www.thunderguy.com\/plusplus\/20050421\/clarifying-c-negation\/"},"modified":"2005-04-21T17:22:54","modified_gmt":"2005-04-21T07:22:54","slug":"clarifying-c-negation","status":"publish","type":"post","link":"https:\/\/thunderguy.com\/semicolon\/2000\/12\/09\/clarifying-c-negation\/","title":{"rendered":"Clarifying C++ negation"},"content":{"rendered":"<p class=\"abstract\">The C++ negation operator (<code>!<\/code>) can be hard to see in code listings, and this leads to bugs. Here are a few solutions to this perennial coding problem.<\/p>\n<p>The C negation operator (<code>!<\/code>) is used by most C++ programmers also. The trouble with it is that it can be hard to see. For example, code like this<\/p>\n<pre class=\"code\">\r\n<code class=\"bad-code\">if (!limited) {...}<\/code>\r\n<\/pre>\n<p>can be confusing, as it&#8217;s easy to miss the exclamation mark when skimming the code. Some people get around this by using<\/p>\n<pre class=\"code\">\r\n<code class=\"bad-code\">if (limited==false) {...}<\/code>\r\n<\/pre>\n<p>but many other people think this is ugly, unnatural and verbose. (I am one of these people.) Such people often desert C++ for a <code>!<\/code>-less language like Python. But if you insist on C++, not all is lost. Standard C++ offers a neat solution:<\/p>\n<pre class=\"code\">\r\n<code>if (not limited) {...}<\/code>\r\n<\/pre>\n<p>Much prettier than <code>limited==false<\/code> and much more visible than <code>!limited<\/code>. And so clear, even a non-programmer could understand it. If your compiler doesn&#8217;t support these alternative keywords (<code>not<\/code> instead of <code>!<\/code>, <code>and<\/code> instead of <code>&amp;&amp;<\/code>, etc.), you could always use <code>#define<\/code>. (Of course, the <code>#define<\/code> trick works in C as well.)<\/p>\n<p><strong>Postscript:<\/strong> Another solution to the visibility problem just occurred to me, late at night. This is particularly good since it works in C, Java, and probably any other language that uses <code>!<\/code>.<\/p>\n<pre class=\"code\">\r\n<code>if (!!!limited) {...}<\/code>\r\n<\/pre>\n<p>Evaluating the utility of this approach is left as an exercise to the reader.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C++ negation operator (!) can be hard to see in code listings, and this leads to bugs. Here are a few solutions to this perennial coding problem. The C negation operator (!) is used by most C++ programmers also. The trouble with it is that it can be hard to see. For example, code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[3],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","tag-cpp"],"_links":{"self":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":0,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}