<?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>Holsee&#039;s Blog &#187; Rant</title>
	<atom:link href="http://www.blog.holsee.com/tag/rant/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blog.holsee.com</link>
	<description>Adventures in Entrepreneurship, Code, Coffee and Photography..</description>
	<lastBuildDate>Fri, 02 Jul 2010 10:48:38 +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>C# private fields to &#8216;_&#8217; or not to &#8216;_&#8217; that is the Question</title>
		<link>http://www.blog.holsee.com/2009/11/c-private-fields-to-_-or-not-to-_-that-is-the-question/</link>
		<comments>http://www.blog.holsee.com/2009/11/c-private-fields-to-_-or-not-to-_-that-is-the-question/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 17:21:16 +0000</pubDate>
		<dc:creator>holsee</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Rant]]></category>

		<guid isPermaLink="false">http://www.blog.holsee.com/2009/11/c-private-fields-to-_-or-not-to-_-that-is-the-question/</guid>
		<description><![CDATA[I’m getting sick of the argument:
&#34;Should private fields in a class be prefixed with an ‘_’?&#34; 
So I&#8217;m going to try and crack this nut once and for all!
Example:

private double _price;	//Using “_”
&#160;
public double Price
&#123;
    get &#123; return _price; &#125;
    private set &#123; _price = value; &#125;
&#125;


private double price; 	//Not [...]]]></description>
			<content:encoded><![CDATA[<p>I’m getting sick of the argument:</p>
<p><em>&quot;Should private fields in a class be prefixed with an ‘_’?&quot; </em></p>
<p>So I&#8217;m going to try and crack this nut once and for all!</p>
<p><strong>Example:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">double</span> _price<span style="color: #008000;">;</span>	<span style="color: #008080; font-style: italic;">//Using “_”</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">double</span> Price
<span style="color: #000000;">&#123;</span>
    get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _price<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">private</span> set <span style="color: #000000;">&#123;</span> _price <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">double</span> price<span style="color: #008000;">;</span> 	<span style="color: #008080; font-style: italic;">//Not using “_”</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">double</span> Price
<span style="color: #000000;">&#123;</span>
    get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> price<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">private</span> set <span style="color: #000000;">&#123;</span> price <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Note:&#160; I am well aware I could use an automatic property here, but so not the point =].</p>
<p>So what do Microsoft say? Well <a href="http://msdn.microsoft.com/en-us/library/xzf533w0%28VS.71%29.aspx" target="_blank">here</a> is their Naming Convention section of MSDN.&#160; They don’t want to touch this topic it seems as there is no guidance on how to name a private field.&#160; When they discuss property naming conventions <a href="http://msdn.microsoft.com/en-us/library/fzcth91k%28VS.71%29.aspx" target="_blank">here</a> this is all we get:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Color
<span style="color: #000000;">&#123;</span>
	get <span style="color: #000000;">&#123;</span><span style="color: #008080; font-style: italic;">// Insert code here.}</span>
	set <span style="color: #000000;">&#123;</span><span style="color: #008080; font-style: italic;">// Insert code here.}</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Hmm that was helpful =/.</p>
<p>I got my hands on the <a href="http://www.idesign.net/idesign/DesktopDefault.aspx" target="_blank">iDesign</a> C# coding conventions which you can download from <a href="http://www.idesign.net/idesign/download/IDesign%20CSharp%20Coding%20Standard.zip" target="_blank">here</a>.</p>
<p>They go down the road of prefixing private fields with “m_”&#160; D= ! That is a barrel of worms right there so I’m going to leave it at that! </p>
<p><strong>Arguments AGAINST prefixing with an underscore:</strong></p>
<ol>
<li>In intellisense the property of said name and its backing field will appear together.<a href="http://www.blog.holsee.com/wp-content/uploads/2009/11/clip_image0018.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="clip_image001[8]" border="0" alt="clip_image001[8]" src="http://www.blog.holsee.com/wp-content/uploads/2009/11/clip_image0018_thumb.png" width="149" height="90" /></a> </li>
<li>C# is case sensitive therefore there seems to be no need for an ‘_’. </li>
<li>If you wish to differentiate between fields from your local variables / parameters use the “this” keyword to indicate a field from a local variable like so:

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> WithDiscount<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> price<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">price</span> <span style="color: #008000;">=</span> price<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</li>
<li>“StyleCop will call you a son of a whore if you use underscores” <a href="http://twitter.com/blowdart" target="_blank">@blowdart</a> </li>
</ol>
<p><em>If you can think of anymore please leave a comment.</em></p>
<p><strong>Arguments FOR prefixing private fields with an underscore:</strong></p>
<ol>
<li>When I got my C# Certification I was taught to use an &quot;_&quot;. </li>
<li>You do not need to put “this” in your code before private fields. </li>
<li>If you don’t wish to use “this” everywhere you have to get creative with naming. </li>
<li>You don’t need to think about what a variable is, “_” prefix makes it quite clear. </li>
<li>All private fields with be grouped together in intellisense:</li>
<p><a href="http://www.blog.holsee.com/wp-content/uploads/2009/11/clip_image001.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="clip_image001" border="0" alt="clip_image001" src="http://www.blog.holsee.com/wp-content/uploads/2009/11/clip_image001_thumb.png" width="169" height="107" /></a></p>
<li>“..it allows for the same naming convention to span C# and VB.NET” <a href="http://twitter.com/csharphacker" target="_blank">@csharphacker</a></li>
<li>R# by default says so! </li>
<li>I said so &gt;.&lt;! </li>
</ol>
<p><em>Again If you can think of anymore please leave a comment.</em></p>
<p><em><strong>Conclusion</strong></em></p>
<p><em>What sells the “_” for me is the fact I don&#8217;t have to put this everywhere in my code&#160; when working with private variables nor do I have to think about alternate naming for local variables or parameters to avoid using this.</em></p>
<p><em>What is you opinion?&#160; Let the battle rage on..!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.holsee.com/2009/11/c-private-fields-to-_-or-not-to-_-that-is-the-question/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
