    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: QM Bites â€“ The two sides of Boolean Parameters</title>
        <link>https://members.accu.org/index.php/journals/2183</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #130 - December 2015 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c356/">o130</a>
                    (7)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c356-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c356+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;QM Bites â€“ The two sides of Boolean Parameters</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 03 December 2015 09:46:25 +00:00 or Thu, 03 December 2015 09:46:25 +00:00</p>
<p><strong>Summary:</strong>&nbsp;Boolean parameters are tempting but make life difficult. Matthew Wilson advises us to avoid them (almost) all the time.</p>
<p><strong>Body:</strong>&nbsp;<table class="sidebartable">
	<tr>
		<td>
			<table class="journaltable">
				<tr>
					<th>QM Bites - Introduction</th>
				</tr>
				<tr>
					<td><p>Welcome to QM Bites!</p>
						
						<p>Those gentle readers of patience and good memory will remember that I write the Quality Matters column in this journal, only I havenâ€™t done so in quite some time. The problem is a simple one: Iâ€™m stuck halfway in my exceptions series, Iâ€™m a perfectionist (and not in a good way, in truth), and I havenâ€™t been able to get past my commitment to finish the series and move on to the many other topics Iâ€™d planned (and still intend) to cover.</p>
						
						<p>Our patient editor, Frances Buontempo, has offered gentle encouragement from time to time, and very recently had the liberating (for me) insight that I should write about other things until my exception-mojo is fully charged. Further, Iâ€™ve just taken on a new long-term role as Application Architect for a company that does extremely high-performance software (in many technologies, but mainly C++), with a remit to transform and modernise. This means Iâ€™m going to have a tremendous amount of new material to draw on and huge motivation to rekindle my previous intentions to wander the software quality landscape dropping my magic pixie dust.</p>
						
						<p>Thus: QM Bites. This new vehicle will let me guarantee my contributions to every (or most) Overload issue, usually two or three Bites and the occasional full-size Quality Matters instalment when Iâ€™m able. Itâ€™s win-win, baby! ;-)</p>
						
						<p><strong>Recap</strong></p>
						
						<p>Iâ€™ll be relying on a host of previously discussed topics in the Bites. As I examine each one, Iâ€™ll provide a short definition, reference to previously discussed points in QM, and relevant references to established works.</p>
						
						<p><strong>TL;DR</strong></p>
						
						<p>Even prior to the advent of the Twitter age I was informed with disheartening frequency that my written prolixity was enervating to an unsettling and, often, self-defeating degree. As a consequence, not only will I be putting very real effort into being succinct, I will also pander to the 140-character generation by including a pithiest-possible executive summary at the head of each Bite. (Sigh!)</p>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>

<h2>TL;DR</h2>
<p class="EditorIntro">Writing fns with bool params quick&amp;easy; costs maintainers of cli-code far more effort than U save</p>

<h2>Bite</h2>

