<?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/"
		>
<channel>
	<title>Comments on: Coding: Passing booleans into methods</title>
	<atom:link href="http://www.markhneedham.com/blog/2009/04/08/coding-passing-booleans-into-methods/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog/2009/04/08/coding-passing-booleans-into-methods/</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Sun, 14 Mar 2010 14:43:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: ASP.NET MVC Archived Blog Posts, Page 1</title>
		<link>http://www.markhneedham.com/blog/2009/04/08/coding-passing-booleans-into-methods/comment-page-1/#comment-14324</link>
		<dc:creator>ASP.NET MVC Archived Blog Posts, Page 1</dc:creator>
		<pubDate>Mon, 13 Apr 2009 05:05:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1109#comment-14324</guid>
		<description>[...] to VoteCoding: Passing booleans into methods (4/7/2009)Tuesday, April 07, 2009 from www.markhneedham.comIn the specific case I was referring to in the post [...]</description>
		<content:encoded><![CDATA[<p>[...] to VoteCoding: Passing booleans into methods (4/7/2009)Tuesday, April 07, 2009 from <a href="http://www.markhneedham.comIn" rel="nofollow">http://www.markhneedham.comIn</a> the specific case I was referring to in the post [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivan Sanchez &#187; Dodging booleans</title>
		<link>http://www.markhneedham.com/blog/2009/04/08/coding-passing-booleans-into-methods/comment-page-1/#comment-14160</link>
		<dc:creator>Ivan Sanchez &#187; Dodging booleans</dc:creator>
		<pubDate>Sat, 11 Apr 2009 11:12:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1109#comment-14160</guid>
		<description>[...] Mark Needham&#8217;s post about how inexpressive an API can be when using booleans in method parameters, another clear [...]</description>
		<content:encoded><![CDATA[<p>[...] Mark Needham's post about how inexpressive an API can be when using booleans in method parameters, another clear [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2009/04/08/coding-passing-booleans-into-methods/comment-page-1/#comment-13982</link>
		<dc:creator>Mark Needham</dc:creator>
		<pubDate>Thu, 09 Apr 2009 15:28:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1109#comment-13982</guid>
		<description>@Carlo - I guess ideally it would be cool to get the field to render itself but with the template approach to the view I think it&#039;s very difficult/impossible to do it.

A colleague of mine showed me the seaside framework and although I haven&#039;t used it the whole way of doing things looks really nice. I think I need to understand smalltalk syntax a bit better to really get what&#039;s going on though!</description>
		<content:encoded><![CDATA[<p>@Carlo &#8211; I guess ideally it would be cool to get the field to render itself but with the template approach to the view I think it's very difficult/impossible to do it.</p>
<p>A colleague of mine showed me the seaside framework and although I haven't used it the whole way of doing things looks really nice. I think I need to understand smalltalk syntax a bit better to really get what's going on though!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carlo</title>
		<link>http://www.markhneedham.com/blog/2009/04/08/coding-passing-booleans-into-methods/comment-page-1/#comment-13950</link>
		<dc:creator>Carlo</dc:creator>
		<pubDate>Thu, 09 Apr 2009 08:21:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1109#comment-13950</guid>
		<description>The solution you propose is probably over-engineered and all that was needed was 2 distinct methods IMO.

I say over-engineered because as far as I can see you are using double-dispatch where there is only a single dimension of variability. EditMode is the one dimension while HtmlHelper is not (is that right...HtmlHelper is only ever one type (class)?).

The method name looks like it is trying to do 2 things, maybe it should be taking in a field representation object i.e. HtmlHelper.renderField(field) {
field.renderOn(this);
}


field could be an editable or readonly field which is polymorphic. Yes the decision on which type of field that must be created still needs to be decided, in a factory maybe, (much as the boolean check) but at least the solution is general and not as specific as the proposed solution.

I personally like the http://www.seaside.st frameworks way of using a canvas and &#039;brushes&#039; to display html, javascript etc.

Field&gt;&gt;renderOn: htmlCanvas
    ^htmlCanvas textInput
        value: textInputExample;
	callback: [ :value &#124; textInputExample := value ];
        beReadOnly. 

EditableField&gt;&gt;renderOn: htmlCanvas
    ^ (super renderOn: htmlCanvas) beEditable. 

EditableField would extend Field and the type of object (EditableField component or a readonly field component) would receive the polymorphic call to render itself on a canvas, in this case htmlCanvas.

I know what your post is trying to say but the benefit of going into double dispatch is harder to follow, albeit for the more explicit naming used which helps the user understand what is tryign to be achieved. Isn&#039;t simply easier to have a more descriptive intention revealing method or a more &#039;complete&#039; object that encapsulates the model and can render itself.

Good post...cheers</description>
		<content:encoded><![CDATA[<p>The solution you propose is probably over-engineered and all that was needed was 2 distinct methods IMO.</p>
<p>I say over-engineered because as far as I can see you are using double-dispatch where there is only a single dimension of variability. EditMode is the one dimension while HtmlHelper is not (is that right&#8230;HtmlHelper is only ever one type (class)?).</p>
<p>The method name looks like it is trying to do 2 things, maybe it should be taking in a field representation object i.e. HtmlHelper.renderField(field) {<br />
field.renderOn(this);<br />
}</p>
<p>field could be an editable or readonly field which is polymorphic. Yes the decision on which type of field that must be created still needs to be decided, in a factory maybe, (much as the boolean check) but at least the solution is general and not as specific as the proposed solution.</p>
<p>I personally like the <a href="http://www.seaside.st" rel="nofollow">http://www.seaside.st</a> frameworks way of using a canvas and 'brushes' to display html, javascript etc.</p>
<p>Field&gt;&gt;renderOn: htmlCanvas<br />
    ^htmlCanvas textInput<br />
        value: textInputExample;<br />
	callback: [ :value | textInputExample := value ];<br />
        beReadOnly. </p>
<p>EditableField&gt;&gt;renderOn: htmlCanvas<br />
    ^ (super renderOn: htmlCanvas) beEditable. </p>
<p>EditableField would extend Field and the type of object (EditableField component or a readonly field component) would receive the polymorphic call to render itself on a canvas, in this case htmlCanvas.</p>
<p>I know what your post is trying to say but the benefit of going into double dispatch is harder to follow, albeit for the more explicit naming used which helps the user understand what is tryign to be achieved. Isn't simply easier to have a more descriptive intention revealing method or a more 'complete' object that encapsulates the model and can render itself.</p>
<p>Good post&#8230;cheers</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob Scott</title>
		<link>http://www.markhneedham.com/blog/2009/04/08/coding-passing-booleans-into-methods/comment-page-1/#comment-13874</link>
		<dc:creator>Rob Scott</dc:creator>
		<pubDate>Wed, 08 Apr 2009 12:25:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1109#comment-13874</guid>
		<description>The problem with passing a boolean is that it indicates a method that does two things (whatever it does in the true case and whatever it does in the false case).

Your method interface became more expressive because you refactored the code to have a single purpose (RenderFieldWith) and a single purpose is more easily expressed.  

So, by focusing on expressability you were able to back into a very old design principle for methods.</description>
		<content:encoded><![CDATA[<p>The problem with passing a boolean is that it indicates a method that does two things (whatever it does in the true case and whatever it does in the false case).</p>
<p>Your method interface became more expressive because you refactored the code to have a single purpose (RenderFieldWith) and a single purpose is more easily expressed.  </p>
<p>So, by focusing on expressability you were able to back into a very old design principle for methods.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DotNetShoutout</title>
		<link>http://www.markhneedham.com/blog/2009/04/08/coding-passing-booleans-into-methods/comment-page-1/#comment-13862</link>
		<dc:creator>DotNetShoutout</dc:creator>
		<pubDate>Wed, 08 Apr 2009 09:19:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1109#comment-13862</guid>
		<description>&lt;strong&gt;Coding: Passing booleans into methods at Mark Needham...&lt;/strong&gt;

Thank you for submitting this cool story - Trackback from DotNetShoutout...</description>
		<content:encoded><![CDATA[<p><strong>Coding: Passing booleans into methods at Mark Needham&#8230;</strong></p>
<p>Thank you for submitting this cool story &#8211; Trackback from DotNetShoutout&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony Williams</title>
		<link>http://www.markhneedham.com/blog/2009/04/08/coding-passing-booleans-into-methods/comment-page-1/#comment-13855</link>
		<dc:creator>Anthony Williams</dc:creator>
		<pubDate>Wed, 08 Apr 2009 07:36:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1109#comment-13855</guid>
		<description>I agree that having a single method that does A or B depending on a boolean parameter is less than ideal. However you need to have something somewhere that makes a choice. If you are calling with a constant as in

HtmlHelper.DropDownOrReadOnly(true)

then we can just call the right method directly:

HtmlHelper.ReadOnly()

rather than fussing with an EditMode.

On the other hand, if we&#039;re not passing a constant then the type of the passed variable doesn&#039;t matter:

HtmlHelper.DropDownOrReadOnly(editMode)

Here it doesn&#039;t matter whether editMode is a boolean or an EditMode instance.

The big problem with boolean parameters is that the sense of the parameter is unclear --- what does &quot;true&quot; mean? If I set editMode to &quot;true&quot; does that mean I *can* edit, or I *cannot* edit? If we can encode this information as high up the call chain as possible it makes things clearer. If we make our editMode property hold an EditMode instance then in the logic that handles changing the mode we can assign EditMode.ReadOnly or EditMode.DropDown as appropriate. There is now no danger of disagreement over what &quot;true&quot; means --- this information is directly encoded into a behaviour choice at the point that the selection is made.</description>
		<content:encoded><![CDATA[<p>I agree that having a single method that does A or B depending on a boolean parameter is less than ideal. However you need to have something somewhere that makes a choice. If you are calling with a constant as in</p>
<p>HtmlHelper.DropDownOrReadOnly(true)</p>
<p>then we can just call the right method directly:</p>
<p>HtmlHelper.ReadOnly()</p>
<p>rather than fussing with an EditMode.</p>
<p>On the other hand, if we're not passing a constant then the type of the passed variable doesn't matter:</p>
<p>HtmlHelper.DropDownOrReadOnly(editMode)</p>
<p>Here it doesn't matter whether editMode is a boolean or an EditMode instance.</p>
<p>The big problem with boolean parameters is that the sense of the parameter is unclear &#8212; what does "true" mean? If I set editMode to "true" does that mean I *can* edit, or I *cannot* edit? If we can encode this information as high up the call chain as possible it makes things clearer. If we make our editMode property hold an EditMode instance then in the logic that handles changing the mode we can assign EditMode.ReadOnly or EditMode.DropDown as appropriate. There is now no danger of disagreement over what "true" means &#8212; this information is directly encoded into a behaviour choice at the point that the selection is made.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reflective Perspective - Chris Alcock &#187; The Morning Brew #324</title>
		<link>http://www.markhneedham.com/blog/2009/04/08/coding-passing-booleans-into-methods/comment-page-1/#comment-13853</link>
		<dc:creator>Reflective Perspective - Chris Alcock &#187; The Morning Brew #324</dc:creator>
		<pubDate>Wed, 08 Apr 2009 06:52:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1109#comment-13853</guid>
		<description>[...] Coding: Passing booleans into methods - Mark Needham talks about using boolean parameters to methods to change the flow of code, and how having separate methods for each flow is more expressive. [...]</description>
		<content:encoded><![CDATA[<p>[...] Coding: Passing booleans into methods &#8211; Mark Needham talks about using boolean parameters to methods to change the flow of code, and how having separate methods for each flow is more expressive. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger</title>
		<link>http://www.markhneedham.com/blog/2009/04/08/coding-passing-booleans-into-methods/comment-page-1/#comment-13830</link>
		<dc:creator>Roger</dc:creator>
		<pubDate>Wed, 08 Apr 2009 00:30:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1109#comment-13830</guid>
		<description>That is all well-and-good unless you make your client code look like the following:

if(someBoolean) o.DoThis(); 
else o.DoThat();

or, more confusing yet...
HtmlHelper.DropDownOrReadOnly(isReadOnly ? EditMode.ReadOnly : EditMode.Editable);

I get what your saying, I believe in it ( see http://csharptest.net/?p=198 ).  It&#039;s just worth saying that in NO WAY should you force the logic to the client, rather the goal is to eliminate the condition all together.  Provided the caller knows which type of control he is creating, the approach above works very well.  When the caller is deriving whether or not the control is read-only by some data (say an access check) the solution gets more troublesome.</description>
		<content:encoded><![CDATA[<p>That is all well-and-good unless you make your client code look like the following:</p>
<p>if(someBoolean) o.DoThis();<br />
else o.DoThat();</p>
<p>or, more confusing yet&#8230;<br />
HtmlHelper.DropDownOrReadOnly(isReadOnly ? EditMode.ReadOnly : EditMode.Editable);</p>
<p>I get what your saying, I believe in it ( see <a href="http://csharptest.net/?p=198" rel="nofollow">http://csharptest.net/?p=198</a> ).  It's just worth saying that in NO WAY should you force the logic to the client, rather the goal is to eliminate the condition all together.  Provided the caller knows which type of control he is creating, the approach above works very well.  When the caller is deriving whether or not the control is read-only by some data (say an access check) the solution gets more troublesome.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
