<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: avoid boolean parameters</title>
	<atom:link href="http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/feed/" rel="self" type="application/rss+xml" />
	<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/</link>
	<description>development, by example</description>
	<lastBuildDate>Tue, 07 Feb 2012 05:40:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: 2005 silk and spinach top ten &#171; silk and spinach</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-15521</link>
		<dc:creator><![CDATA[2005 silk and spinach top ten &#171; silk and spinach]]></dc:creator>
		<pubDate>Tue, 30 Jun 2009 15:36:02 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-15521</guid>
		<description><![CDATA[[...] editing in Rails 2.0setup and teardown for a ruby TestSuitea ruby sparkline showing variationavoid boolean parametersreek, a code smells detector for rubythe anti-if campaignnothing but &quot;uses&quot; and [...]]]></description>
		<content:encoded><![CDATA[<p>[...] editing in Rails 2.0setup and teardown for a ruby TestSuitea ruby sparkline showing variationavoid boolean parametersreek, a code smells detector for rubythe anti-if campaignnothing but &quot;uses&quot; and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: reek 0.3.0 released &#171; silk and spinach</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-15234</link>
		<dc:creator><![CDATA[reek 0.3.0 released &#171; silk and spinach]]></dc:creator>
		<pubDate>Sun, 02 Nov 2008 22:24:56 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-15234</guid>
		<description><![CDATA[[...] main news is the arrival of a new smell: I&#8217;ve included a first naive check for the Control Couple [...]]]></description>
		<content:encoded><![CDATA[<p>[...] main news is the arrival of a new smell: I&#8217;ve included a first naive check for the Control Couple [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: the anti-if campaign &#171; silk and spinach</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-15227</link>
		<dc:creator><![CDATA[the anti-if campaign &#171; silk and spinach]]></dc:creator>
		<pubDate>Tue, 28 Oct 2008 11:19:12 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-15227</guid>
		<description><![CDATA[[...] avoid boolean parameters [...]]]></description>
		<content:encoded><![CDATA[<p>[...] avoid boolean parameters [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Rutherford</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-15222</link>
		<dc:creator><![CDATA[Kevin Rutherford]]></dc:creator>
		<pubDate>Mon, 27 Oct 2008 08:32:51 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-15222</guid>
		<description><![CDATA[@Moffdub: You&#039;re right -- as noted by Colin in the first comment above, a boolean parameter is a special case of the Control Coupling smell. There&#039;s a section describing Control Coupling in my imminent book, the &lt;a href=&quot;http://silkandspinach.net/2008/09/15/ruby-refactoring-workbook/&quot; rel=&quot;nofollow&quot;&gt;Ruby Refactoring Workbook&lt;/a&gt;.]]></description>
		<content:encoded><![CDATA[<p>@Moffdub: You&#8217;re right &#8212; as noted by Colin in the first comment above, a boolean parameter is a special case of the Control Coupling smell. There&#8217;s a section describing Control Coupling in my imminent book, the <a href="http://silkandspinach.net/2008/09/15/ruby-refactoring-workbook/" rel="nofollow">Ruby Refactoring Workbook</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MoffDub</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-15219</link>
		<dc:creator><![CDATA[MoffDub]]></dc:creator>
		<pubDate>Sun, 26 Oct 2008 02:09:55 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-15219</guid>
		<description><![CDATA[I think you can extend this pattern further to any parameter, be it a boolean, enumeration, or whatever, that is used inside the algorithm to branch. I started doing this earlier this week at my workplace, but immediately stopped because I knew it smelled. I applaud your articulation of this code smell.]]></description>
		<content:encoded><![CDATA[<p>I think you can extend this pattern further to any parameter, be it a boolean, enumeration, or whatever, that is used inside the algorithm to branch. I started doing this earlier this week at my workplace, but immediately stopped because I knew it smelled. I applaud your articulation of this code smell.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Rutherford</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-13683</link>
		<dc:creator><![CDATA[Kevin Rutherford]]></dc:creator>
		<pubDate>Mon, 19 May 2008 20:50:12 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-13683</guid>
		<description><![CDATA[Hi Jack,  The key is that this string is input from the user.  If you consider your software in terms of the &lt;a href=&quot;http://silkandspinach.net/2004/07/16/hexagonal-soup/&quot; rel=&quot;nofollow&quot;&gt;hexagonal architecture&lt;/a&gt; pattern, the code you show would sit in an Adapter.  The job of the adapters is to convert between the domain objects and the real world -- in this case to convert between the user&#039;s string and an object of the equivalent class.  So it&#039;s good code (as long as it occurs only once in the system).]]></description>
		<content:encoded><![CDATA[<p>Hi Jack,  The key is that this string is input from the user.  If you consider your software in terms of the <a href="http://silkandspinach.net/2004/07/16/hexagonal-soup/" rel="nofollow">hexagonal architecture</a> pattern, the code you show would sit in an Adapter.  The job of the adapters is to convert between the domain objects and the real world &#8212; in this case to convert between the user&#8217;s string and an object of the equivalent class.  So it&#8217;s good code (as long as it occurs only once in the system).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jack zhang</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-13649</link>
		<dc:creator><![CDATA[jack zhang]]></dc:creator>
		<pubDate>Fri, 16 May 2008 00:25:04 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-13649</guid>
		<description><![CDATA[Kevin,  your comments are correct and so the key issue here is that the system somewhere know the decision; Here I got a question for you, hope you show me how to avoid the if else use pattern or some smart ideas:
say I got input from user which is string, for example &quot;Engineer&quot;, &quot;Manager&quot;, &quot;Saleman&quot; etc, in my program once I get this string, I should create object of this class accordingly, I am using C++, so the straigtforward way to do it as : 
if(name == &quot;Engineer&quot;)
 return new Engineer();
else if(name == &quot;Manager&quot;)
 return new Manager();
esle if  if(name == &quot;Saleman&quot;)
   return new Saleman();
else 
  return null;

I spent quite a while to see if I can use subclass to replace 
the type which is string here, but still I have to swith the decision to someone else, to create concret class of type;
Do you have any idea? Thanks]]></description>
		<content:encoded><![CDATA[<p>Kevin,  your comments are correct and so the key issue here is that the system somewhere know the decision; Here I got a question for you, hope you show me how to avoid the if else use pattern or some smart ideas:<br />
say I got input from user which is string, for example &#8220;Engineer&#8221;, &#8220;Manager&#8221;, &#8220;Saleman&#8221; etc, in my program once I get this string, I should create object of this class accordingly, I am using C++, so the straigtforward way to do it as :<br />
if(name == &#8220;Engineer&#8221;)<br />
 return new Engineer();<br />
else if(name == &#8220;Manager&#8221;)<br />
 return new Manager();<br />
esle if  if(name == &#8220;Saleman&#8221;)<br />
   return new Saleman();<br />
else<br />
  return null;</p>
<p>I spent quite a while to see if I can use subclass to replace<br />
the type which is string here, but still I have to swith the decision to someone else, to create concret class of type;<br />
Do you have any idea? Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 3 years old today! &#171; silk and spinach</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-949</link>
		<dc:creator><![CDATA[3 years old today! &#171; silk and spinach]]></dc:creator>
		<pubDate>Sat, 16 Jun 2007 19:42:00 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-949</guid>
		<description><![CDATA[[...] remained popular throughout - and indeed the stats show there&#8217;s been a lot of interest in avoid boolean parameters and if&#8230; just recently - both written in July of 2004. Those two, together with the product [...]]]></description>
		<content:encoded><![CDATA[<p>[...] remained popular throughout &#8211; and indeed the stats show there&#8217;s been a lot of interest in avoid boolean parameters and if&#8230; just recently &#8211; both written in July of 2004. Those two, together with the product [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Rutherford</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-695</link>
		<dc:creator><![CDATA[Kevin Rutherford]]></dc:creator>
		<pubDate>Fri, 08 Jun 2007 07:38:24 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-695</guid>
		<description><![CDATA[@Brian:

I agree that your enums are more readable (and extensible) than the booleans.  But they still suffer from the problem of duplication.  The code &lt;strong&gt;inside&lt;/strong&gt; the method must include conditional structures to figure out what to do.  And those conditionals duplicate knowledge that&#039;s already present in the method&#039;s caller.

And that, in turn, means that the system as a whole is harder to maintain.  Because each time we want to add a new file type, for example, we&#039;ll have to change and/or recompile many different classes.

So it seems to me that forcing all conceptually related behaviour (creating new things in the filestore) through this one interface makes us work too hard.  We have to work to make it readable, and we have to work harder when things change.  Which is why I have this little rule of thumb - when I feel the need to introduce a Boolean parameter, I take a step back and re-think my object design, before it&#039;s too late.]]></description>
		<content:encoded><![CDATA[<p>@Brian:</p>
<p>I agree that your enums are more readable (and extensible) than the booleans.  But they still suffer from the problem of duplication.  The code <strong>inside</strong> the method must include conditional structures to figure out what to do.  And those conditionals duplicate knowledge that&#8217;s already present in the method&#8217;s caller.</p>
<p>And that, in turn, means that the system as a whole is harder to maintain.  Because each time we want to add a new file type, for example, we&#8217;ll have to change and/or recompile many different classes.</p>
<p>So it seems to me that forcing all conceptually related behaviour (creating new things in the filestore) through this one interface makes us work too hard.  We have to work to make it readable, and we have to work harder when things change.  Which is why I have this little rule of thumb &#8211; when I feel the need to introduce a Boolean parameter, I take a step back and re-think my object design, before it&#8217;s too late.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-681</link>
		<dc:creator><![CDATA[Brian]]></dc:creator>
		<pubDate>Thu, 07 Jun 2007 20:45:05 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-681</guid>
		<description><![CDATA[This strikes me as a perfect place to use a type-safe enumeration.  Then instead of booleans, you could write something like this:
&lt;code&gt;
enum FileType { GenericFile, TemporaryFile }

File createFile(String name, FileType type);

// call like this:
File f = FileCreator.createFile(&quot;/my/file/path&quot;, FileType.GenericFile);
&lt;/code&gt;
This also has another benefit over booleans, namely the ability to assume more than two values.  You could easily extend the enumeration like this:
&lt;code&gt;
enum FileType { GenericFile, TemporaryFile, Directory }
&lt;/code&gt;
Not that this would be the best way to create a directory, of course, but it&#039;s more straightforward than this:
&lt;code&gt;
File createFile(String path, boolean isTemp, boolean isDir);
&lt;/code&gt;]]></description>
		<content:encoded><![CDATA[<p>This strikes me as a perfect place to use a type-safe enumeration.  Then instead of booleans, you could write something like this:<br />
<code><br />
enum FileType { GenericFile, TemporaryFile }</p>
<p>File createFile(String name, FileType type);</p>
<p>// call like this:<br />
File f = FileCreator.createFile("/my/file/path", FileType.GenericFile);<br />
</code><br />
This also has another benefit over booleans, namely the ability to assume more than two values.  You could easily extend the enumeration like this:<br />
<code><br />
enum FileType { GenericFile, TemporaryFile, Directory }<br />
</code><br />
Not that this would be the best way to create a directory, of course, but it&#8217;s more straightforward than this:<br />
<code><br />
File createFile(String path, boolean isTemp, boolean isDir);<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob Meyer</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-118</link>
		<dc:creator><![CDATA[Rob Meyer]]></dc:creator>
		<pubDate>Mon, 26 Jun 2006 20:24:08 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-118</guid>
		<description><![CDATA[I agree completely. In Dave&#039;s bold/underline example, Kevin points out correctly that single functions to affect formatting in that way is probably not the ideal API, so in trying to convert said function with 4 booleans to 16 individual methods, we&#039;ve discovered something that&#039;s probably a code smell, which has value.

What&#039;s more, is I&#039;d argue that even though it&#039;s more code and tedious for the author of the library, that the individual versions are still more readable. As an API consumer I&#039;d still rather write:

displayTextInBoldAndUnderline()

then:

displayText(true, true, false, true);

There&#039;s no way anyone reading the code later has a chance of understanding the 2nd one without checking out the displayText documentation, but the first one reads like English and is clear.

But Kevin&#039;s right, fix the API, because it&#039;s only a matter of time until someone wants that text to be able to marked superscript or subscript, and any sort of flagging technique is quickly going to get out of control.]]></description>
		<content:encoded><![CDATA[<p>I agree completely. In Dave&#8217;s bold/underline example, Kevin points out correctly that single functions to affect formatting in that way is probably not the ideal API, so in trying to convert said function with 4 booleans to 16 individual methods, we&#8217;ve discovered something that&#8217;s probably a code smell, which has value.</p>
<p>What&#8217;s more, is I&#8217;d argue that even though it&#8217;s more code and tedious for the author of the library, that the individual versions are still more readable. As an API consumer I&#8217;d still rather write:</p>
<p>displayTextInBoldAndUnderline()</p>
<p>then:</p>
<p>displayText(true, true, false, true);</p>
<p>There&#8217;s no way anyone reading the code later has a chance of understanding the 2nd one without checking out the displayText documentation, but the first one reads like English and is clear.</p>
<p>But Kevin&#8217;s right, fix the API, because it&#8217;s only a matter of time until someone wants that text to be able to marked superscript or subscript, and any sort of flagging technique is quickly going to get out of control.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kevin</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-117</link>
		<dc:creator><![CDATA[kevin]]></dc:creator>
		<pubDate>Wed, 10 May 2006 22:33:15 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-117</guid>
		<description><![CDATA[Nico Mommaerts has posted related thoughts at &lt;a href=&quot;http://www.hansei-kaizen.be/article/144/thissetdraftfalse-or-thispost&quot; rel=&quot;nofollow&quot;&gt;http://www.hansei-kaizen.be/article/144/thissetdraftfalse-or-thispost&lt;/a&gt;]]></description>
		<content:encoded><![CDATA[<p>Nico Mommaerts has posted related thoughts at <a href="http://www.hansei-kaizen.be/article/144/thissetdraftfalse-or-thispost" rel="nofollow">http://www.hansei-kaizen.be/article/144/thissetdraftfalse-or-thispost</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Rutherford</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-741</link>
		<dc:creator><![CDATA[Kevin Rutherford]]></dc:creator>
		<pubDate>Mon, 16 May 2005 22:00:01 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-741</guid>
		<description><![CDATA[Responding to Dave:
I would probably tackle your displayText example using Decorators. That is, I would look for a way to create little “do it in bold” and “underline it” objects that could modify or add to the original behaviour of the text string in question. As I tried to say in hexagonal soup, the particular caller who knows which text decorations it needs will assemble the string together with the appropriate decorators at that time. The rest of the system need never know that the decorators are there. Little objects each doing one job, instead of functions doing several.
]]></description>
		<content:encoded><![CDATA[<p>Responding to Dave:<br />
I would probably tackle your displayText example using Decorators. That is, I would look for a way to create little “do it in bold” and “underline it” objects that could modify or add to the original behaviour of the text string in question. As I tried to say in hexagonal soup, the particular caller who knows which text decorations it needs will assemble the string together with the appropriate decorators at that time. The rest of the system need never know that the decorators are there. Little objects each doing one job, instead of functions doing several.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Rutherford</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-742</link>
		<dc:creator><![CDATA[Kevin Rutherford]]></dc:creator>
		<pubDate>Mon, 16 May 2005 21:35:38 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-742</guid>
		<description><![CDATA[Responding to Rik:
I agree, many languages these days offer ways to render a boolean parameter more readable.  However, that does nothing to remove the duplication I pointed out - that somewhere up the calling stack is another method that already knows which code path should be taken.  As Colin points out, we should try to avoid that degree of interference and control.
]]></description>
		<content:encoded><![CDATA[<p>Responding to Rik:<br />
I agree, many languages these days offer ways to render a boolean parameter more readable.  However, that does nothing to remove the duplication I pointed out &#8211; that somewhere up the calling stack is another method that already knows which code path should be taken.  As Colin points out, we should try to avoid that degree of interference and control.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Kirby</title>
		<link>http://silkandspinach.net/2004/07/15/avoid-boolean-parameters/#comment-743</link>
		<dc:creator><![CDATA[Dave Kirby]]></dc:creator>
		<pubDate>Sun, 10 Apr 2005 11:24:10 +0000</pubDate>
		<guid isPermaLink="false">http://silkandspinach.wordpress.com/2004/07/15/avoid-boolean-parameters/#comment-743</guid>
		<description><![CDATA[Creating two different functions is fine when you only have one flag, but become a nightmare if you have more than one.  For example a &lt;strong&gt;displayText&lt;/strong&gt; function that can display text in any combination of bold, italic, underline and strikethrough would require 16 different functions - e.g. displayTextInBoldAndUnderline(...) etc. Having multiple functions with different names also makes it hard to change the behaviour at runtime.  Here is a simple exercise:  rewrite the above function into the 16 separate functions.  Then write a code snippet that iterates through a list of strings and prints it in bold if it contains a &#039;b&#039;, underline if it contains a &#039;u&#039;, italic if it contains an &#039;i&#039;, and strikethrough if it contains an &#039;s&#039;.  Multiple combinations are allowed, of course.As Rik says, in a modern language that allows named parameters with default values this becomes a non-issue.  Python has had this facility from the start, so you can call the function as: &lt;strong&gt;displayText(&quot;some text&quot;, bold=True, underline=True)&lt;/strong&gt;
]]></description>
		<content:encoded><![CDATA[<p>Creating two different functions is fine when you only have one flag, but become a nightmare if you have more than one.  For example a <strong>displayText</strong> function that can display text in any combination of bold, italic, underline and strikethrough would require 16 different functions &#8211; e.g. displayTextInBoldAndUnderline(&#8230;) etc. Having multiple functions with different names also makes it hard to change the behaviour at runtime.  Here is a simple exercise:  rewrite the above function into the 16 separate functions.  Then write a code snippet that iterates through a list of strings and prints it in bold if it contains a &#8216;b&#8217;, underline if it contains a &#8216;u&#8217;, italic if it contains an &#8216;i&#8217;, and strikethrough if it contains an &#8216;s&#8217;.  Multiple combinations are allowed, of course.As Rik says, in a modern language that allows named parameters with default values this becomes a non-issue.  Python has had this facility from the start, so you can call the function as: <strong>displayText(&#8220;some text&#8221;, bold=True, underline=True)</strong></p>
]]></content:encoded>
	</item>
</channel>
</rss>

