<?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>So Jake Says &#187; C# Process</title>
	<atom:link href="http://www.jakevoytko.com/blog/tag/c-process/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jakevoytko.com/blog</link>
	<description>Ye Olde Computer Science Blogge</description>
	<lastBuildDate>Sun, 17 Jan 2010 15:16:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>The Law of Expanding Wrappers</title>
		<link>http://www.jakevoytko.com/blog/2008/09/01/the-law-of-expanding-wrappers/</link>
		<comments>http://www.jakevoytko.com/blog/2008/09/01/the-law-of-expanding-wrappers/#comments</comments>
		<pubDate>Mon, 01 Sep 2008 04:00:26 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C# Process]]></category>
		<category><![CDATA[Classes]]></category>
		<category><![CDATA[composition vs. inheritance]]></category>
		<category><![CDATA[Extension]]></category>
		<category><![CDATA[Law of Expanding Wrappers]]></category>
		<category><![CDATA[Wrappers]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=138</guid>
		<description><![CDATA[Or, Why It&#8217;s Sometimes Better To Inherit Than to Write a Wrapper Class All Part of the Process I recently wrote code in C# that needed to call an external program for an answer. No problem! Let&#8217;s just instantiate the Process class and be on our merry way! public static string GetOutputFromProcess&#40;string name, string args&#41; [...]]]></description>
			<content:encoded><![CDATA[<p><em>Or, Why It&#8217;s Sometimes Better To Inherit Than to Write a Wrapper Class</em></p>
<h2>All Part of the <code>Process</code></h2>
<p>I recently wrote code in C# that needed to call an external program for an answer. No problem! Let&#8217;s just instantiate the <code>Process</code> class and be on our merry way!</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> GetOutputFromProcess<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> name, <span style="color: #FF0000;">string</span> args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Process proc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Process<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    ProcessStartInfo sI
        <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ProcessStartInfo<span style="color: #000000;">&#40;</span>name, args<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    sI.<span style="color: #0000FF;">RedirectStandardError</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
    sI.<span style="color: #0000FF;">RedirectStandardInput</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
    sI.<span style="color: #0000FF;">RedirectStandardOutput</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
    sI.<span style="color: #0000FF;">UseShellExecute</span> <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
    sI.<span style="color: #0000FF;">CreateNoWindow</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
    proc.<span style="color: #0000FF;">StartInfo</span> <span style="color: #008000;">=</span> sI<span style="color: #008000;">;</span>
&nbsp;
    proc.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    proc.<span style="color: #0000FF;">WaitForExit</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">return</span> proc.<span style="color: #0000FF;">StandardOutput</span>.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This code sucks, but it works under strict assumptions.</p>
<ul>
<li>If the program is guaranteed to always exit, then this function will always return a value.</li>
<li>If the program runs in less than about 50 milliseconds on Grandma&#8217;s machine (the one that you thought was blazing fast back in 1997), there won&#8217;t be many noticeable UI hiccups.</li>
</ul>
<p>The biggest problem (to me) is that this code is not extensible, unless you like 35-parameter functions, or 35 3-parameter functions. I don&#8217;t.</p>
<h2>Class Act</h2>
<p>We use this code on our project for a while, and we finally need to add functionality: some of our programs need to output errors to the terminal for debugging purposes. Let&#8217;s make this into a class!</p>
<p>We all know that <a href="http://www.ronkes.nl/blog/?2005-10-26-compositionvsinheritance">composition is preferred to inheritance</a>, so let&#8217;s avoid evil inheritance and compose this sucker!</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> MyProcessManager
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> Process proc<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> MyProcessManager<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> name, <span style="color: #FF0000;">string</span> args<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        proc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Process<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        ProcessStartInfo sI
            <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ProcessStartInfo<span style="color: #000000;">&#40;</span>name, args<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        sI.<span style="color: #0000FF;">RedirectStandardError</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        sI.<span style="color: #0000FF;">RedirectStandardInput</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        sI.<span style="color: #0000FF;">RedirectStandardOutput</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        sI.<span style="color: #0000FF;">UseShellExecute</span> <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
        sI.<span style="color: #0000FF;">CreateNoWindow</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
        proc.<span style="color: #0000FF;">StartInfo</span> <span style="color: #008000;">=</span> sI<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> UseErrorTerminal<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> use<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        proc.<span style="color: #0000FF;">StartInfo</span>.<span style="color: #0000FF;">CreateNoWindow</span> <span style="color: #008000;">=</span> <span style="color: #008000;">!</span>use<span style="color: #008000;">;</span>
        proc.<span style="color: #0000FF;">StartInfo</span>.<span style="color: #0000FF;">RedirectStandardError</span> <span style="color: #008000;">=</span> <span style="color: #008000;">!</span>use<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        proc.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        proc.<span style="color: #0000FF;">WaitForExit</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> proc.<span style="color: #0000FF;">StandardOutput</span>.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This isn&#8217;t so bad. This is extensible, testable, and the functionality is simple. Perfect!</p>
<p>Well, not quite. Let&#8217;s say that we have a program that takes 7 seconds to run. This will freeze the UI solid without threading. We will need to add delegate handling and enable events in the <code>Process</code>.</p>
<p>Maybe we want to redirect each of the streams at different times.</p>
<p>Maybe we want to change the <code>Process</code> name without changing anything else.</p>
<p>Maybe we want to have access to the standard output and standard error separately while the <code>Process</code> is running.</p>
<p>Enter <em>The Law of Expanding Wrappers</em>:</p>
<h2>The Law of Expanding Wrappers</h2>
<blockquote><p>A wrapper class will expand to match the functionality of the wrapped class.</p></blockquote>
<h2>What Does This Imply?</h2>
<p>The direct implication is that you can (sometimes) save yourself effort by using inheritance! Rather than adding wrappers in our <code>Process</code> example, isn&#8217;t this easier?</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> MyCustomProcess<span style="color: #008000;">:</span> Process
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> MyCustomProcess<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> name, <span style="color: #FF0000;">string</span> args<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        ProcessStartInfo sI
            <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ProcessStartInfo<span style="color: #000000;">&#40;</span>name, args<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        sI.<span style="color: #0000FF;">RedirectStandardError</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        sI.<span style="color: #0000FF;">RedirectStandardInput</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        sI.<span style="color: #0000FF;">RedirectStandardOutput</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        sI.<span style="color: #0000FF;">UseShellExecute</span> <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
        sI.<span style="color: #0000FF;">CreateNoWindow</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
        StartInfo <span style="color: #008000;">=</span> sI<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The magic here is that you get ALL of the functionality of <code>Process</code>, <em>for free</em>. The whole <code>Process</code> class is at your disposal. Modifying small details is trivial! If you want to add functionality, just write a method to do it!</p>
<p>Even better, this helps stave off the <a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">Law of Leaky Abstractions</a>:</p>
<blockquote><p>All non-trivial abstractions, to some degree, are leaky.</p></blockquote>
<p>We&#8217;ve changed a potentially complex abstraction into a trivial extension. That&#8217;s gotta be worth something.</p>
<h2>What Doesn&#8217;t This Imply?</h2>
<p><strong>This does not say that the interface ends up identical.</strong></p>
<p>Maybe you&#8217;re passing memory-managing objects instead of pointers to objects. Maybe your interface only has half of the methods, and is far better organized. Great! Good work! There&#8217;s no law that says that yucky-but-functional interfaces have to be used.</p>
<p>You might not use every method that the old class offers. Some methods deprecate, and some are simply vestigial. <strong>If nobody uses a function, it&#8217;s not functionality.</strong></p>
<p>On a related note&#8230;</p>
<p><strong>This does not say that the interface ends up worse</strong>.</p>
<p>In the example of <code>Process</code>, it is difficult to find a generalized interface that is better than the interface of <code>Process</code>. It&#8217;s easy to use, and you can pass around and store the starting info as an entity.</p>
<p>Not all interfaces are created equal. You may be able to generate all functionality from a few parameters. Maybe you can slay redundant parameters with a single blow.</p>
<p>Your interface could even add functionality (like error-checking).</p>
<h2>Conclusions</h2>
<p>At this simplest level, I make the case that programmers should consider using inheritance instead of composition for wrapper classes. If you do choose the wrapper approach, it should be for the following reasons:</p>
<ol>
<li>You understand that you&#8217;re going to eventually get the same functionality that the wrapped class offers.</li>
<li>You still have a damn good reason (like improving the input/output).</li>
</ol>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=138&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/09/01/the-law-of-expanding-wrappers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
