<?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: Assertions in constructors</title>
	<atom:link href="http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Tue, 16 Mar 2010 05:27:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Coding: Checking invariants in a factory method at Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/comment-page-1/#comment-22252</link>
		<dc:creator>Coding: Checking invariants in a factory method at Mark Needham</dc:creator>
		<pubDate>Sat, 05 Sep 2009 14:47:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=939#comment-22252</guid>
		<description>[...] after that I suggested that I didn&#039;t think it should go in the constructor of an object but that we should rely on objects to be good citizens and not pass in null values or the like to [...]</description>
		<content:encoded><![CDATA[<p>[...] after that I suggested that I didn't think it should go in the constructor of an object but that we should rely on objects to be good citizens and not pass in null values or the like to [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jacob Stanley</title>
		<link>http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/comment-page-1/#comment-9990</link>
		<dc:creator>Jacob Stanley</dc:creator>
		<pubDate>Fri, 20 Feb 2009 00:34:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=939#comment-9990</guid>
		<description>Looks like the &lt;generics&gt; and code formatting were stripped out of the example, but you get the idea :)</description>
		<content:encoded><![CDATA[<p>Looks like the &lt;generics&gt; and code formatting were stripped out of the example, but you get the idea <img src='http://www.markhneedham.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jacob Stanley</title>
		<link>http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/comment-page-1/#comment-9986</link>
		<dc:creator>Jacob Stanley</dc:creator>
		<pubDate>Fri, 20 Feb 2009 00:31:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=939#comment-9986</guid>
		<description>Checking constructor arguments can be done in a fairly non intrusive way with extension methods.

I use an extension method in the form - T NotNull(this T, string) - which allows for the following syntax:

public ControllerContext(HttpContextBase httpContext, RouteData routeData, ControllerBase controller)
    : base(httpContext, routeData)
{
    Controller = controller.NotNull(&quot;controller&quot;);
}

Tagged with the right attributes (from JetBrains.Annotations), ReSharper will even give you intellisense on the string which represents the parameter name.

[DebuggerHidden]
[AssertionMethod]
public static T NotNull(
    [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] this T argument,
    [InvokerParameterName] string argumentName) where T : class
{
    if (argument == null)
    {
        throw new ArgumentNullException(argumentName);
    }

    return argument;
}</description>
		<content:encoded><![CDATA[<p>Checking constructor arguments can be done in a fairly non intrusive way with extension methods.</p>
<p>I use an extension method in the form &#8211; T NotNull(this T, string) &#8211; which allows for the following syntax:</p>
<p>public ControllerContext(HttpContextBase httpContext, RouteData routeData, ControllerBase controller)<br />
    : base(httpContext, routeData)<br />
{<br />
    Controller = controller.NotNull("controller");<br />
}</p>
<p>Tagged with the right attributes (from JetBrains.Annotations), ReSharper will even give you intellisense on the string which represents the parameter name.</p>
<p>[DebuggerHidden]<br />
[AssertionMethod]<br />
public static T NotNull(<br />
    [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] this T argument,<br />
    [InvokerParameterName] string argumentName) where T : class<br />
{<br />
    if (argument == null)<br />
    {<br />
        throw new ArgumentNullException(argumentName);<br />
    }</p>
<p>    return argument;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/comment-page-1/#comment-9393</link>
		<dc:creator>Mark Needham</dc:creator>
		<pubDate>Mon, 16 Feb 2009 13:40:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=939#comment-9393</guid>
		<description>&quot;Hhhmmm … does the server-rule about client data submission apply here?&quot;

Good point, I guess the boundary of client/server would be an area where I wouldn&#039;t trust the data coming in.

Re: Developers on the team etc - that was exactly the discussion being used in favour of having the assertions when we talked about it in the book club.

I&#039;m not sure what the answer is - I&#039;ve not worked on a project where we put these types of assertions throughout the codebase, it would be interesting to see how it worked out.</description>
		<content:encoded><![CDATA[<p>"Hhhmmm … does the server-rule about client data submission apply here?"</p>
<p>Good point, I guess the boundary of client/server would be an area where I wouldn't trust the data coming in.</p>
<p>Re: Developers on the team etc &#8211; that was exactly the discussion being used in favour of having the assertions when we talked about it in the book club.</p>
<p>I'm not sure what the answer is &#8211; I've not worked on a project where we put these types of assertions throughout the codebase, it would be interesting to see how it worked out.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul</title>
		<link>http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/comment-page-1/#comment-9389</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Mon, 16 Feb 2009 13:32:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=939#comment-9389</guid>
		<description>&quot;we would assume that our classes are good citizens&quot;

Hhhmmm ... does the server-rule about client data submission apply here?

We always tell our server-side developers not to trust the client input, to always validate it: The client is always either stupid or malicious.

Without validating the inputs, we&#039;re assuming that both current and future callers will be &quot;good citizens&quot; -- but how many developers are on the team?  How many maintenance developers will fix defects and add features to this codebase over the next five or ten years?

Will they all develop code that meets the &quot;good citizen&quot;-test and what the potential consequences of them not doing so?  Just a null-pointer exception or something more serious?

I don&#039;t think the readability argument holds water, because the potential consequences if there&#039;s a situation where null results in an unexpected result, rather than an exception, could be high.</description>
		<content:encoded><![CDATA[<p>"we would assume that our classes are good citizens"</p>
<p>Hhhmmm &#8230; does the server-rule about client data submission apply here?</p>
<p>We always tell our server-side developers not to trust the client input, to always validate it: The client is always either stupid or malicious.</p>
<p>Without validating the inputs, we're assuming that both current and future callers will be "good citizens" &#8212; but how many developers are on the team?  How many maintenance developers will fix defects and add features to this codebase over the next five or ten years?</p>
<p>Will they all develop code that meets the "good citizen"-test and what the potential consequences of them not doing so?  Just a null-pointer exception or something more serious?</p>
<p>I don't think the readability argument holds water, because the potential consequences if there's a situation where null results in an unexpected result, rather than an exception, could be high.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reflective Perspective - Chris Alcock &#187; The Morning Brew #287</title>
		<link>http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/comment-page-1/#comment-9353</link>
		<dc:creator>Reflective Perspective - Chris Alcock &#187; The Morning Brew #287</dc:creator>
		<pubDate>Mon, 16 Feb 2009 08:37:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=939#comment-9353</guid>
		<description>[...] Coding: Assertions in constructors - Mark Needham considers the need for assertions to catch null references passed into constructors, and questions if this is a good idea [...]</description>
		<content:encoded><![CDATA[<p>[...] Coding: Assertions in constructors &#8211; Mark Needham considers the need for assertions to catch null references passed into constructors, and questions if this is a good idea [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arjan`s World &#187; LINKBLOG for February 14, 2009</title>
		<link>http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/comment-page-1/#comment-9113</link>
		<dc:creator>Arjan`s World &#187; LINKBLOG for February 14, 2009</dc:creator>
		<pubDate>Sat, 14 Feb 2009 22:01:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=939#comment-9113</guid>
		<description>[...] Coding: Assertions in constructors - Mark Needham &#8216; I suppose this is a limitation of the Java/C# type system that you can&#8217;t prevent null values being passed into a constructor or that you can even have a null value in the first place &#8216; [...]</description>
		<content:encoded><![CDATA[<p>[...] Coding: Assertions in constructors &#8211; Mark Needham ' I suppose this is a limitation of the Java/C# type system that you can't prevent null values being passed into a constructor or that you can even have a null value in the first place ' [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy D. Miller</title>
		<link>http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/comment-page-1/#comment-8906</link>
		<dc:creator>Jeremy D. Miller</dc:creator>
		<pubDate>Fri, 13 Feb 2009 17:40:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=939#comment-8906</guid>
		<description>My opinion is that the null ref argument checks are just unwieldy code noise.  I think the MVC team is doing that because of a coding standard more than a real need.  On any project that I do, all dependencies are generally filled by an IoC tool, so the threat of an NRE is effectively nil.  In that case, the null checks do more damage to readability than good in terms of error prevention/detection.

I think the MVC code is a poster child for the negative impact of defensive coding.  I find that code to be very hard to follow because of the overwhelming defensive code checks.

Besides, it&#039;s nice to be able to throw in nulls in unit tests when a dependency isn&#039;t really in play.</description>
		<content:encoded><![CDATA[<p>My opinion is that the null ref argument checks are just unwieldy code noise.  I think the MVC team is doing that because of a coding standard more than a real need.  On any project that I do, all dependencies are generally filled by an IoC tool, so the threat of an NRE is effectively nil.  In that case, the null checks do more damage to readability than good in terms of error prevention/detection.</p>
<p>I think the MVC code is a poster child for the negative impact of defensive coding.  I find that code to be very hard to follow because of the overwhelming defensive code checks.</p>
<p>Besides, it's nice to be able to throw in nulls in unit tests when a dependency isn't really in play.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
