<?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>Internet Marketing &#38; Technology Blog &#124; R2i &#187; programming</title>
	<atom:link href="http://www.r2integrated.com/blog/index.php/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.r2integrated.com/blog</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 14:43:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tips and Tricks: String Manipulation</title>
		<link>http://www.r2integrated.com/blog/index.php/tips-and-tricks-string-manipulation/</link>
		<comments>http://www.r2integrated.com/blog/index.php/tips-and-tricks-string-manipulation/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 20:02:39 +0000</pubDate>
		<dc:creator>NickChristy</dc:creator>
				<category><![CDATA[Engineering Best Practices]]></category>
		<category><![CDATA[Thought Leadership]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[technology]]></category>

		<guid isPermaLink="false">http://www.r2integrated.com/blog/?p=774</guid>
		<description><![CDATA[As engineers, one of the most common things we need to do is manipulate strings in some fashion.  All of us have had to compare a value to a constant at some point in our careers as software developers.  The most common code snippet that I see when doing this looks similar to:]]></description>
			<content:encoded><![CDATA[<p>As engineers, one of the most common things we need to do is manipulate strings in some fashion. All of us have had to compare a value to a constant at some point in our careers as software developers. The most common code snippet that I see when doing this looks similar to:</p>
<p><a href="http://www.r2integrated.com/blog/wp-content/uploads/2009/10/code_11.JPG"><img class="alignleft size-full wp-image-763" src="http://www.r2integrated.com/blog/wp-content/uploads/2009/10/code_11.JPG" alt="code_1" width="559" height="95" /></a></p>
<p>Now take a second to ask yourself, what is wrong with this? You might write a quick unit test to pass in values like &#8220;Is THIS the same&#8221; or &#8220;This doesn&#8217;t equal&#8221; and more than likely &#8220;Is this the same&#8221;. The tests all pass and you think your job is done. Stop to think about your method again; is there something wrong? Your tests pass and you have used common practices so everything should be good. Remember the golden rule &#8211; &#8220;Always practice defensive programming&#8221;. What if I wrote a test looked like:</p>
<p><a href="http://www.r2integrated.com/blog/wp-content/uploads/2009/10/code_33.JPG"><img class="alignleft size-full wp-image-770" src="http://www.r2integrated.com/blog/wp-content/uploads/2009/10/code_33.JPG" alt="code_3" width="589" height="48" /></a></p>
<p>Ahh, a null pointer exception. Now take a second look at your code. You could go back and add a quick check for a null parameter into your code. What does that really do? Your taking care of the &#8220;defensive&#8221; programming rule, but at the same time your adding lines of code. Do you really want someone else coming into your project and reading a bunch of wasted if(object==null) statements? Not really. One quick trick would be to reverse the order of your .equals()&#8230;</p>
<p><a href="http://www.r2integrated.com/blog/wp-content/uploads/2009/10/code_21.JPG"><img class="alignleft size-full wp-image-765" src="http://www.r2integrated.com/blog/wp-content/uploads/2009/10/code_21.JPG" alt="code_2" width="561" height="110" /></a></p>
<p>Now what have we done? By simply reversing the order so that the object that is checking for the equality is a known value, we have defended against the possibility of a null value exception. No need to check to see if x is null, rather we would return false, because after all, they aren&#8217;t equal. This trick can be applied in other cases, such as casting or returning portions of strings.</p>
<p>I&#8217;ll leave this blog open to discussion on other ways to easily protect yourselves against the dreaded null pointer exception&#8230;</p>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
]]></content:encoded>
			<wfw:commentRss>http://www.r2integrated.com/blog/index.php/tips-and-tricks-string-manipulation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

