{"id":4362,"date":"2014-03-30T10:03:39","date_gmt":"2014-03-30T10:03:39","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/improve-code-readability-without-performance-loss-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:03:39","modified_gmt":"2014-03-30T10:03:39","slug":"improve-code-readability-without-performance-loss-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/improve-code-readability-without-performance-loss-collection-of-common-programming-errors\/","title":{"rendered":"Improve code readability without performance loss-Collection of common programming errors"},"content":{"rendered":"<p>We have several classes where a call to a member function may change state and sometimes not depending on a boolean with default value.<\/p>\n<pre><code> A a;\n a.set( \"foobar\" );\n assert( a.changed() == true );\n<\/code><\/pre>\n<p>versus<\/p>\n<pre><code> A a;\n a.set( \"foobar\", false );\n assert( a.changed() == false );\n<\/code><\/pre>\n<p>Please note that those member functions are virtual. I would favor to use an alias to better read code and make a private member function still allowing the old method and give only the wrappers to the public:<\/p>\n<pre><code> a.silentlySet( \"foobar\" ) {\n    a.set( \"foobar\", false );\n }\n<\/code><\/pre>\n<p>I guess this would be inlined then anyway. I know I could write enums and constants which would also improve readability: <code>a.set( \"foobar\", SILENTLY );<\/code> but I don&#8217;t feel good with it, as the problem is of boolean nature (do or not). On the other side. However I would like to hear how you handle such a situation. (I assume I will get an &#8220;it depends&#8221; answer as I&#8217;ve certainly missed some crucial details) Live with it?<\/p>\n<p>Those member functions are not very often called and used, but also not seldom. What I fear is that sometimes you may misinterpret a boolean as silent\/not_silent-flag when this has another meaning.<\/p>\n<p>An example where this could also be misleading:<\/p>\n<pre><code>if( b-&gt;getData( false ) &amp;&amp; something_other )\n<\/code><\/pre>\n<p>this could be mixed up with <code>b-&gt;getData() == false<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have several classes where a call to a member function may change state and sometimes not depending on a boolean with default value. A a; a.set( &#8220;foobar&#8221; ); assert( a.changed() == true ); versus A a; a.set( &#8220;foobar&#8221;, false ); assert( a.changed() == false ); Please note that those member functions are virtual. I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4362","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4362"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4362\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}