<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Prefixing Instance Fields?</title>
	<atom:link href="http://davybrion.com/blog/2009/10/prefixing-instance-fields/feed/" rel="self" type="application/rss+xml" />
	<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/</link>
	<description>Trying to walk that thin line between intelligence and ignorance</description>
	<lastBuildDate>Thu, 11 Mar 2010 06:13:45 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Arjay</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22985</link>
		<dc:creator>Arjay</dc:creator>
		<pubDate>Tue, 17 Nov 2009 22:54:22 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22985</guid>
		<description>It&#039;s always bugged me about VB (not that I&#039;ve coded much in VB) with regard to its me. convention.  The field (or member) of a class is part of the class instance so I&#039;ve always found using me. or this. redundant.  I prefix private fields with underscores so I can spot them in the ide and on printed paper.

I also declare them at the bottom of the class declaration, but that&#039;s another story.</description>
		<content:encoded><![CDATA[<p>It&#8217;s always bugged me about VB (not that I&#8217;ve coded much in VB) with regard to its me. convention.  The field (or member) of a class is part of the class instance so I&#8217;ve always found using me. or this. redundant.  I prefix private fields with underscores so I can spot them in the ide and on printed paper.</p>
<p>I also declare them at the bottom of the class declaration, but that&#8217;s another story.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Chiasson</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22982</link>
		<dc:creator>Brian Chiasson</dc:creator>
		<pubDate>Tue, 17 Nov 2009 13:51:04 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22982</guid>
		<description>Conventions are the biggest reason for using an underscore, I have no problems with it. The other reason that I use underscore is for CLS compliance. My project (unfortunately) has libraries written in both C# and VB.NET, and field names that only differ in case to property names are not CLS compliant.</description>
		<content:encoded><![CDATA[<p>Conventions are the biggest reason for using an underscore, I have no problems with it. The other reason that I use underscore is for CLS compliance. My project (unfortunately) has libraries written in both C# and VB.NET, and field names that only differ in case to property names are not CLS compliant.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Julian Birch</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22916</link>
		<dc:creator>Julian Birch</dc:creator>
		<pubDate>Tue, 10 Nov 2009 13:46:10 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22916</guid>
		<description>I think this is a lot more to do with tooling than people realize.  In the days that hungarian notation was favoured, figuring out the type of a variable was often painful, especially when using the windows API.  Equally, these days, there are two things that encourage people to use underscores:

1) Instance fields don&#039;t get coloured differently from local variables in the average IDE.
2) Putting an underscore in front groups them together in class browsers.

It wouldn&#039;t be hard to improve the tooling in these areas, of course...</description>
		<content:encoded><![CDATA[<p>I think this is a lot more to do with tooling than people realize.  In the days that hungarian notation was favoured, figuring out the type of a variable was often painful, especially when using the windows API.  Equally, these days, there are two things that encourage people to use underscores:</p>
<p>1) Instance fields don&#8217;t get coloured differently from local variables in the average IDE.<br />
2) Putting an underscore in front groups them together in class browsers.</p>
<p>It wouldn&#8217;t be hard to improve the tooling in these areas, of course&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arjan&#8217;s World &#187; LINKBLOG for Oct 27, 2009</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22826</link>
		<dc:creator>Arjan&#8217;s World &#187; LINKBLOG for Oct 27, 2009</dc:creator>
		<pubDate>Tue, 27 Oct 2009 16:14:44 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22826</guid>
		<description>[...] Prefixing Instance Fields? &#8211; Davy Brion [...]</description>
		<content:encoded><![CDATA[<p>[...] Prefixing Instance Fields? &#8211; Davy Brion [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabriel Lozano-Moran</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22825</link>
		<dc:creator>Gabriel Lozano-Moran</dc:creator>
		<pubDate>Tue, 27 Oct 2009 07:52:49 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22825</guid>
		<description>It depends on what the accessbility of the field is. If we are talking about private fields it does not matter what you use, as long as your entire team uses the same convention. If we are talking about a protected field, according to the Framework Design Guidelines, they should be PascalCased (upper camel case). Public fields are bad IMHO, it&#039;s like a cop querying your brain for your name and address instead of asking you to show him your ID.

@Wayne: a better naming for your CountIsValid() method would be to start with Is as in IsValidCount(), see also the Framework Design Guidelines or on MSDN Library.

Also like Davy mentions there are several reasons why you should not use complex properties and it is because it is possible that consumers of your API will use the property in a for-loop for example without caching the returned value of a property in a local variable first:

for (var i = 0; i &lt; someClass.Count; i++)
{
    Foo(someClass.Count);
}

Because you dont expect too much complexity in the getter for Count property. But when a developer sees that it is a method they tend to cache it in a variable first:

for (var i = 0; i &lt; someClass.GetCount(); i++)
{
    Foo(someClass.GetCount());
}

They will probabyl write this:

var count = someClass.GetCount();
for (var i = 0; i &lt; count; i++)
{
    Foo(count);
}

I think it has to do with psychology :-)</description>
		<content:encoded><![CDATA[<p>It depends on what the accessbility of the field is. If we are talking about private fields it does not matter what you use, as long as your entire team uses the same convention. If we are talking about a protected field, according to the Framework Design Guidelines, they should be PascalCased (upper camel case). Public fields are bad IMHO, it&#8217;s like a cop querying your brain for your name and address instead of asking you to show him your ID.</p>
<p>@Wayne: a better naming for your CountIsValid() method would be to start with Is as in IsValidCount(), see also the Framework Design Guidelines or on MSDN Library.</p>
<p>Also like Davy mentions there are several reasons why you should not use complex properties and it is because it is possible that consumers of your API will use the property in a for-loop for example without caching the returned value of a property in a local variable first:</p>
<p>for (var i = 0; i &lt; someClass.Count; i++)<br />
{<br />
    Foo(someClass.Count);<br />
}</p>
<p>Because you dont expect too much complexity in the getter for Count property. But when a developer sees that it is a method they tend to cache it in a variable first:</p>
<p>for (var i = 0; i &lt; someClass.GetCount(); i++)<br />
{<br />
    Foo(someClass.GetCount());<br />
}</p>
<p>They will probabyl write this:</p>
<p>var count = someClass.GetCount();<br />
for (var i = 0; i &lt; count; i++)<br />
{<br />
    Foo(count);<br />
}</p>
<p>I think it has to do with psychology <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wayne</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22823</link>
		<dc:creator>wayne</dc:creator>
		<pubDate>Tue, 27 Oct 2009 02:10:54 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22823</guid>
		<description>Something related that I have pused with MS only to receive &quot;we could but, ...&quot; is property scopped fields i.e.

public int Count
{
    int count = 0;
    get { return count; }
    set { if (CountIsValid(value)) {count = value;} else {throw new InvalidArgumentException();} } 
}

This would remove the need to most of the fields in most classes</description>
		<content:encoded><![CDATA[<p>Something related that I have pused with MS only to receive &#8220;we could but, &#8230;&#8221; is property scopped fields i.e.</p>
<p>public int Count<br />
{<br />
    int count = 0;<br />
    get { return count; }<br />
    set { if (CountIsValid(value)) {count = value;} else {throw new InvalidArgumentException();} }<br />
}</p>
<p>This would remove the need to most of the fields in most classes</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tcmaster</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22822</link>
		<dc:creator>tcmaster</dc:creator>
		<pubDate>Mon, 26 Oct 2009 21:48:17 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22822</guid>
		<description>I use underscore, ._. and never this. While I hate to put unnecessary fields to class (they make &quot;class-wide-global-variables&quot;, and make class logic hard to understand), I do use underscore to keep me focused on code logic by know which is what.</description>
		<content:encoded><![CDATA[<p>I use underscore, ._. and never this. While I hate to put unnecessary fields to class (they make &#8220;class-wide-global-variables&#8221;, and make class logic hard to understand), I do use underscore to keep me focused on code logic by know which is what.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy Gray</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22821</link>
		<dc:creator>Jeremy Gray</dc:creator>
		<pubDate>Mon, 26 Oct 2009 20:00:28 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22821</guid>
		<description>@Dennis - While I agree with your thoughts about &quot;this&quot;, and also give a +1 for the rest of Davy&#039;s post, here&#039;s why you are wrong about the first paragraph of your comment:

Resharper(Ctrl+B) ==&gt; requires an extra click to see information that could be there at all times. Not everyone installs R#. Navigating back is an extra step. Depending on where you navigated to, navigating back can prove problematic given VS behaviour.
VS(goto definition) ==&gt; requires an extra click to see information that could be there at all times. Slow. Navigating back is an extra step. Depending on where you navigated to, navigating back can prove problematic given VS behaviour.
Reflector(click on the variable) ==&gt; requires an extra click to see information that could be there at all times. Reflecter is way too late. This insight is needed when working with source, not when reverse engineering compiled assemblies.

+1 for _. All the benefit of &quot;this.&quot;, but with less typing. None of the overhead of going hunting for information interactively when it could just _be there_.</description>
		<content:encoded><![CDATA[<p>@Dennis &#8211; While I agree with your thoughts about &#8220;this&#8221;, and also give a +1 for the rest of Davy&#8217;s post, here&#8217;s why you are wrong about the first paragraph of your comment:</p>
<p>Resharper(Ctrl+B) ==&gt; requires an extra click to see information that could be there at all times. Not everyone installs R#. Navigating back is an extra step. Depending on where you navigated to, navigating back can prove problematic given VS behaviour.<br />
VS(goto definition) ==&gt; requires an extra click to see information that could be there at all times. Slow. Navigating back is an extra step. Depending on where you navigated to, navigating back can prove problematic given VS behaviour.<br />
Reflector(click on the variable) ==&gt; requires an extra click to see information that could be there at all times. Reflecter is way too late. This insight is needed when working with source, not when reverse engineering compiled assemblies.</p>
<p>+1 for _. All the benefit of &#8220;this.&#8221;, but with less typing. None of the overhead of going hunting for information interactively when it could just _be there_.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sneal</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22820</link>
		<dc:creator>Sneal</dc:creator>
		<pubDate>Mon, 26 Oct 2009 15:47:33 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22820</guid>
		<description>Using fields for passing data between methods can be a code smell, if over used. Done properly, the code can become so much more readable. If you find only a few methods using the same instance variable perhaps an extract class refactoring is an order? 

The naming thing doesn&#039;t really matter, as long as you&#039;re consistent.</description>
		<content:encoded><![CDATA[<p>Using fields for passing data between methods can be a code smell, if over used. Done properly, the code can become so much more readable. If you find only a few methods using the same instance variable perhaps an extract class refactoring is an order? </p>
<p>The naming thing doesn&#8217;t really matter, as long as you&#8217;re consistent.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dennis</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22819</link>
		<dc:creator>Dennis</dc:creator>
		<pubDate>Mon, 26 Oct 2009 14:51:48 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22819</guid>
		<description>I detest warts! Prefixing with underscores(warts) adds little to no value to helping a developer understand the code. If I really want to know the scope of the variable, Resharper(Ctrl+B), VS(goto definition) and Reflector(click on the variable) all make the experience easy enough. Remembering to type &quot;_&quot; is just as bad as typing &quot;this.&quot;. 

+1 for the 2 general practices about using fields.

The only place I use warts is writing BDD specifications. Mostly because the spec runners tools &quot;can&quot; generate a friendly version of the specifications.</description>
		<content:encoded><![CDATA[<p>I detest warts! Prefixing with underscores(warts) adds little to no value to helping a developer understand the code. If I really want to know the scope of the variable, Resharper(Ctrl+B), VS(goto definition) and Reflector(click on the variable) all make the experience easy enough. Remembering to type &#8220;_&#8221; is just as bad as typing &#8220;this.&#8221;. </p>
<p>+1 for the 2 general practices about using fields.</p>
<p>The only place I use warts is writing BDD specifications. Mostly because the spec runners tools &#8220;can&#8221; generate a friendly version of the specifications.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Think Before Coding</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22818</link>
		<dc:creator>Think Before Coding</dc:creator>
		<pubDate>Mon, 26 Oct 2009 13:57:30 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22818</guid>
		<description>I don&#039;t use underscores. With short methods, the intent is always clear, and resharper usualy help to spot the rare cases where you mix up. In this case, I try to find better names so that there is no more ambiguity.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t use underscores. With short methods, the intent is always clear, and resharper usualy help to spot the rare cases where you mix up. In this case, I try to find better names so that there is no more ambiguity.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AllanC</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22817</link>
		<dc:creator>AllanC</dc:creator>
		<pubDate>Mon, 26 Oct 2009 13:39:03 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22817</guid>
		<description>It has taken me many years to get to the style I use. I have been programming in OO since around 1990. I agree with the two recommendations in your blog, but I see them as a best practices issue that is orthgonal to naming. I use the prefix &quot;my&quot; for instance-based field names and the &quot;our&quot; prefix for statics.</description>
		<content:encoded><![CDATA[<p>It has taken me many years to get to the style I use. I have been programming in OO since around 1990. I agree with the two recommendations in your blog, but I see them as a best practices issue that is orthgonal to naming. I use the prefix &#8220;my&#8221; for instance-based field names and the &#8220;our&#8221; prefix for statics.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Filip Duyck</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22816</link>
		<dc:creator>Filip Duyck</dc:creator>
		<pubDate>Mon, 26 Oct 2009 13:23:57 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22816</guid>
		<description>I put underscores mostly because when I want to access a member variable, I just type _ and get a list of nothing but member variables. Many moons ago another reason was to easily make them stand out against local variables, but R# fixes that as you pointed out. Last one I can think of (but not really a special reason for me) is to avoid naming clashes with local variables - typing &#039;_&#039; is so much easier than typing &#039;this.&#039; :-)

In the end, I don&#039;t think it&#039;s something worth thinking about all that much ;)</description>
		<content:encoded><![CDATA[<p>I put underscores mostly because when I want to access a member variable, I just type _ and get a list of nothing but member variables. Many moons ago another reason was to easily make them stand out against local variables, but R# fixes that as you pointed out. Last one I can think of (but not really a special reason for me) is to avoid naming clashes with local variables &#8211; typing &#8216;_&#8217; is so much easier than typing &#8216;this.&#8217; <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>In the end, I don&#8217;t think it&#8217;s something worth thinking about all that much <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: den Ben</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22815</link>
		<dc:creator>den Ben</dc:creator>
		<pubDate>Mon, 26 Oct 2009 12:52:55 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22815</guid>
		<description>+1 for NDepend&#039;s LCOM metric (Lack Of Cohesion of Methods: http://www.ndepend.com/Metrics.aspx#LCOM)

Still, a visual indication is nice to have.  So if you do not use Resharper, prefixing them with an underscore doesn&#039;t seem at all that bad to me.  Then again, why would one not use Resharper? ;-)</description>
		<content:encoded><![CDATA[<p>+1 for NDepend&#8217;s LCOM metric (Lack Of Cohesion of Methods: <a href="http://www.ndepend.com/Metrics.aspx#LCOM)" rel="nofollow">http://www.ndepend.com/Metrics.aspx#LCOM)</a></p>
<p>Still, a visual indication is nice to have.  So if you do not use Resharper, prefixing them with an underscore doesn&#8217;t seem at all that bad to me.  Then again, why would one not use Resharper? <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22814</link>
		<dc:creator>Mark</dc:creator>
		<pubDate>Mon, 26 Oct 2009 12:28:36 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22814</guid>
		<description>It&#039;s a very good point regarding field-itis.  I recently pair programmed with a junior developer who was using a lot of unnecessary fields and it was something that stuck out like a sore thumb.  

The reason I noticed was not down to some inbuilt software engineering smell detector; it was purely down to the fact that the unnecessary use of fields frequently made the code harder to follow.  You have to jump around looking to see what the field is, who accesses it and so forth.  

It should also be noted that NDepend&#039;s LCOM metric will flag up classes that have fields accessed by a small proportion of its methods.  This could be useful for identifying classes with this problem.</description>
		<content:encoded><![CDATA[<p>It&#8217;s a very good point regarding field-itis.  I recently pair programmed with a junior developer who was using a lot of unnecessary fields and it was something that stuck out like a sore thumb.  </p>
<p>The reason I noticed was not down to some inbuilt software engineering smell detector; it was purely down to the fact that the unnecessary use of fields frequently made the code harder to follow.  You have to jump around looking to see what the field is, who accesses it and so forth.  </p>
<p>It should also be noted that NDepend&#8217;s LCOM metric will flag up classes that have fields accessed by a small proportion of its methods.  This could be useful for identifying classes with this problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Awkward Coder</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22813</link>
		<dc:creator>Awkward Coder</dc:creator>
		<pubDate>Mon, 26 Oct 2009 09:41:49 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22813</guid>
		<description>Old C++ head here! So I can&#039;t stop using underscores, just habit...

I think method structure &amp; size are more important, just apply SRP in moderation along with obvious naming and you can&#039;t go wrong :)

also try not to use &#039;if&#039; statements ;)</description>
		<content:encoded><![CDATA[<p>Old C++ head here! So I can&#8217;t stop using underscores, just habit&#8230;</p>
<p>I think method structure &amp; size are more important, just apply SRP in moderation along with obvious naming and you can&#8217;t go wrong <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>also try not to use &#8216;if&#8217; statements <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BjartN</title>
		<link>http://davybrion.com/blog/2009/10/prefixing-instance-fields/comment-page-1/#comment-22812</link>
		<dc:creator>BjartN</dc:creator>
		<pubDate>Mon, 26 Oct 2009 06:50:43 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778#comment-22812</guid>
		<description>I don&#039;t think it really matters that much. The visual clue is the reason I use them, but I would have no problems not using them if a project demanded that. I think what is more important is that you stick with one style throughout the project making the code base more consistent.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t think it really matters that much. The visual clue is the reason I use them, but I would have no problems not using them if a project demanded that. I think what is more important is that you stick with one style throughout the project making the code base more consistent.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