<p>As Iâ€™ve discussed previously on a number of occasions ([<a href="#[XSTLv1]">XSTLv1</a>, <a href="#[QM#1]">QM#1</a>, <a href="#[QM#2]">QM#2</a>]) and as is evident from deductive reasoning alone, combinations of characteristics of software quality are often in conflict: <em>expressiveness</em> vs <em>efficiency</em>; <em>efficiency</em> vs <em>portability</em>; and so forth. This is not always the case, however, and usually the sub-divisions of the composite characteristic <em>discoverability &amp; transparency</em> (see sidebar) collaborate well and, further, tend to also to work well with others, especially <em>modularity</em>, but also (depending on how good is the design of the given component) with <em>expressiveness</em>, <em>flexibility</em>, <em>portability</em>, <em>correctness</em>/<em>robustness</em>/<em>reliability</em>, and, even, <em>efficiency</em>.</p>

<p>However, sometimes these very collegial subdivisions work against each other. In my experience, the most common example of this is when it comes to using Boolean parameters.</p>

<p>Consider a programmer tasked with writing an API for the manipulation of fonts, such that one is able to create a font from a base family along with selecting emboldenment, italicisation, underlining, and superscripting. The API might look like the following:</p>

<pre class="programlisting">
  Font CreateFont(string family, bool bold, bool
  italicised, bool underlined, bool superscripted);</pre>
  
<p>From the perspective of <strong>discoverability</strong> of the API, this is great: As a user I specify the family, then each of my (Boolean) choices as to whether it is emboldened, italicised, underlined, and superscripted. What could be clearer?</p>

<p>Similarly, the <strong>transparency</strong> of the <em>implementation</em> of this is likely to be high, since the name of each of the parameters clearly indicates its intent.</p>

<table class="sidebartable">
	<tr>
		<td class="title">Definition of Discoverability &amp; Transparency</td>
	</tr>
	<tr>
		<td>
			<p><strong>Discoverability</strong> is how easy it is to understand a component in order to be able to use it.</p>
			
			<p><strong>Transparency</strong> is how easy it is to understand a component in order to be able to change it.</p>
		</td>
	</tr>

</table>

<p>However, the problem comes in the <strong>transparency</strong> <em>of the client code</em>. What does any of us know of the majority of the intended semantics of the following statement:</p>

<pre class="programlisting">
  Font f = CreateFont(&quot;Courier&quot;, true, false,
  false, false);</pre>

<p>This violates one of the most important of the Principles of UNIX Programming (PoUP) [<a href="#[AoUP]">AoUP</a>, <a href="#[XSTLv1]">XSTLv1</a>]: </p>

<p class="lesson"><strong>Principle of Transparency</strong>: Design for visibility to make inspection and debugging easier.</p>

<p>If I want to understand the statement in the client code, I have to look at the API. The use of booleans has made me do work I should not need to do in an ideal world. Thus, thereâ€™s another PoUP violation:</p>

<p class="lesson"><strong>Principle of Economy</strong>: Programmer time is expensive; conserve it in preference to machine time.</p>

<p>(NOTE: Iâ€™m not even going to touch on the nightmare of what happens if the API designer decides to reorder some of the parameters â€¦)</p>

<p>So whatâ€™s the humble programmer to do? The answer is simple to state and, at least for those languages that support enumeration(-like) types, straightforward to implement: make each parameter be of a different type.</p>

<p>API:</p>

<pre class="programlisting">
  Font CreateFont(string family, Emboldenment bold,
  Italicisation italicised, Underlining underlined,
  Superscripting superscripted);</pre>
  
<p>Client-code:</p>

<pre class="programlisting">
  Font f = CreateFont(&quot;Courier&quot;, Emboldenment.Bold,
  Italicisation.None, Underlining.None,
  Superscripting.None);</pre>

<p>For certain, this is somewhat more effort to write (although if you have an Intellisense-like editor it may well be less effort and faster). But without a doubt your maintenance efforts will be <em>massively</em> less.</p>

<p>Note that this phenomenon doesnâ€™t even have to involve multiple parameters. One of my longest standing mistakes in this respect was in the constructors of reference-counting smart pointers, as seen in listing 1.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
// namespace stlsoft
  template &lt;typename T&gt;
  class ref_ptr
  {
    . . .
  public: // Construction
    ref_ptr(
      T* pt
    , bool addRef
    );
    . . .
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>To be sure, when looking at uses of this in client code you donâ€™t have the ambiguity of multiple parameters, but you still have to know what the second parameter of <code>stlsoft::ref_ptr</code>â€™s constructor does. And itâ€™s harder to grep your code for reference copying vs owning in a codebase.</p>

<p>So, the advice is: <strong>Avoid Boolean parameters (almost) all the time. </strong></p>

<h2>References</h2>

<p class="bibliomixed"><a id="[AoUP]"></a>[AoUP] <em>The Art of UNIX Programming</em>, Eric S. Raymond, Addison-Wesley, 2003</p>

<p class="bibliomixed"><a id="[QM#1]"></a>[QM#1] Quality Matters: Introductions and Nomenclature, Matthew Wilson, <em>Overload</em> 92, August 2009</p>

<p class="bibliomixed"><a id="[QM#2]"></a>[QM#2] Quality Matters: Correctness, Robustness, and Reliability, Matthew Wilson, <em>Overload</em> 93, October 2009</p>

<p class="bibliomixed"><a id="[XSTLv1]"></a>[XSTLv1] <em>Extended STL, volume 1: Collections and Iterators</em>, Matthew Wilson, Addison-Wesley, 2007</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
