<?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>Think In Geek &#187; programming tips</title>
	<atom:link href="http://thinkingeek.com/tag/programming-tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkingeek.com</link>
	<description>In geek we trust</description>
	<lastBuildDate>Wed, 01 Feb 2012 14:45:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>C#, the &#8220;and&#8221; operators and lazy evaluation</title>
		<link>http://thinkingeek.com/2009/10/12/c-the-and-operators-and-lazy-evaluation/</link>
		<comments>http://thinkingeek.com/2009/10/12/c-the-and-operators-and-lazy-evaluation/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 15:00:11 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[programming tips]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=121</guid>
		<description><![CDATA[Today at work we found a bug. My workmate, not used to C#, usually uses the &#38; operator to compare boolean values. However, in C#, the &#38; operator does not use lazy evaluation. One curious thing about C# is that it can use two different operators to calculate an and expression: the &#38; operator and [...]]]></description>
			<content:encoded><![CDATA[<p>Today at work we found a bug. My workmate, not used to C#, usually uses the <i>&amp;</i> operator to compare boolean values. However, in C#, the <i>&amp;</i> operator does not use <a href="http://en.wikipedia.org/wiki/Lazy_evaluation" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Lazy_evaluation?referer=');">lazy evaluation</a>.</p>
<p>One curious thing about C# is that it can use two different operators to calculate an <i>and</i> expression: the <a href="http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/sbf85k1c.aspx?referer=');">&amp;</a> operator and the <a href="http://msdn.microsoft.com/en-us/library/2a723cdk.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/2a723cdk.aspx?referer=');">&amp;&amp;</a> operator. The difference between both is that the first one (<i>&amp;</i>) can be used both with integer types and boolean types. When used with integer types it will perform a bitwise comparison between the two, and when used with boolean values it will use the logical <i>and</i> operation between the two boolean values, <b>evaluating all the parts of the expression</b>. This means that using a code like this one:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>someObject <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;</span> someObject<span style="color: #008000;">.</span><span style="color: #0000FF;">SomeProperty</span> <span style="color: #008000;">==</span> someValue<span style="color: #008000;">&#41;</span></pre></div></div>

<p>will throw a runtime error if <i>someObject</i> is null, because it will try to obtain the <i>SomeProperty</i> value.</p>
<p>However, the <i>&amp;&amp;</i> operator is only available to boolean expressions, and it uses lazy evaluation, this is, if the first condition evaluated is false, it will calculate false without evaluating the rest of the expression, because an <i>and</i> is only true if all the expressions are true.</p>
<p>Conclusion, be sure to always use <i>&#038;amp&amp;</i> when evaluating boolean values if you want to avoid run time surprises <img src='http://thinkingeek.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fthinkingeek.com%2F2009%2F10%2F12%2Fc-the-and-operators-and-lazy-evaluation%2F&amp;title=C%23%2C%20the%20%E2%80%9Cand%E2%80%9D%20operators%20and%20lazy%20evaluation" id="wpa2a_2" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save_url=http_3A_2F_2Fthinkingeek.com_2F2009_2F10_2F12_2Fc-the-and-operators-and-lazy-evaluation_2F_amp_title=C_23_2C_20the_20_E2_80_9Cand_E2_80_9D_20operators_20and_20lazy_20evaluation?referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2009/10/12/c-the-and-operators-and-lazy-evaluation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Full view of ComboBox drop-down list components in C# 3.0</title>
		<link>http://thinkingeek.com/2008/11/18/full-view-of-combobox-drop-down-list-components-in-c-30/</link>
		<comments>http://thinkingeek.com/2008/11/18/full-view-of-combobox-drop-down-list-components-in-c-30/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 12:43:02 +0000</pubDate>
		<dc:creator>alopez</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[programming tips]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=88</guid>
		<description><![CDATA[By default in C# 3.0 ComboBox controls don’t provide support for showing drop-down list items if they exceed the width of their parent ComboBox, like this one: This is annoying because users cannot read properly the information. To solve that problem, all we have to do is derive the ComboBox class and override the DropDown [...]]]></description>
			<content:encoded><![CDATA[<p>By default in C# 3.0 ComboBox controls don’t provide support for showing drop-down list items if they exceed the width of their parent ComboBox, like this one:
</p>
<div id="attachment_89" class="wp-caption aligncenter" style="width: 246px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/image001.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/image001.png" alt="Cropped ComboBox" title="Cropped ComboBox" width="236" height="123" class="size-medium wp-image-89" /></a><p class="wp-caption-text">Cropped ComboBox</p></div>
<p>
This is annoying because users cannot read properly the information. To solve that problem, all we have to do is derive the ComboBox class and override the DropDown event as follows:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ComboBoxEx <span style="color: #008000;">:</span> ComboBox
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> ComboBoxEx<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		DropDown <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span>event_DropDown<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">void</span> event_DropDown<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">try</span>
		<span style="color: #008000;">&#123;</span>
			ComboBox comboBox <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>ComboBox<span style="color: #008000;">&#41;</span>sender<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Catch the combo firing this event</span>
			<span style="color: #6666cc; font-weight: bold;">int</span> width <span style="color: #008000;">=</span> comboBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Current width for ComboBox</span>
			Graphics g <span style="color: #008000;">=</span> comboBox<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateGraphics</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Get graphics for ComboBox</span>
			Font font <span style="color: #008000;">=</span> comboBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Font</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Doesn't change original font</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">//checks if a scrollbar will be displayed.</span>
			<span style="color: #6666cc; font-weight: bold;">int</span> vertScrollBarWidth<span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>comboBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">&gt;</span> comboBox<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxDropDownItems</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#125;</span>
				<span style="color: #008080; font-style: italic;">//If yes, then get its width to adjust the size of the drop down list.</span>
				vertScrollBarWidth <span style="color: #008000;">=</span> SystemInformation<span style="color: #008000;">.</span><span style="color: #0000FF;">VerticalScrollBarWidth</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #0600FF; font-weight: bold;">else</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #008080; font-style: italic;">//Otherwise set to 0</span>
				vertScrollBarWidth <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #008080; font-style: italic;">//Loop through list items and check size of each items.</span>
			<span style="color: #008080; font-style: italic;">//set the width of the drop down list to the width of the largest item.</span>
			<span style="color: #6666cc; font-weight: bold;">int</span> newWidth<span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> s <span style="color: #0600FF; font-weight: bold;">in</span> comboBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>s <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
				<span style="color: #008000;">&#123;</span>
					newWidth <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>g<span style="color: #008000;">.</span><span style="color: #0000FF;">MeasureString</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, font<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">+</span> vertScrollBarWidth<span style="color: #008000;">;</span>
					<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>width <span style="color: #008000;">&lt;</span> newWidth<span style="color: #008000;">&#41;</span>
						width <span style="color: #008000;">=</span> newWidth<span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #008080; font-style: italic;">// Finally, adjust the new width</span>
			comboBox<span style="color: #008000;">.</span><span style="color: #0000FF;">DropDownWidth</span> <span style="color: #008000;">=</span> width<span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#123;</span>  <span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>   
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The following picture shows the results of using the above control instead of the default one:
</p>
<div id="attachment_90" class="wp-caption aligncenter" style="width: 246px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/image003.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/image003.png" alt="Non Cropped ComboBox" title="Non Cropped ComboBox" width="236" height="123" class="size-medium wp-image-90" /></a><p class="wp-caption-text">Non Cropped ComboBox</p></div>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fthinkingeek.com%2F2008%2F11%2F18%2Ffull-view-of-combobox-drop-down-list-components-in-c-30%2F&amp;title=Full%20view%20of%20ComboBox%20drop-down%20list%20components%20in%20C%23%203.0" id="wpa2a_4" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save_url=http_3A_2F_2Fthinkingeek.com_2F2008_2F11_2F18_2Ffull-view-of-combobox-drop-down-list-components-in-c-30_2F_amp_title=Full_20view_20of_20ComboBox_20drop-down_20list_20components_20in_20C_23_203.0?referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2008/11/18/full-view-of-combobox-drop-down-list-components-in-c-30/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# and the StringBuilder class</title>
		<link>http://thinkingeek.com/2008/07/16/c-and-the-stringbuilder-class/</link>
		<comments>http://thinkingeek.com/2008/07/16/c-and-the-stringbuilder-class/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 17:07:37 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming tips]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=67</guid>
		<description><![CDATA[This morning I was working on a project at work. It&#8217;s a Web Application using the ASP .NET 2.0 framework and C# as a code behind language. My friend Ioannis came over to see what was I doing and when he saw I was appending some strings together he asked me this question: &#8220;are you [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I was working on a project at work. It&#8217;s a Web Application using the ASP .NET 2.0 framework and C# as a code behind language. My friend Ioannis came over to see what was I doing and when he saw I was appending some strings together he asked me this question: &#8220;<em>are you using a StringBuilder to use those strings?</em>&#8220;. And I replied with this answer: &#8220;<em>no, I am not</em>&#8220;. This kind of stupid dialog came over because last week we were discussing about using StringBuilders instead of the default String class operators to append strings each other in Java. It seemed using the StringBuilder class resulted in an overall performance gain. It was then when I asked: &#8220;<em>don&#8217;t tell me this happens with C#, too?</em>&#8220;. And he answered: &#8220;<em>yes, it does!</em>&#8220;.</p>
<p>So, what&#8217;s the matter with StringBuilders in C#?</p>
<p><span id="more-67"></span></p>
<p>It seems the same thing happens with Strings, C# and Java. Here is a copy paste from the <a href="http://msdn2.microsoft.com/en-us/library/2839d5h5%28vs.80%29.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn2.microsoft.com/en-us/library/2839d5h5_28vs.80_29.aspx?referer=');">MSDN web page</a> about the StringBuilder usage:</p>
<blockquote><p>
The <strong>String</strong> object is immutable. Every time you use one of the methods in the <strong>System.String</strong> class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new <strong>String</strong> object can be costly. The <a href="http://msdn2.microsoft.com/en-us/library/system.text.stringbuilder%28VS.80%29.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn2.microsoft.com/en-us/library/system.text.stringbuilder_28VS.80_29.aspx?referer=');">System.Text.StringBuilder</a> class can be used when you want to modify a string without creating a new object. For example, using the <strong>StringBuilder</strong> class can boost performance when concatenating many strings together in a loop.
</p></blockquote>
<p>
So I decided to run some tests to see how much it was worth to change my code to use the StringBuilder class, because I use the string &#8216;+&#8217; operator a lot in my program, and the results are simply amazing. Here you can find a chart comparing the times it took to concatenate a given number of strings using both methods. See for yourself and then&#8230; use StringBuilders from now on!
</p>
<div id="attachment_68" class="wp-caption aligncenter" style="width: 310px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/string.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/string-300x180.png" alt="String usage" title="String usage" width="300" height="180" class="size-medium wp-image-68" /></a><p class="wp-caption-text">String usage</p></div>
<div id="attachment_69" class="wp-caption aligncenter" style="width: 310px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/stringbuilder.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/stringbuilder-300x180.png" alt="StringBuilder usage" title="StringBuilder usage" width="300" height="180" class="size-medium wp-image-69" /></a><p class="wp-caption-text">StringBuilder usage</p></div>
<p>
Here&#8217;s the source code i used if you want to try it for yourself:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Adapdev.Diagnostics</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> SBuilder
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">class</span> Program
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            HiPerfTimer timer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HiPerfTimer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                StringBuilder sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                timer<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1000</span> <span style="color: #008000;">&lt;</span>strong<span style="color: #008000;">&gt;</span> i<span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>j<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                timer<span style="color: #008000;">.</span><span style="color: #0000FF;">Stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">double</span> timeStringBuilder <span style="color: #008000;">=</span> timer<span style="color: #008000;">.</span><span style="color: #0000FF;">Duration</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #6666cc; font-weight: bold;">string</span> s <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
                timer<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1000</span> <span style="color: #008000;">&lt;/</span>strong<span style="color: #008000;">&gt;</span> i<span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    s <span style="color: #008000;">+=</span> j<span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                timer<span style="color: #008000;">.</span><span style="color: #0000FF;">Stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">double</span> timeString <span style="color: #008000;">=</span> timer<span style="color: #008000;">.</span><span style="color: #0000FF;">Duration</span><span style="color: #008000;">;</span>
&nbsp;
                StringBuilder line <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                line<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1000</span> <span style="color: #008000;">*</span> i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                line<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                line<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>timeStringBuilder<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                line<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                line<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>timeString<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Console</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>line<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
You can download the external timer classes from <a href="http://www.codeproject.com/csharp/highperformancetimercshar.asp" onclick="pageTracker._trackPageview('/outgoing/www.codeproject.com/csharp/highperformancetimercshar.asp?referer=');">The Code Project</a>.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fthinkingeek.com%2F2008%2F07%2F16%2Fc-and-the-stringbuilder-class%2F&amp;title=C%23%20and%20the%20StringBuilder%20class" id="wpa2a_6" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save_url=http_3A_2F_2Fthinkingeek.com_2F2008_2F07_2F16_2Fc-and-the-stringbuilder-class_2F_amp_title=C_23_20and_20the_20StringBuilder_20class?referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2008/07/16/c-and-the-stringbuilder-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Double Check Design Pattern</title>
		<link>http://thinkingeek.com/2007/07/29/the-double-check-design-pattern/</link>
		<comments>http://thinkingeek.com/2007/07/29/the-double-check-design-pattern/#comments</comments>
		<pubDate>Sun, 29 Jul 2007 17:21:37 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[programming tips]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=74</guid>
		<description><![CDATA[One of the deficiencies of actual programming languages, specially those ones still widely used that are old, such as C or C++, is that they were designed having in mind the sequential programming paradigm. This usually means that those languages don&#8217;t have standard ways to work with multithreading features, and you usually have to rely [...]]]></description>
			<content:encoded><![CDATA[<p>One of the deficiencies of actual programming languages, specially those ones still widely used that are old, such as C or C++, is that they were designed having in mind the sequential programming paradigm. This usually means that those languages don&#8217;t have standard ways to work with multithreading features, and you usually have to rely on third party libraries to develop thread safe software.</p>
<p>Today I&#8217;ll be discussing a design pattern called <em>Double Check</em>, which can be widely used to manage the resource access, elimination and initialization in a safe thread way.</p>
<p><span id="more-74"></span></p>
<p>Consider another common design pattern: the <a href="http://en.wikipedia.org/wiki/Singleton_pattern" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Singleton_pattern?referer=');">Singleton Pattern</a>. A singleton is a special class which is allowed to have one and only one instance in a program. This pattern can be used in a variety of ways, one of the most usual one is to store global program information, which, as its name states, is global and unique to the application, so having more than one instance of that class would have the burden to mantain them synchronized, or even more nasty consequences.</p>
<p>There are different ways to implement a singleton class. I will focus on C#, but the example shown here could be easily extended to other languages such as C++ or Java. The trick on the pattern consists on declaring the class constructor private. This way we can be sure no one will be creating new instances of that class at will. After having done this, we have to provide a public method, which usually has the name getInstance, that will be the one actually creating this item and returning it to the caller.</p>
<p>The code would look similar to this one:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> Singleton
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Singleton instance <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> Singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Singleton Instance
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>instance <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> instance<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
Another way to create this singleton class would be doing the instantiation of the private variable on the class itself and not on the <em>getInstance</em> method, but this second way gives us a <em>lazy initialization</em>, so the variable is actually instantiated only when it&#8217;s needed, and never before (who knows, maybe we will never need it in some circumstances).</p>
<p>This implementation is perfect&#8230; as long as this method isn&#8217;t called twice (or more) at the same time. Consider what would happen if two threads requested an instance of the singleton class: both threads would check for the instance being null, and both threads would create the instance. That would sure lead to an unknown behaviour, and more than one instance of the class would be in the program, which is exactly what we wanted to avoid.</p>
<p>The first solution that comes in mind is to lock the method so multiple threads must wait to enter the method and get the singleton instance. It would look like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> Singleton
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Singleton instance <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">object</span> sempahore <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> Singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Singleton Instance
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">lock</span><span style="color: #008000;">&#40;</span>semaphore<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>instance <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> instance<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
This way we ensure that if n threads try to access the routine at the same time, only one will be initializing the singleton instance, since when the others gain the access to the method again, that instance won&#8217;t be null anymore. However, even if this solution is valid and works, it has a serious performance problem. Consider the situation where the instance has been already created. Then more than one thread access the routine again to get that instance. What will happen is that those threads will have to block and wait for other threads to finish, even if the instance is not null, so in any case will be created again. Conclusion: we are blocking threads that don&#8217;t need to be blocked. &#8220;<em>Ok, you may say, we will put the lock inside the if clause, and we&#8217;ll have the issue resolved</em>&#8220;. Let&#8217;s take a look at this solution:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> Singleton
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Singleton instance <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">object</span> sempahore <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> Singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Singleton Instance
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>instance <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">lock</span><span style="color: #008000;">&#40;</span>semaphore<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> instance<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
You don&#8217;t have to be a genious to see that this is not working, either. It will work after the instance has been created, because the threads won&#8217;t be entering the if clause, and they won&#8217;t be blocked. But if we are in the same situation as the described in the first place &#8211;this is, more than one thread entering the method before the instance has been created&#8211;, we have the same problem we had before: the singleton will be created more than once.</p>
<p>
The final solution is simple: check again for the variable being null! This is why this pattern is called <em>Double Check</em>. The final code would look like this one:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> Singleton
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Singleton instance <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">object</span> sempahore <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> Singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Singleton Instance
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>instance <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">lock</span><span style="color: #008000;">&#40;</span>semaphore<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>instance <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                    instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> instance<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
This way, we allow the threads to pass by the if clause if the instance has been already instantiated, and if it has not, the threads will get blocked, and the first one to gain access to the method will do the instantiation. Further threads will check again against the instance being null or not, so it won&#8217;t be created again.
</p>
<p>
Altough this pattern may be implemented similarly in other languages, be aware that, as I said before, a lot of them were never intended to deal with multithreading, and that compiler optimizations may give you code which is not really thread safe, or that actually is not optimized. Here you have a link discussing this issue with Java and C++: <a href="http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html" onclick="pageTracker._trackPageview('/outgoing/www.cs.umd.edu/_pugh/java/memoryModel/DoubleCheckedLocking.html?referer=');">http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html</a>.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fthinkingeek.com%2F2007%2F07%2F29%2Fthe-double-check-design-pattern%2F&amp;title=The%20Double%20Check%20Design%20Pattern" id="wpa2a_8" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save_url=http_3A_2F_2Fthinkingeek.com_2F2007_2F07_2F29_2Fthe-double-check-design-pattern_2F_amp_title=The_20Double_20Check_20Design_20Pattern?referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2007/07/29/the-double-check-design-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Quiz</title>
		<link>http://thinkingeek.com/2007/07/27/ruby-quiz/</link>
		<comments>http://thinkingeek.com/2007/07/27/ruby-quiz/#comments</comments>
		<pubDate>Fri, 27 Jul 2007 17:17:41 +0000</pubDate>
		<dc:creator>jsegura</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[programming tips]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=71</guid>
		<description><![CDATA[Maybe one of the best ways to learn a new programming language is playing with it. Nowadays if you don&#8217;t code in Ruby you aren&#8217;t cool. I have to recognize that programming in Ruby is funnier than in other language and for the moment I don&#8217;t have anything bad to say about it. Well, I [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe one of the best ways to learn a new programming language is playing with it. Nowadays if you don&#8217;t code in Ruby you aren&#8217;t cool. I have to recognize that programming in Ruby is funnier than in other language and for the moment I don&#8217;t have anything bad to say about it.</p>
<p>Well, I want to introduce <a href="http://www.rubyquiz.com/" onclick="pageTracker._trackPageview('/outgoing/www.rubyquiz.com/?referer=');">Ruby Quiz</a>. It is a collection of minigames prepared to learn Ruby (or to improve your skills). Every week they publish one game, and if you are brave you can send your solution to them and it will be public. I think it&#8217;s one of the best ways to learn because you can compare solutions and find were you are weak in Ruby and redo your solution doing it smarter.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fthinkingeek.com%2F2007%2F07%2F27%2Fruby-quiz%2F&amp;title=Ruby%20Quiz" id="wpa2a_10" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save_url=http_3A_2F_2Fthinkingeek.com_2F2007_2F07_2F27_2Fruby-quiz_2F_amp_title=Ruby_20Quiz?referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2007/07/27/ruby-quiz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Customizing IRB</title>
		<link>http://thinkingeek.com/2007/06/27/customizing-irb/</link>
		<comments>http://thinkingeek.com/2007/06/27/customizing-irb/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 16:40:49 +0000</pubDate>
		<dc:creator>jsegura</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[programming tips]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=49</guid>
		<description><![CDATA[While developing a Ruby application or while learning ruby, one of the things you must use is IRB (interactive ruby). As in its man page is said &#8220;irb is a tool to execute interactively ruby expressions read from stdin.&#8221;. In this tool you can type and execute directly ruby code. It&#8217;s very useful but like [...]]]></description>
			<content:encoded><![CDATA[<p>While developing a Ruby application or while learning ruby, one of the things you must use is IRB (interactive ruby). As in its man page is said &#8220;irb is a tool to execute interactively ruby expressions read from stdin.&#8221;. In this tool you can type and execute directly ruby code. It&#8217;s very useful but like most other programs like <a href="http://www.vim.org/" onclick="pageTracker._trackPageview('/outgoing/www.vim.org/?referer=');">ViM</a> (Vi IMproved) the real power is its customization.</p>
<p>Here I post my .irbrc and to make things clear there are some explanations on each line.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># autocompletion of methods when pressing TAB</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'irb/completion'</span>
<span style="color:#008000; font-style:italic;"># Wirble is a plugin to colorize your irb, it's installed from a gem (gem install -y wirble)</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'wirble'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Make use of readline library</span>
ARGV.<span style="color:#9900CC;">concat</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#996600;">&quot;--readline&quot;</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># autoindent of code while typing it</span>
IRB.<span style="color:#9900CC;">conf</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:AUTO_INDENT</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># wirble initializations</span>
Wirble.<span style="color:#9900CC;">init</span>
Wirble.<span style="color:#9900CC;">colorize</span></pre></div></div>

<p>
As I said before, IRB is very powerful and a proof is that in <a href="http://www.ruby-lang.org/" onclick="pageTracker._trackPageview('/outgoing/www.ruby-lang.org/?referer=');">Ruby Lang</a> they encourage you to <a href="http://tryruby.hobix.com/" onclick="pageTracker._trackPageview('/outgoing/tryruby.hobix.com/?referer=');">try ruby in your browser</a> with an embedded IRB.
</p>
<p>
Also in rails the console for debugging your application is an irb instance preloaded with all rails configuration. In <a href="http://www.railscasts.com/" onclick="pageTracker._trackPageview('/outgoing/www.railscasts.com/?referer=');">RailsCasts</a> there is a <a href="http://railscasts.com/episodes/48" onclick="pageTracker._trackPageview('/outgoing/railscasts.com/episodes/48?referer=');">screencast</a> that shows you some tricks about it.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fthinkingeek.com%2F2007%2F06%2F27%2Fcustomizing-irb%2F&amp;title=Customizing%20IRB" id="wpa2a_12" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save_url=http_3A_2F_2Fthinkingeek.com_2F2007_2F06_2F27_2Fcustomizing-irb_2F_amp_title=Customizing_20IRB?referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2007/06/27/customizing-irb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Code Snippets in Visual Studio 2005</title>
		<link>http://thinkingeek.com/2007/06/15/code-snippets-in-visual-studio-2005/</link>
		<comments>http://thinkingeek.com/2007/06/15/code-snippets-in-visual-studio-2005/#comments</comments>
		<pubDate>Fri, 15 Jun 2007 16:52:00 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[programming tips]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=19</guid>
		<description><![CDATA[In the developing process of applications that are not as small as the typical &#8220;Hello, World!&#8221; examples, there are a variety of factors than can lead to important time savings. There&#8217;s a lot of documentation out there on how to design and specify application before the coding process starts, but there is a crucial factor [...]]]></description>
			<content:encoded><![CDATA[<p>In the developing process of applications that are not as small as the typical &#8220;Hello, World!&#8221; examples, there are a variety of factors than can lead to important time savings.</p>
<p>There&#8217;s a lot of documentation out there on how to design and specify application before the coding process starts, but there is a crucial factor on success that is not usually spoken of: the way you manage, create and edit your source files.</p>
<p>And of course there are some beautiful software pieces to help developers in that process. They&#8217;re called IDEs (Integrated Development Environment).</p>
<p>The problem with most of those IDEs is that they offer so many options that you usually have to read a user&#8217;s manual to really take the best from them. Ok, this is something normal, you might say. Maybe you&#8217;re right, but be honest, how many software user guides have you read in your life? And I&#8217;m not talking about the usual RTFM for a linux man page which can be 4 pages long at most. I&#8217;m talking about a user&#8217;s manual of 500 pages. I haven&#8217;t.</p>
<p>And that&#8217;s the reason today I&#8217;ll be talking about a nice feature I found on one of the most powerful IDEs out there (regardless of being from Microsoft): Visual Studio 2005 Code Snippets.</p>
<p><span id="more-19"></span></p>
<p>Code Snippets are more than just snippets of code, to start with. It&#8217;s not a simple code listing to quickly paste into your files, because Code Snippets are smart and adaptative, this is, they can be parametrized. Let&#8217;s see this in a simple example.</p>
<p>Imagine you want to code a for loop. All those kind of code structures behave the same way: there&#8217;s an initial condition, an ending condition, a condition modifier so you eventually leave the bucle, and of course the body, the code you want to be executed every time the bucle takes place. And it&#8217;s not unusual to use an integer variable to manage the counter which controls how many times that piece of code will be executed, for example the index of an array and the size of that array. You would code something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">int</span> length <span style="color: #008000;">=</span> someArray<span style="color: #008000;">.</span><span style="color: #0000FF;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> length<span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    doSomething<span style="color: #008000;">&#40;</span>array<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
Ok, this is a shot piece of code and it takes about 10 seconds to write it, but it would be better if some IDE feature could help us. This is where Code Snippets come into action. Let&#8217;s do this exact thing using Code Snippiets.
</p>
<ol>
<li>Go to the part of the code you want to place the bucle in and press the right mouse button to select the <em>Insert Snippet</em> option. </li>
<li>Select the <em>for</em> on the list box.</li>
<li>You&#8217;ll see the basic structure of the code has already been typed for you.</li>
</ol>
<p><div id="attachment_14" class="wp-caption aligncenter" style="width: 160px"><a href="http://thinkingeek.com/wp-content/uploads/2008/10/snippets1.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/10/snippets1-150x150.png" alt="Inserting a snippet" title="snippets1" width="150" height="150" class="size-thumbnail wp-image-14" /></a><p class="wp-caption-text">Inserting a snippet</p></div><br />
<div id="attachment_15" class="wp-caption aligncenter" style="width: 160px"><a href="http://thinkingeek.com/wp-content/uploads/2008/10/snippets2.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/10/snippets2-150x150.png" alt="Inserting a snippet (2)" title="snippets2" width="150" height="150" class="size-thumbnail wp-image-15" /></a><p class="wp-caption-text">Inserting a snippet (2)</p></div><br />
<div id="attachment_16" class="wp-caption aligncenter" style="width: 160px"><a href="http://thinkingeek.com/wp-content/uploads/2008/10/snippets3.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/10/snippets3-150x104.png" alt="Inserting a snippet (3)" title="snippets3" width="150" height="104" class="size-thumbnail wp-image-16" /></a><p class="wp-caption-text">Inserting a snippet (3)</p></div></p>
<p>But this is not the end of the story!</p>
<p>With the tab key you can now navigate through the index variable and the ending condition of the bucle to type whatever you want there. Pressing enter will end the snippet wizard, and voilá, you have your for bucle coded, and only the body needs to be filled.</p>
<p>This example is kind of basic and you probably could code this far quicker by hand than by having to press mouse buttons and cursor arrows. You may be right, but as you may have already seen, there are plenty of snippets than can help you really improve your coding speed for all those &#8220;always the same&#8221; code structures. One good example of it is the property definitions on a C# class.</p>
<p>But code snippets not only can add code to a blank line, they can also sorround existing code to add features on it. Imagine you &#8216;ve started coding a method and you realize later that there is some kind of exception you should be dealing with. You know what that means, going to the upper part of the code, typing the try {, going down, close the bracket, and probably indenting the bracketed code to have a pretty printed code. Not to mention you should probably have to add a catch or finally clause after, too. This is work that can be tedious and it is not done in 10 seconds as a for bucle. Code Snippets can help us here too.</p>
<ol>
<li>Select the code you want to sorround with a <em>try/finally</em> clause.</li>
<li>Click the right mouse button and select <em>Sorround With</em>.</li>
<li>Select the <em>tryf</em> snippet.</li>
</ol>
<p><div id="attachment_17" class="wp-caption aligncenter" style="width: 160px"><a href="http://thinkingeek.com/wp-content/uploads/2008/10/snippets4.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/10/snippets4-150x150.png" alt="Sorrounding with snippets" title="snippets4" width="150" height="150" class="size-thumbnail wp-image-17" /></a><p class="wp-caption-text">Sorrounding with snippets</p></div><br />
<div id="attachment_18" class="wp-caption aligncenter" style="width: 160px"><a href="http://thinkingeek.com/wp-content/uploads/2008/10/snippets5.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/10/snippets5-150x150.png" alt="Sorrounding with snippets (2)" title="snippets5" width="150" height="150" class="size-thumbnail wp-image-18" /></a><p class="wp-caption-text">Sorrounding with snippets (2)</p></div><br />
<div id="attachment_13" class="wp-caption aligncenter" style="width: 160px"><a href="http://thinkingeek.com/wp-content/uploads/2008/10/snippets6.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/10/snippets6-150x150.png" alt="Sorrounding with snippets (3)" title="snippets6" width="150" height="150" class="size-thumbnail wp-image-13" /></a><p class="wp-caption-text">Sorrounding with snippets (3)</p></div></p>
<p>One of the most interesting things about Code Snippets is that you can create your own for repetitive or tedious tasks you use to code. Let&#8217;s see how to proceed to create a custom Code Snippet.</p>
<p>To begin with, a Code Snippet is an XML file. It&#8217;s basic structure is the one following:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;codesnippets</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;codesnippet</span> <span style="color: #000066;">format</span>=<span style="color: #ff0000;">&quot;1.0.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;header<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/header<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;snippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;code</span> <span style="color: #000066;">language</span>=<span style="color: #ff0000;">&quot;CSharp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #808080; font-style: italic;">&lt;!--[CDATA[MessageBox.Show(&quot;Hello World&quot;);]]--&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/code<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/snippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/codesnippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/codesnippets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The element names are pretty self explanatory. You can customize the title of the snippet, and put the static code in a <em>CDATA </em>element, previously defining the language you&#8217;ll be using.</p>
<p>You can add <em>Literals </em>and <em>Objects </em>to the snippet code too. <em>Literals </em>are pieces of the snippet the programmer will usually want to replace, such as variable names and similar items. <em>Objects </em>are elements that the snippet needs but they usually are objects previously defined in your code that you want the snippet to use, such as specific objects to work with. You&#8217;ll understand this better with an example.</p>
<p>Let&#8217;s suppose you want to create a code snippet that checks a specific web request variable to validate against a custom SQL Injection class you&#8217;ve programmed. For simplicity&#8217;s sake we&#8217;ll suppose the validation class is already programmed and has a method that does this security feature. In this case, the literal could be the variable name of the web request, and the object could be the SQL Validator class instance you&#8217;ve previously defined in your code.</p>
<p>Both literals and objects are defined inside the Declarations node in the snippet XML as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;literal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>requestName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tooltip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Replace with the variable name of the HTTP Request<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tooltip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>action<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/literal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SqlValidator<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MyProject.Data.Validation.SQLValidator<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tooltip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Replace with a SQLValidator object in your application.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tooltip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>sqlValidator<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>As you can see, you can define an identifier, a tooltip and a default value, and in the Object element the class of the object.</p>
<p>Now the only thing you have to do is replace in your code snippet the pieces of code where you want those literals and objects to appear like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;code</span> <span style="color: #000066;">language</span>=<span style="color: #ff0000;">&quot;CSharp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #808080; font-style: italic;">&lt;!--[CDATA[</span>
<span style="color: #808080; font-style: italic;">            string variableRequested = Request[&quot;$requestName$&quot;];</span>
<span style="color: #808080; font-style: italic;">            if (!$SQLValidator$.Validate(variableRequested))</span>
<span style="color: #808080; font-style: italic;">            //Insert error handling here</span>
<span style="color: #808080; font-style: italic;">        --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/code<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So, the final snippet would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;codesnippets</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;codesnippet</span> <span style="color: #000066;">format</span>=<span style="color: #ff0000;">&quot;1.0.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;header<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/header<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;snippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;literal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>requestName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tooltip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Replace with the variable name of the HTTP Request<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tooltip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>action<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/literal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SqlValidator<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MyProject.Data.Validation.SQLValidator<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tooltip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Replace with a SQLValidator object in your application.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tooltip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>sqlValidator<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;code</span> <span style="color: #000066;">language</span>=<span style="color: #ff0000;">&quot;CSharp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #808080; font-style: italic;">&lt;!--[CDATA[</span>
<span style="color: #808080; font-style: italic;">                    string variableRequested = Request[&quot;$requestName$&quot;];</span>
<span style="color: #808080; font-style: italic;">                    if (!$SQLValidator$.Validate(variableRequested))</span>
<span style="color: #808080; font-style: italic;">                    //Insert error handling here</span>
<span style="color: #808080; font-style: italic;">                    --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/code<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/snippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/codesnippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/codesnippets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now you just have to copy this snippet in your custom snippet folder (usually in Visual Studio 2005 folder created on My Documents) and you&#8217;ll be able to use it from the Visual Studio menu as any other one, or use the Code Snipper Manager on the Visual Studio Tools menu.</p>
<p>For more information about Code Snippets you can visit the MSDN page <a href="http://msdn2.microsoft.com/en-us/library/ms165393(VS.80).aspx" onclick="pageTracker._trackPageview('/outgoing/msdn2.microsoft.com/en-us/library/ms165393_VS.80_.aspx?referer=');">http://msdn2.microsoft.com/en-us/library/ms165393(VS.80).aspx</a>.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fthinkingeek.com%2F2007%2F06%2F15%2Fcode-snippets-in-visual-studio-2005%2F&amp;title=Code%20Snippets%20in%20Visual%20Studio%202005" id="wpa2a_14" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save_url=http_3A_2F_2Fthinkingeek.com_2F2007_2F06_2F15_2Fcode-snippets-in-visual-studio-2005_2F_amp_title=Code_20Snippets_20in_20Visual_20Studio_202005?referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2007/06/15/code-snippets-in-visual-studio-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

