<?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>my code trip &#187; Best Practices</title>
	<atom:link href="http://mycodetrip.com/tag/coding-software-best-practices/feed/" rel="self" type="application/rss+xml" />
	<link>http://mycodetrip.com</link>
	<description>stories from the information technology highway</description>
	<lastBuildDate>Mon, 29 Nov 2010 19:58:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Guidelines For Code Comments &#8211; Do&#8217;s And Don&#8217;ts</title>
		<link>http://mycodetrip.com/2009/01/08/guidelines-for-code-comments-dos-and-donts_236/</link>
		<comments>http://mycodetrip.com/2009/01/08/guidelines-for-code-comments-dos-and-donts_236/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 03:02:05 +0000</pubDate>
		<dc:creator>Shiva Manjunath</dc:creator>
				<category><![CDATA[Tips / HowTos]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Opinion Column]]></category>

		<guid isPermaLink="false">http://mycodetrip.com/?p=236</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[]]></content:encoded>
			<wfw:commentRss>http://mycodetrip.com/2009/01/08/guidelines-for-code-comments-dos-and-donts_236/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Casting Syntax Vs AS Operator &#8211; Which Approach Is Better And Why ?</title>
		<link>http://mycodetrip.com/2008/10/15/casting-syntax-vs-as-operator-which-approach-better-why-performance_105/</link>
		<comments>http://mycodetrip.com/2008/10/15/casting-syntax-vs-as-operator-which-approach-better-why-performance_105/#comments</comments>
		<pubDate>Wed, 15 Oct 2008 23:25:54 +0000</pubDate>
		<dc:creator>Shiva Manjunath</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[C# .Net]]></category>

		<guid isPermaLink="false">http://mycodetrip.com/?p=105</guid>
		<description><![CDATA[The two most popular approaches to casting &#8211; converting data types and objects  &#8211; in C#  are Using the cast ]]></description>
			<content:encoded><![CDATA[<p>The two most popular approaches to casting &#8211; converting data types and objects  &#8211; in C#  are</p>
<ol>
<li> Using the <strong>cast syntax </strong>i.e. (TypeToBeCastTo) (type/object) and</li>
<li> Using the <strong>AS operator </strong>i.e. (type/object) AS TypeToBeCastTo</li>
</ol>
<p>It is speculated that most developers use the first approach, but have you ever wondered which approach is the better one, is more performance efficient, and why ?</p>
<p>According to <strong>Jon Skeet</strong> (on MSDN), the fundamental difference in the two approaches is as follows.</p>
<ol>
<li> The AS operator returns a null  when the variable you are trying to convert is not of the requested type or in it&#8217;s inheritance chain. The cast syntax, on the other hand, throws an exception.</li>
<li> AS operator can only be used on reference type variables, whereas the cast syntax can be applied to reference and value types.</li>
<li> The AS operator also will not be able to perform conversions on user-defined types, whereas the casting syntax will be able to accomplish this.</li>
</ol>
<p><strong>Performance</strong>: According to <strong>Jon</strong>, the <strong>AS operator syntax </strong>is known to be slightly faster than the casting syntax in <strong>v1.0</strong> and <strong>v1.1</strong>.</p>
<p>Moreover, to me, it makes sense to use the <strong>AS operator</strong> where possible also because it does not throw an exception, which are in general very expensive in terms of performance. Lastly, I think the AS syntax also looks &#8220;more elegant&#8221;.</p>
<p><strong>Further Reading</strong></p>
<ul>
<li><a title="Jon Skeet's C# Frequently Asked Questions " href="http://blogs.msdn.com/csharpfaq/archive/2004/03/12/88420.aspx" target="_blank"> Jon Skeet&#8217;s C# Frequently Asked Questions</a></li>
<li><a title="Ian Griffiths' essay on C#'s approach to Casting" href="http://www.interact-sw.co.uk/iangblog/2004/01/20/casting" target="_blank">Ian Griffiths&#8217; essay on C#&#8217;s approach to Casting</a>. <em>(1500+ words including code).</em></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://mycodetrip.com/2008/10/15/casting-syntax-vs-as-operator-which-approach-better-why-performance_105/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Safest, Most Efficient Way To Clear A StringBuilder Variable</title>
		<link>http://mycodetrip.com/2008/09/17/safest-most-efficient-way-to-clear-a-stringbuilder-variable_10/</link>
		<comments>http://mycodetrip.com/2008/09/17/safest-most-efficient-way-to-clear-a-stringbuilder-variable_10/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 04:02:30 +0000</pubDate>
		<dc:creator>Shiva Manjunath</dc:creator>
				<category><![CDATA[Tips / HowTos]]></category>
		<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Best Practices]]></category>

		<guid isPermaLink="false">http://mycodetrip.com/?p=10</guid>
		<description><![CDATA[The safest and most efficient way to clear a StringBuilder variable is as follows.]]></description>
			<content:encoded><![CDATA[<p>The safest and most efficient way to clear a <strong>StringBuilder </strong>variable is as follows</p>
<pre class="brush: csharp">
stringBuilder sbr = new StringBuilder();
// loop
// more code
// clear sbr variable
sbr.Length = 0;
</pre>
<p><strong>Why ?</strong></p>
<p>It is a well know fact in the .Net community that using StringBuilder for string concatenation and manipulation is far more efficient that using the string class. The problem arises when you need to clear the StringBuilder variable. For regular string variables, you would simply set the variable equal to &#8220;&#8221;. However, you cannot do that  with the StringBuilder class. Moreover, the StringBuilder does not have a Clear() method as one would expect.</p>
<p>So when using a <strong>StringBuilder </strong>in a loop, for example, to read from a DataReader and write to a FileStream line by line, you are left with atleast 3 choices.</p>
<ol>
<li>Use the <strong>Remove() </strong>method, to remove all the characters in the variable.</li>
<li>Use <strong>new </strong>to recreate a new instance of the StringBuilder</li>
<li>Use the <strong>Clear() </strong>method.</li>
</ol>
<p>Using the <strong>Remove()</strong> method is not efficient, and could also throw an error if your variable has no characters in it. Using <strong>new </strong>to recreate the instance is obviously a performance overhead. So Of these 3, I would recommend that the last option.</p>
<p>Using the <strong>Clear()</strong> method is the safest and most efficient !</p>
]]></content:encoded>
			<wfw:commentRss>http://mycodetrip.com/2008/09/17/safest-most-efficient-way-to-clear-a-stringbuilder-variable_10/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

