<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>popcyclical - language</title>
    <link>http://popcyclical.com/</link>
    <description>The software development blog of James "poprhythm" Kolpack</description>
    <language>en-us</language>
    <copyright>James Kolpack</copyright>
    <lastBuildDate>Wed, 24 Mar 2010 01:06:11 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>dasblog@example.com</managingEditor>
    <webMaster>dasblog@example.com</webMaster>
    <item>
      <trackback:ping>http://popcyclical.com/Trackback.aspx?guid=86f1449a-8d4c-4b00-892d-7c93f655b879</trackback:ping>
      <pingback:server>http://popcyclical.com/pingback.aspx</pingback:server>
      <pingback:target>http://popcyclical.com/PermaLink,guid,86f1449a-8d4c-4b00-892d-7c93f655b879.aspx</pingback:target>
      <dc:creator>James Kolpack</dc:creator>
      <wfw:comment>http://popcyclical.com/CommentView,guid,86f1449a-8d4c-4b00-892d-7c93f655b879.aspx</wfw:comment>
      <wfw:commentRss>http://popcyclical.com/SyndicationService.asmx/GetEntryCommentsRss?guid=86f1449a-8d4c-4b00-892d-7c93f655b879</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’m feeling a bit guilty about some code I wrote:
</p>
        <pre class="brush: csharp; light: true;">using (new OperationTimer("MyOperation", this))
{
    // ... complete operation
}</pre>
        <p>
This innocent looking C# snippet is hiding a tricky secret - the <font face="Courier New">using</font> statement
is being misused (no pun intended).  The <a href="http://msdn.microsoft.com/en-us/library/yh598w02%28VS.80%29.aspx" target="_blank">documentation
defines the intended usage clearly</a>:
</p>
        <dl>
          <dt>using Statement 
<dd>
Defines a scope, outside of which an object or objects will be disposed.</dd></dt>
        </dl>
        <p>
The problem?  The notion of “object disposal” is being hijacked!  In your
garden variety <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx" target="_blank"><font face="Courier New">IDisposable</font></a> implementation,
you’d be dealing with an <a href="http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx" target="_blank">external
resource that needs to be released</a> before the object can be removed from memory. 
Instead, I’m using it to time a block of code like so:
</p>
        <pre class="brush: csharp; toolbar: false;">class OperationTimer : IDisposable
{
    private readonly string _operationName;
    private readonly ITimable _obj;
    private readonly Stopwatch _stopwatch;

    public OperationTimer(string operationName, ITimable obj)
    {
        _operationName = operationName;
        _obj = obj;
        _stopwatch = new Stopwatch();
        _stopwatch.Start();
    }

    public void Dispose()
    {
        _stopwatch.Stop();
        _obj.OnOperationCompleted(_operationName, _stopwatch.Elapsed);
    }
}</pre>
        <p>
The constructor starts a timer and the <font face="Courier New">Dispose()</font> method
stops it and reports the elapsed time.  (<em>aside: if you’re interested in how
I’m using the timer, check out my previous article </em><a href="http://popcyclical.com/2010/03/21/SimplifiedPerformanceCountersInNET.aspx"><em>Simplified
Performance Counters</em></a>) There are certainly other ways to accomplish this same
behavior, but they lack the elegance of a neatly scoped code block.  It’s arguably
an acceptable way to repurpose the language.  In fact, the ASP.NET MVC authors
saw fit to use it in a similar fashion with the <a href="http://msdn.microsoft.com/en-us/library/dd410596.aspx" target="_blank">BeginForm
helper</a>.  The only “resource” it disposes of is to render a closing <font face="Courier New">&lt;/form&gt;</font> tag.
</p>
        <p>
My question is: <strong>When does repurposing language constructs turn from “acceptable
language use” to a “dirty trick”, or worse, “illegible line noise”</strong>?
</p>
        <p>
