<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<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/"
	>

<channel>
	<title>object-oriented &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://wordpress.com/tag/object-oriented/</link>
	<description>Feed of posts on WordPress.com tagged "object-oriented"</description>
	<pubDate>Thu, 21 Aug 2008 00:06:29 +0000</pubDate>

	<generator>http://wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[Static is music to my ears]]></title>
<link>http://nullpointerfactory.wordpress.com/?p=122</link>
<pubDate>Tue, 19 Aug 2008 05:25:04 +0000</pubDate>
<dc:creator>Yev</dc:creator>
<guid>http://nullpointerfactory.wordpress.com/?p=122</guid>
<description><![CDATA[There&#8217;s something in the collective consciousness of OOP practitioners, an annoying little dem]]></description>
<content:encoded><![CDATA[<p>There's something in the collective consciousness of OOP practitioners, an annoying little demon that makes us think of every datum as a class instance, and every function as an instance method. I'll leave the classes alone, but our affinity for instance methods is troubling. Instance methods have quite a few disadvantages over static methods:</p>
<ul>
<li>They cannot be tested without instantiating their respective classes (imposing both memory and performance costs).</li>
<li>They lend themselves to reuse via inheritance and not via composition, though the latter is generally accepted as the preferable approach.</li>
<li>When passed as delegates in C#, they create a potential memory leak by preventing the garbage collector from scooping up their owner when no longer needed.</li>
</ul>
<p>So let's put the instance demon firmly in his place. Make every method you write static unless you can explicitly explain it needs to be a member. The only two such explanations I could think of were "it's a modifier" and "it requires private fields". Barring those two scenarios, a function can do just fine being static. For example if we have an Account class, and we want to generate some text summary of the account, it's tempting to add an instance member to Account:</p>
<pre>public class Account{
   public String getSummary(){
       return this.getName() + " has a balance of $" + this.getBalance();
   }
   ...
}</pre>
<p>I would argue that this is better off as a static method:</p>
<pre>public class AccountUtilities{
   public static String generateAccountSummary(string name, double balance){
       return name + " has a balance of $" balance;
   }
}</pre>
<p>Now, you get the freedom of using that report logic with any data, not just the Account class or its subclasses. You also get the ability to test it without mocking Account. Yay!</p>
<p>I don't think I've written anything earth-shattering here, but without a concious effort, I find the natural propensity for writing members working its way into my code. We need to combat, or at least to recognize and question our instance instinct (try saying that five times fast). Our code will be that much better for it.</p>
]]></content:encoded>
</item>

</channel>
</rss>
