<?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>[Dot] Code Dump &#187; guidelines</title>
	<atom:link href="http://www.dotcodedump.com/category/guidelines/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotcodedump.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 06 Jan 2010 12:27:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Defensive Coding</title>
		<link>http://www.dotcodedump.com/2009/01/defensive-coding/</link>
		<comments>http://www.dotcodedump.com/2009/01/defensive-coding/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 05:02:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[guidelines]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://blog.lintnernet.com/?p=46</guid>
		<description><![CDATA[Inputs, and Data

Defensive coding produces code that accepts the fact that, inputs are going to be invalid, other code is probably not going to work right, and the data is bad.   In other words real world coding.  Sometimes as programmers we get caught in our own little bubbles where data is perfect, [...]]]></description>
			<content:encoded><![CDATA[<div>Inputs, and Data</div>
<div></div>
<div>Defensive coding produces code that accepts the fact that, inputs are going to be invalid, other code is probably not going to work right, and the data is bad.   In other words real world coding.  Sometimes as programmers we get caught in our own little bubbles where data is perfect, and nothing ever returns a null, but that is a pipe dream.  In the real world things change, and people make mistakes, and coding defensively is way to deal with this fact.</div>
<div> </div>
<div>To code defensively you must understand the following:</div>
<div></div>
<div>
<ul>
<li>Don&#8217;t make assumptions</li>
<li>You can&#8217;t control user input.</li>
<li>You can&#8217;t trust the data.</li>
<li>You can&#8217;t trust other code.</li>
<li>The requirements will change.</li>
<li>It always takes longer than you thought.</li>
<li>You can do it right now, or do it right again later.</li>
</ul>
</div>
<div></div>
<div>Defensively coding is necessary when confidence levels in technical, or other aspects of software project are low.  Defensive coding is not a solution, but a compensenatory technique for producing high quality code when coditions do not foster it.  Some people might say that defensive coding smells, and it&#8217;s not ideal, but I believe it to be acceptable when rewriting a project, or reworking other aspects of the project are not a viable option.</div>
<div></div>
<div>Examples of defensive coding using VB.NET</div>
<div></div>
<div><span class="Apple-style-span" style="font-weight: bold;">Example 1. Null check from a database call.</span></div>
<div></div>
<div>Dim myDbObject as DbObject</div>
<div>Dim myValue as string</div>
<div></div>
<div>myDbObject = DbLayer.GetDbObject()</div>
<div></div>
<div>If myDbObject IsNot Nothing  then</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>myValue = <span class="Apple-tab-span" style="white-space: pre;"> </span>myDbObject.Property</div>
<div>End If</div>
<div></div>
<div></div>
<div><span class="Apple-style-span" style="font-weight: bold;">Example 2. Checking input data</span></div>
<div><span class="Apple-style-span" style="font-weight: bold;"><br /></span></div>
<div>Public Sub GetInput(ByVal Input as DateTime)</div>
<div></div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>If Input > DateTime.MinValue AndAlso Input <>
<div><span class="Apple-tab-span" style="white-space: pre;">  </span>DbLayer.SaveDate(Input)</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>Else</div>
<div><span class="Apple-tab-span" style="white-space: pre;">  </span>DbLayer.SaveDate(DateTime.MinValue)</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>End If</div>
<div></div>
<div>End Sub</div>
<div></div>
<div>In example one, the basic premise is that we define our variable rather than just using the object value straight from the database, and check if it&#8217;s null before using it.  This is basic common sense, but this pattern can be extrapolated out, and inplementing these checks will never serve you wrong. In the second example before passing data back to the database we check to see if the date values are within range.  This is a basic check, but it is likely to cause errors when dealing with date inputs.  It is always a good idea to use a basic check to see if your data is at least in a general range.  It will save you headaches later.</div>
<div></div>
<div>These were very rudimentary examples, but this is the fundamental of coding defensively.  This all can be distilled down to the idea that you can only control data once it is inside of your application, and everthing else will probably be corrupt, invalid, or buggy at some point, so deal with it.  </div>
<div></div>
<div>To be continued</div>
</div>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.dotcodedump.com/2009/01/defensive-coding/&amp;title=Defensive+Coding" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.dotcodedump.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.dotcodedump.com/2009/01/defensive-coding/&amp;title=Defensive+Coding" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.dotcodedump.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dotnetkicks.com/kick/?url=http://www.dotcodedump.com/2009/01/defensive-coding/&amp;title=Defensive+Coding" rel="nofollow" title="Add to&nbsp;DotNetKicks"><img class="social_img" src="http://www.dotcodedump.com/wp-content/plugins/social-bookmarks/images/dotnetkicks.png" title="Add to&nbsp;DotNetKicks" alt="Add to&nbsp;DotNetKicks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Defensive+Coding&amp;url=http://www.dotcodedump.com/2009/01/defensive-coding/&amp;title=Defensive+Coding" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.dotcodedump.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://www.dotcodedump.com/2009/01/defensive-coding/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.dotcodedump.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.dotcodedump.com/2009/01/defensive-coding/&amp;title=Defensive+Coding" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://www.dotcodedump.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.dotcodedump.com/2009/01/defensive-coding/&amp;title=Defensive+Coding" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.dotcodedump.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://www.dotcodedump.com/2009/01/defensive-coding/&amp;title=Defensive+Coding" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.dotcodedump.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.dotcodedump.com/2009/01/defensive-coding/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.dotcodedump.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Defensive+Coding+@+http://www.dotcodedump.com/2009/01/defensive-coding/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.dotcodedump.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.dotcodedump.com/2009/01/defensive-coding/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