It seems like a slippery slope.  One instance that I don’t care for is controlling
execution flow by-way-of logical operator precedence in most C-like languages:
</p>
        <pre class="brush: csharp;  light: true;">expression1 &amp;&amp; expression2 || expression3</pre>
        <p>
Which is equivalent to:
</p>
        <pre class="brush: csharp;  light: true;">if (expression1)
    expression2
else
    expression3</pre>
        <p>
This takes advantage of the order of evaluation in a logical statement – it is assumed
(correctly) that <font face="Courier New">expression2</font> will never be evaluated
if <font face="Courier New">expression1</font> is evaluated as false, and instead, <font face="Courier New">expression3</font> will
get to run.  Likewise, if the first two evaluate to true, the truth value is
known for the statement and <font face="Courier New">expression3</font> is never evaluated.
This is clearly not the intended usage which the language designers had in mind, but
it works, and it saves any keywords from being written.
</p>
        <p>
Some truly beautiful code has been written by way of hijacking the language. 
For instance, <a href="http://www.cise.ufl.edu/~manuel/obfuscate/pi.c">here’s a program
that will calculate the value of pi using an ascii circle</a>.  Truly neat -
but also completely useless from a software development standpoint.
</p>
        <p>
What do you think?  Should I just get over my guilt about repurposing <font face="Courier New">IDisposable</font>? 
Or, should I be true to the original intent of the language and find another way?
</p>
        <img width="0" height="0" src="http://popcyclical.com/aggbug.ashx?id=86f1449a-8d4c-4b00-892d-7c93f655b879" />
        <br />
        <hr />
        <a href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=1252729" rel="tag" style="display:none">CodeProject</a>
      </body>
      <title>Programming Language Misuse</title>
      <guid isPermaLink="false">http://popcyclical.com/PermaLink,guid,86f1449a-8d4c-4b00-892d-7c93f655b879.aspx</guid>
      <link>http://popcyclical.com/2010/03/24/ProgrammingLanguageMisuse.aspx</link>
      <pubDate>Wed, 24 Mar 2010 01:06:11 GMT</pubDate>
      <description>&lt;p&gt;
