<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Performance Guidelines for Properties</title><link>http://blogs.msdn.com/b/ricom/archive/2011/12/19/performance-guidelines-for-properties.aspx</link><description>I can&amp;rsquo;t say I&amp;rsquo;ve asked the framework guidelines folks about this but I&amp;rsquo;m fairly sure there would be a lot of agreement from the guidelines gurus; so in the spirit of approximately correct advice I give you Rico&amp;rsquo;s Guidelines for</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Performance Guidelines for Properties</title><link>http://blogs.msdn.com/b/ricom/archive/2011/12/19/performance-guidelines-for-properties.aspx#10253304</link><pubDate>Thu, 05 Jan 2012 03:13:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10253304</guid><dc:creator>ricom</dc:creator><description>&lt;p&gt;Umm... in 21 years? :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10253304" width="1" height="1"&gt;</description></item><item><title>re: Performance Guidelines for Properties</title><link>http://blogs.msdn.com/b/ricom/archive/2011/12/19/performance-guidelines-for-properties.aspx#10253272</link><pubDate>Thu, 05 Jan 2012 00:20:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10253272</guid><dc:creator>Reiley Yang</dc:creator><description>&lt;p&gt;Great to see your blog being active again.&lt;/p&gt;
&lt;p&gt;When shall I expect to see a series of &amp;quot;History of Internet Explorer&amp;quot;? :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10253272" width="1" height="1"&gt;</description></item><item><title>re: Performance Guidelines for Properties</title><link>http://blogs.msdn.com/b/ricom/archive/2011/12/19/performance-guidelines-for-properties.aspx#10250107</link><pubDate>Wed, 21 Dec 2011 20:16:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10250107</guid><dc:creator>ricom</dc:creator><description>&lt;p&gt;@Chris: &amp;nbsp;This is a strange pattern, the class in question is accepting a list of Bars from anyone at all and you&amp;#39;re willing to lazy-init an empty list in the event that you don&amp;#39;t have one yet. &amp;nbsp;Normally I&amp;#39;d say you should be careful not to expose your guts like that but in this case we&amp;#39;re clearly talking about some kind of argument acceptor/builder.&lt;/p&gt;
&lt;p&gt;The lazy init stuff often ends up being more trouble than its worth. &amp;nbsp;Consider:&lt;/p&gt;
&lt;p&gt;if (yourThing.Bars.Count == 0) {&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Now we made an empty list just so we could see that it&amp;#39;s empty?&lt;/p&gt;
&lt;p&gt;Following the no-alloc rule if you have a read-only empty ilist that can&amp;#39;t be changed and you returned that then you get a much better pattern.&lt;/p&gt;
&lt;p&gt;_bars = mySharedReadonlyEmptyList;&lt;/p&gt;
&lt;p&gt;public IList&amp;lt;Foo&amp;gt; Bars { get { return _bars; } set { _bars = value }}&lt;/p&gt;
&lt;p&gt;Life is good. &amp;nbsp;Count is free. &amp;nbsp;Bars is simple. &amp;nbsp;10/10.&lt;/p&gt;
&lt;p&gt;But that whole pattern is scary... you can&amp;#39;t assume anything about Bars from call to call becuase they can change the guts of the list and you would never know.&lt;/p&gt;
&lt;p&gt;This is really a whole different kind of sin.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10250107" width="1" height="1"&gt;</description></item><item><title>re: Performance Guidelines for Properties</title><link>http://blogs.msdn.com/b/ricom/archive/2011/12/19/performance-guidelines-for-properties.aspx#10249740</link><pubDate>Tue, 20 Dec 2011 21:10:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10249740</guid><dc:creator>Chris Marisic</dc:creator><description>&lt;p&gt;What about properties that behave as such?&lt;/p&gt;
&lt;p&gt;public IList&amp;lt;Foo&amp;gt; Bars { get { return Bars ?? (Bars = new List&amp;lt;Food&amp;gt;()) } set { _bars = value }}&lt;/p&gt;
&lt;p&gt;I actually hate having to define these setters and targeting a backing field. Seems like something the auto property should be able to handle. or a keyword like _field = value etc.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10249740" width="1" height="1"&gt;</description></item><item><title>re: Performance Guidelines for Properties</title><link>http://blogs.msdn.com/b/ricom/archive/2011/12/19/performance-guidelines-for-properties.aspx#10249710</link><pubDate>Tue, 20 Dec 2011 19:47:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10249710</guid><dc:creator>ricom</dc:creator><description>&lt;p&gt;@Daniel: indeed I like it much better than the auto-init flavor&lt;/p&gt;
&lt;p&gt;@Martin: of course you&amp;#39;re right: it&amp;#39;s all about the tradeoffs but on the other hand I do like to prime the pump if you will with what greatness looks like. &amp;nbsp;At least to my eyes :) &amp;nbsp; &amp;nbsp;Thanks for the reference, a lot of truth there too.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10249710" width="1" height="1"&gt;</description></item><item><title>re: Performance Guidelines for Properties</title><link>http://blogs.msdn.com/b/ricom/archive/2011/12/19/performance-guidelines-for-properties.aspx#10249587</link><pubDate>Tue, 20 Dec 2011 14:06:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10249587</guid><dc:creator>Daniel Neely</dc:creator><description>&lt;p&gt;I think I wasn&amp;#39;t clear enough in my first comment. &amp;nbsp;Throwing an exception for a lazy initialized property eliminates the potential performance surprise from first access that would occur if the null check called the initializer, but does so at the cost of breaking the encapsulation of the lazy initialization because the caller needs to explicitly call the initialization method. &amp;nbsp;I&amp;#39;m not sure that tradeoff is worthwhile since you&amp;#39;re making things harder on the caller; and if a future version is able to replace the heavy initialization with something light enough not to need special behavior you&amp;#39;re forced to modify all the using code to make the change.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10249587" width="1" height="1"&gt;</description></item><item><title>re: Performance Guidelines for Properties</title><link>http://blogs.msdn.com/b/ricom/archive/2011/12/19/performance-guidelines-for-properties.aspx#10249559</link><pubDate>Tue, 20 Dec 2011 12:12:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10249559</guid><dc:creator>Martin Jul</dc:creator><description>&lt;p&gt;From a performance point you are spot on, but it is also relevant to look at it from a design point of view. Properties (and fields) tend to expose implementation details, leading to tightly coupled code, as argued in the classic article by Allen Holub, “Why getter and setter methods are evil”&lt;/p&gt;
&lt;p&gt;(you have to forgive the Java folks that they did not have properties back when he wrote that):&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html"&gt;www.javaworld.com/.../jw-0905-toolbox.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;quot;Though getter/setter methods are commonplace in Java, they are not particularly object oriented (OO). In fact, they can damage your code&amp;#39;s maintainability. Moreover, the presence of numerous getter and setter methods is a red flag that the program isn&amp;#39;t necessarily well designed from an OO perspective. &amp;quot;&lt;/p&gt;
&lt;p&gt;His point is that properties tend to give the illusion of encapsulation, while actually exposing implementation. Fields are more honest about exposing zero-behaviour implementation details. The alternative is using double-dispatch, interfaces etc. but most people consider that a lot of work in languages like C# and tend to prefer properties for that reason. &lt;/p&gt;
&lt;p&gt;I think a good rule of thumb for using fields/properties is to at least encapsulate the value they give out, e.g.&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t give a Price field the type &amp;quot;double&amp;quot; just because it would currently fit in that data type. Create a Price or Money struct instead and expose that. Then it&amp;#39;s easier to change the implementation when you realise that doubles have rounding errors that are not acceptable. From a perf view this should be just as good, from a design view the semantics of the field are much clearer, and from a maintainability view it saves you a lot of trouble.&lt;/p&gt;
&lt;p&gt;In any case remember, it’s all about trade-offs, not absolutes. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10249559" width="1" height="1"&gt;</description></item><item><title>re: Performance Guidelines for Properties</title><link>http://blogs.msdn.com/b/ricom/archive/2011/12/19/performance-guidelines-for-properties.aspx#10249503</link><pubDate>Tue, 20 Dec 2011 08:36:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10249503</guid><dc:creator>tobi</dc:creator><description>&lt;p&gt;All of this article is very true and best practice. In the performance hot-spots for performance reasons, in the cold-spots for maintenance reasons. Although the rules in the latter case are relaxed.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10249503" width="1" height="1"&gt;</description></item><item><title>re: Performance Guidelines for Properties</title><link>http://blogs.msdn.com/b/ricom/archive/2011/12/19/performance-guidelines-for-properties.aspx#10249475</link><pubDate>Tue, 20 Dec 2011 07:08:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10249475</guid><dc:creator>Henry Boehlert</dc:creator><description>&lt;p&gt;Even if I follow your recommendation (prefer field/hashtable-backed properties), it still will be hard for the compiler to reason whether my property value can change between two calls to get_Property and more often than not it won&amp;#39;t be able to optimize away the second call.&lt;/p&gt;
&lt;p&gt;It is still a pity .NET (or C#/VB, fwiw) don&amp;#39;t support const declarations in the C++ sense, or at least real read-only properties. That would give the compiler at least some hint and could even provide support for virtual properties.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10249475" width="1" height="1"&gt;</description></item><item><title>re: Performance Guidelines for Properties</title><link>http://blogs.msdn.com/b/ricom/archive/2011/12/19/performance-guidelines-for-properties.aspx#10249433</link><pubDate>Tue, 20 Dec 2011 02:56:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10249433</guid><dc:creator>ricom</dc:creator><description>&lt;p&gt;I just added the bit about properties not causing side-effects, I&amp;#39;d meant to include that and forgotten.&lt;/p&gt;
&lt;p&gt;Properties that might be fast or might not be fast, that might fail or might not fail, are a major source of problems.&lt;/p&gt;
&lt;p&gt;@Daniel: The exact example that you give above has no allocations on the non-exceptional path and a clean failure path so I wouldn&amp;#39;t worry about it. &amp;nbsp;But really simpler is better. &amp;nbsp;And as Bjorn says, just changing it to a function is enough to make it clear that there is more there than meets the eye.&lt;/p&gt;
&lt;p&gt;Remember, &amp;quot;The code is more what you&amp;#39;d call &amp;#39;guidelines&amp;#39; than actual rules...&amp;quot;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10249433" width="1" height="1"&gt;</description></item></channel></rss>