<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FormatException &#187; Assert</title>
	<atom:link href="http://www.formatexception.com/tag/assert/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.formatexception.com</link>
	<description>Ramblings on developing in the Windows World</description>
	<lastBuildDate>Fri, 23 Sep 2011 15:34:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Assert Yourself!</title>
		<link>http://www.formatexception.com/2008/09/assert-yourself/</link>
		<comments>http://www.formatexception.com/2008/09/assert-yourself/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 16:28:49 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming Standards]]></category>
		<category><![CDATA[Assert]]></category>

		<guid isPermaLink="false">http://www.formatexception.com/?p=74</guid>
		<description><![CDATA[Java folks, please don&#8217;t get scared off by the C# examples, the contents of the post apply regardless if it is java or C#. None of us use Asserts enough. It&#8217;s that simple, we don&#8217;t. Asserts are essentially a way to ensure we don&#8217;t get a value we shouldn&#8217;t have. The great thing about using [...]]]></description>
			<content:encoded><![CDATA[<p>Java folks, please don&#8217;t get scared off by the C# examples, the contents of the post apply regardless if it is java or C#.</p>
<p>None of us use Asserts enough. It&#8217;s that simple, we don&#8217;t. Asserts are essentially a way to ensure we don&#8217;t get a value we shouldn&#8217;t have. The great thing about using asserts is that they aren&#8217;t included in final code (compiled in &#8220;Release&#8221;), only code complied with &#8220;Debug&#8221;. They make testing easier by verifying that when code is being tested in Debug that it doesn&#8217;t go beyond some condition. They can also be used to ensure when troubleshooting that you have the values you should.</p>
<p>Take a look at the code below:</p>
<pre name="code" class="C#">
private void pass_Click(object sender, RoutedEventArgs e)
{
    int temp = 1;
    Debug.Assert(temp > 0);
    MessageBox.Show(“Pass”);
}

private void fail_Click(object sender, RoutedEventArgs e)
{
    int temp = -1;
    Debug.Assert(temp > 0);
    MessageBox.Show(“Fail”);
}
</pre>
<p>This is a sample of using an assert. I know the example is trivial but I wanted to show a simple use of it. These are simply a Click event on a &#8220;Pass&#8221; button and a Click event on a &#8220;Fail&#8221; button. I would recommend you create your own solution and put these on a pass and fail button so you can see and understand what happens when Debug.Assert is called.</p>
<p>Now lately I&#8217;ve been coming across a lot of code like (anonymous-ized for your protection):</p>
<div style="font-family:Monospace; font-size: 10pt; background-color: white;"><span style="color: #0000ff;">else<br />
</span><span style="color: #000000;">{<br />
</span><span style="color: #2b91af;">    MessageBox</span><span style="color: #000000;">.Show(</span><span style="color: #a31515;">&#8220;ERROR: YOU SHOULD NEVER SEE THIS MESSAGE.  Invalid TransformString!&#8221;</span><span style="color: #000000;">, </span><span style="color: #a31515;">&#8220;SERIOUS ERROR occurred in MyClass:MyMethod&#8221;</span><span style="color: #000000;">, </span><span style="color: #2b91af;">MessageBoxButton</span><span style="color: #000000;">.OK, </span><span style="color: #2b91af;">MessageBoxImage</span><span style="color: #000000;">.Error);<br />
}</span></div>
<p>I&#8217;m sorry but this is simply wrong.</p>
<p>Code Complete 2nd edition has a great chapter on Assertions. The above example is exactly where asserts should be used.</p>
<p>Per the guidelines for &#8220;Using Assertions&#8221; (McConnnel, Code Complete 2, 2004) the following guidelines should apply for using assertions:</p>
<p>&#8220;Use error-handling code for conditions you expect to occur; use assertions for conditions that should never occur.</p>
<p>Avoid putting executable code into assertions.</p>
<p>Use assertions to document and verify preconditions and post conditions.</p>
<p>For highly robust code, assert and then handle the error anyway.&#8221;</p>
<p>That&#8217;s right. The very first usage guideline says to use asserts for conditions that should never occur.</p>
<p>Take a look at a sample from Code Complete (originally VB but I ported it to C#):</p>
<div style="font-family:Monospace; font-size: 10pt; background-color: white;"><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">double</span><span style="color: #000000;"> Velocity(</span><span style="color: #0000ff;">double</span><span style="color: #000000;"> Latitude, </span><span style="color: #0000ff;">double</span><span style="color: #000000;"> Longitude, </span><span style="color: #0000ff;">double</span><span style="color: #000000;"> Elevation)<br />
{<br />
</span><span style="color: #008000;">    //Verify preconditions<br />
</span><span style="color: #000000;">    </span><span style="color: #2b91af;">Debug</span><span style="color: #000000;">.Assert(Latitude &gt;= -90 &amp;&amp; Latitude &lt;= 90, </span><span style="color: #a31515;">&#8220;Invalid Latitude&#8221;</span><span style="color: #000000;">);<br />
    </span><span style="color: #2b91af;">Debug</span><span style="color: #000000;">.Assert(Longitude &gt;= 0 &amp;&amp; Longitude &lt;= 360, </span><span style="color: #a31515;">&#8220;Invalid Longitude&#8221;</span><span style="color: #000000;">);<br />
    </span><span style="color: #2b91af;">Debug</span><span style="color: #000000;">.Assert(Elevation &gt;= -500 &amp;&amp; Elevation &lt;= 75000, </span><span style="color: #a31515;">&#8220;Invalid Elevation&#8221;</span><span style="color: #000000;">);<br />
    </span></div>
<div style="font-family:Monospace; font-size: 10pt; background-color: white;">    <span style="color: #0000ff;">double</span><span style="color: #000000;"> velocity = 0;<br />
    </span><span style="color: #008000;">/* DO THE WORK */<span style="color: #000000;">    </span><span style="color: #008000;">//Verify postconditions<br />
</span><span style="color: #000000;">    </span><span style="color: #2b91af;">Debug</span><span style="color: #000000;">.Assert(velocity &gt;= 0 &amp;&amp; velocity &lt;= 600, <br />
                 </span><span style="color: #a31515;">&#8220;Invalid Velocity&#8221;</span><span style="color: #000000;">, <br />
                 </span><span style="color: #a31515;">&#8220;Can&#8217;t seem to properly work the velocity formula&#8221;</span><span style="color: #000000;">);<br />
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> velocity;<br />
}</span></p>
<p></span></div>
<p>On this McConnell has to say:<br />
&#8220;If the variables Latitude, Longitude, and Elevation were coming from an external source, invalid values should be checked and handled by error-handling code rather then by assertions. If the variables are coming from a trusted, internal source, however, and the routine&#8217;s design is based on the assumption that these values will be withn their valid ranges, then assertions are appropriate.&#8221; (McConnell, 2004)</p>
<p>Now assertions aren&#8217;t the end-all, be-all of validating values. Chances are that in most cases you are still going to want to validate and do error checking. But they&#8217;re a good start.</p>
<p>And just for the java folks that hung around, an assert in java:</p>
<div style="font-family:Monospace; font-size: 10pt; background-color: white;"><span style="color: #008000;">//Check to make sure we&#8217;re not going to do a divide by zero<br />
</span><span style="color: #000000;">assert denominator != 0: </span><span style="color: #a31515;">&#8220;denominator is 0&#8243;</span><span style="color: #000000;">;</span></div>
<p>Refs:</p>
<p>Code Complete (Second Edition), Microsoft Press, Steve McConnell, 2004</p>
<p><a href="http://en.csharp-online.net/Assert" target="_blank">C# Online.NET &#8211; Assert</a></p>
<p><a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.assert.aspx" target="_blank">Debug.Assert Method</a></p>
<p>And just for the java folks:<br />
<a href="http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html" target="_blank">Programming With Assertions</a></p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.formatexception.com%2F2008%2F09%2Fassert-yourself%2F&amp;t=Assert%20Yourself%21" title="Facebook"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.formatexception.com%2F2008%2F09%2Fassert-yourself%2F&amp;t=Assert%20Yourself%21" title="HackerNews"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.formatexception.com/2008/09/assert-yourself/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