I’m feeling a bit guilty about some code I wrote:
&lt;/p&gt;
&lt;pre class="brush: csharp; light: true;"&gt;using (new OperationTimer("MyOperation", this))
{
    // ... complete operation
}&lt;/pre&gt;
&lt;p&gt;
This innocent looking C# snippet is hiding a tricky secret - the &lt;font face="Courier New"&gt;using&lt;/font&gt; statement
is being misused (no pun intended).&amp;nbsp; The &lt;a href="http://msdn.microsoft.com/en-us/library/yh598w02%28VS.80%29.aspx" target="_blank"&gt;documentation
defines the intended usage clearly&lt;/a&gt;:
&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;using Statement 
&lt;dd&gt;
Defines a scope, outside of which an object or objects will be disposed.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;
The problem?&amp;nbsp; The notion of “object disposal” is being hijacked!&amp;nbsp; In your
garden variety &lt;a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx" target="_blank"&gt;&lt;font face="Courier New"&gt;IDisposable&lt;/font&gt;&lt;/a&gt; implementation,
you’d be dealing with an &lt;a href="http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx" target="_blank"&gt;external
resource that needs to be released&lt;/a&gt; before the object can be removed from memory.&amp;nbsp;
Instead, I’m using it to time a block of code like so:
&lt;/p&gt;
&lt;pre class="brush: csharp; toolbar: false;"&gt;class OperationTimer : IDisposable
{
    private readonly string _operationName;
    private readonly ITimable _obj;
    private readonly Stopwatch _stopwatch;

    public OperationTimer(string operationName, ITimable obj)
    {
        _operationName = operationName;
        _obj = obj;
        _stopwatch = new Stopwatch();
        _stopwatch.Start();
    }

    public void Dispose()
    {
        _stopwatch.Stop();
        _obj.OnOperationCompleted(_operationName, _stopwatch.Elapsed);
    }
}&lt;/pre&gt;
&lt;p&gt;
The constructor starts a timer and the &lt;font face="Courier New"&gt;Dispose()&lt;/font&gt; method
stops it and reports the elapsed time.&amp;nbsp; (&lt;em&gt;aside: if you’re interested in how
I’m using the timer, check out my previous article &lt;/em&gt;&lt;a href="http://popcyclical.com/2010/03/21/SimplifiedPerformanceCountersInNET.aspx"&gt;&lt;em&gt;Simplified
Performance Counters&lt;/em&gt;&lt;/a&gt;) There are certainly other ways to accomplish this same
behavior, but they lack the elegance of a neatly scoped code block.&amp;nbsp; It’s arguably
an acceptable way to repurpose the language.&amp;nbsp; In fact, the ASP.NET MVC authors
saw fit to use it in a similar fashion with the &lt;a href="http://msdn.microsoft.com/en-us/library/dd410596.aspx" target="_blank"&gt;BeginForm
helper&lt;/a&gt;.&amp;nbsp; The only “resource” it disposes of is to render a closing &lt;font face="Courier New"&gt;&amp;lt;/form&amp;gt;&lt;/font&gt; tag.
&lt;/p&gt;
&lt;p&gt;
My question is: &lt;strong&gt;When does repurposing language constructs turn from “acceptable
language use” to a “dirty trick”, or worse, “illegible line noise”&lt;/strong&gt;?
&lt;/p&gt;
&lt;p&gt;
It seems like a slippery slope.&amp;nbsp; One instance that I don’t care for is controlling
execution flow by-way-of logical operator precedence in most C-like languages:
&lt;/p&gt;
&lt;pre class="brush: csharp;  light: true;"&gt;expression1 &amp;amp;&amp;amp; expression2 || expression3&lt;/pre&gt;
&lt;p&gt;
Which is equivalent to:
&lt;/p&gt;
&lt;pre class="brush: csharp;  light: true;"&gt;if (expression1)
    expression2
else
    expression3&lt;/pre&gt;
&lt;p&gt;
This takes advantage of the order of evaluation in a logical statement – it is assumed
(correctly) that &lt;font face="Courier New"&gt;expression2&lt;/font&gt; will never be evaluated
if &lt;font face="Courier New"&gt;expression1&lt;/font&gt; is evaluated as false, and instead, &lt;font face="Courier New"&gt;expression3&lt;/font&gt; will
get to run.&amp;nbsp; Likewise, if the first two evaluate to true, the truth value is
known for the statement and &lt;font face="Courier New"&gt;expression3&lt;/font&gt; is never evaluated.
This is clearly not the intended usage which the language designers had in mind, but
it works, and it saves any keywords from being written.
&lt;/p&gt;
&lt;p&gt;
Some truly beautiful code has been written by way of hijacking the language.&amp;nbsp;
For instance, &lt;a href="http://www.cise.ufl.edu/~manuel/obfuscate/pi.c"&gt;here’s a program
that will calculate the value of pi using an ascii circle&lt;/a&gt;.&amp;nbsp; Truly neat -
but also completely useless from a software development standpoint.
&lt;/p&gt;
&lt;p&gt;
What do you think?&amp;nbsp; Should I just get over my guilt about repurposing &lt;font face="Courier New"&gt;IDisposable&lt;/font&gt;?&amp;nbsp;
Or, should I be true to the original intent of the language and find another way?
&lt;/p&gt;
&lt;img width="0" height="0" src="http://popcyclical.com/aggbug.ashx?id=86f1449a-8d4c-4b00-892d-7c93f655b879" /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;a href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=1252729" rel="tag" style="display:none"&gt;CodeProject&lt;/a&gt;</description>
      <comments>http://popcyclical.com/CommentView,guid,86f1449a-8d4c-4b00-892d-7c93f655b879.aspx</comments>
      <category>c#</category>
      <category>language</category>
    </item>
  </channel>
</rss>