<?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 - c#</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>Sun, 12 Sep 2010 02:03:15 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.12105.0</generator>
    <managingEditor>dasblog@example.com</managingEditor>
    <webMaster>dasblog@example.com</webMaster>
    <item>
      <trackback:ping>http://popcyclical.com/Trackback.aspx?guid=368068e5-b898-46f6-90f6-d34c665d7db4</trackback:ping>
      <pingback:server>http://popcyclical.com/pingback.aspx</pingback:server>
      <pingback:target>http://popcyclical.com/PermaLink,guid,368068e5-b898-46f6-90f6-d34c665d7db4.aspx</pingback:target>
      <dc:creator>James Kolpack</dc:creator>
      <wfw:comment>http://popcyclical.com/CommentView,guid,368068e5-b898-46f6-90f6-d34c665d7db4.aspx</wfw:comment>
      <wfw:commentRss>http://popcyclical.com/SyndicationService.asmx/GetEntryCommentsRss?guid=368068e5-b898-46f6-90f6-d34c665d7db4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In <a href="http://weblogs.asp.net/jgalloway/">Jon Galloway’s</a><a href="http://weblogs.asp.net/jgalloway/archive/2005/09/27/426087.aspx">Splitting
Camel Case with RegEx</a> blog post, he introduced a simple regular expression replacement
which can split “ThisIsInPascalCase” into “This Is In Pascal Case”.  Here’s the
original code:
</p>
        <pre>
          <code>output = System.Text.RegularExpressions.Regex.Replace( input, "([A-Z])",
" $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim(); </code>
        </pre>
        <p>
Simple and effective.  Matches any capital letters and inserts a space before
them.  But there’s room for improvement.  First, the call to <code>String.Trim()</code> to
remove any spaces potentially added if the first letter is uppercase – this can be
handled with a <a href="http://msdn.microsoft.com/en-us/library/az24scfc.aspx#grouping_constructs">“Match
if prefix is absent” group</a> containing the “beginning of line” character <code>^</code>. 
This prevents any matches from occurring on the first character, which eliminates
the need for the <code>String.Trim()</code> call.  The formal name for this grouping
construct is “Zero-width negative lookbehind assertion”, but just think of it as “if
you see what’s in here, don’t match the next thing”.
</p>
        <pre>
          <code> (?&lt;!^)([A-Z])</code>
        </pre>
        <p>
Next - there’s a potential issue with how acronyms get handled with this.  Given
this fictional book title: “WCFForNoobs” – the split will occur on each uppercase
letter resulting in “W C F For Noobs”.  The fix is simple, though – require that
uppercase letters be followed by a lowercase:
</p>
        <pre>
          <code> (?&lt;!^)([A-Z][a-z]) </code>
        </pre>
        <p>
…Now it’ll result in “WCF For Noobs” (aren’t we all!).  But now it won’t add
a space before the acronym – for “LearnWCFInSixEasyMonths”, the result will be “LearnWCF
In Six Easy Months”.  No problem – add an alternate match for a lowercase letter
coming before the uppercase letter.  The replace pattern makes this more difficult
– we don’t want the space to go before the lowercase letter, we want it between the
lowercase and the first capital letter of the acronym.  RegEx can handle this
with another lookbehind match group – “Match prefix but exclude it” - <code>(?&lt;=)</code>. 
This allows the match to occur on the lowercase-uppercase pair, but only the uppercase
portion will get matched, so when it comes time to run the replacement, the space
will get inserted between the two letters.  By itself, that’ll look like this:
</p>
        <pre>
          <code> ((?&lt;=[a-z])[A-Z]) </code>
        </pre>
        <p>
Great!  But this needs to be combined with previous expression.  Easy accomplished
with an either/or match using the vertical bar “or” construct:
</p>
        <pre>
          <code> (?&lt;!^)([A-Z][a-z]|(?&lt;=[a-z])[A-Z]) </code>
        </pre>
        <p>
The example “LearnWCFInSixEasyMonths” will now be split into “Learn WCF In Six Easy
Months”.  These same techniques can be used for additional splits – perhaps on
numbers or underscores.  More generally, <a href="http://www.regular-expressions.info/lookaround.html">lookbehind
and lookahead are great tools</a> to have in your RegEx toolbelt.
</p>
        <img width="0" height="0" src="http://popcyclical.com/aggbug.ashx?id=368068e5-b898-46f6-90f6-d34c665d7db4" />
        <br />
        <hr />
        <a href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=1252729" rel="tag" style="display:none">CodeProject</a>
      </body>
      <title>Splitting Pascal/Camel Case with RegEx Enhancements</title>
      <guid isPermaLink="false">http://popcyclical.com/PermaLink,guid,368068e5-b898-46f6-90f6-d34c665d7db4.aspx</guid>
      <link>http://popcyclical.com/2010/09/12/SplittingPascalCamelCaseWithRegExEnhancements.aspx</link>
      <pubDate>Sun, 12 Sep 2010 02:03:15 GMT</pubDate>
      <description>&lt;p&gt;
In &lt;a href="http://weblogs.asp.net/jgalloway/"&gt;Jon Galloway’s&lt;/a&gt; &lt;a href="http://weblogs.asp.net/jgalloway/archive/2005/09/27/426087.aspx"&gt;Splitting
Camel Case with RegEx&lt;/a&gt; blog post, he introduced a simple regular expression replacement
which can split “ThisIsInPascalCase” into “This Is In Pascal Case”.&amp;nbsp; Here’s the
original code:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;output = System.Text.RegularExpressions.Regex.Replace( input, "([A-Z])",
" $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim(); &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Simple and effective.&amp;nbsp; Matches any capital letters and inserts a space before
them.&amp;nbsp; But there’s room for improvement.&amp;nbsp; First, the call to &lt;code&gt;String.Trim()&lt;/code&gt; to
remove any spaces potentially added if the first letter is uppercase – this can be
handled with a &lt;a href="http://msdn.microsoft.com/en-us/library/az24scfc.aspx#grouping_constructs"&gt;“Match
if prefix is absent” group&lt;/a&gt; containing the “beginning of line” character &lt;code&gt;^&lt;/code&gt;.&amp;nbsp;
This prevents any matches from occurring on the first character, which eliminates
the need for the &lt;code&gt;String.Trim()&lt;/code&gt; call.&amp;nbsp; The formal name for this grouping
construct is “Zero-width negative lookbehind assertion”, but just think of it as “if
you see what’s in here, don’t match the next thing”.
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; (?&amp;lt;!^)([A-Z])&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Next - there’s a potential issue with how acronyms get handled with this.&amp;nbsp; Given
this fictional book title: “WCFForNoobs” – the split will occur on each uppercase
letter resulting in “W C F For Noobs”.&amp;nbsp; The fix is simple, though – require that
uppercase letters be followed by a lowercase:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; (?&amp;lt;!^)([A-Z][a-z]) &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
…Now it’ll result in “WCF For Noobs” (aren’t we all!).&amp;nbsp; But now it won’t add
a space before the acronym – for “LearnWCFInSixEasyMonths”, the result will be “LearnWCF
In Six Easy Months”.&amp;nbsp; No problem – add an alternate match for a lowercase letter
coming before the uppercase letter.&amp;nbsp; The replace pattern makes this more difficult
– we don’t want the space to go before the lowercase letter, we want it between the
lowercase and the first capital letter of the acronym.&amp;nbsp; RegEx can handle this
with another lookbehind match group – “Match prefix but exclude it” - &lt;code&gt;(?&amp;lt;=)&lt;/code&gt;.&amp;nbsp;
This allows the match to occur on the lowercase-uppercase pair, but only the uppercase
portion will get matched, so when it comes time to run the replacement, the space
will get inserted between the two letters.&amp;nbsp; By itself, that’ll look like this:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; ((?&amp;lt;=[a-z])[A-Z]) &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Great!&amp;nbsp; But this needs to be combined with previous expression.&amp;nbsp; Easy accomplished
with an either/or match using the vertical bar “or” construct:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; (?&amp;lt;!^)([A-Z][a-z]|(?&amp;lt;=[a-z])[A-Z]) &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
The example “LearnWCFInSixEasyMonths” will now be split into “Learn WCF In Six Easy
Months”.&amp;nbsp; These same techniques can be used for additional splits – perhaps on
numbers or underscores.&amp;nbsp; More generally, &lt;a href="http://www.regular-expressions.info/lookaround.html"&gt;lookbehind
and lookahead are great tools&lt;/a&gt; to have in your RegEx toolbelt.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://popcyclical.com/aggbug.ashx?id=368068e5-b898-46f6-90f6-d34c665d7db4" /&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,368068e5-b898-46f6-90f6-d34c665d7db4.aspx</comments>
      <category>c#</category>
      <category>regex</category>
    </item>
    <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>
          <code>using (new OperationTimer("MyOperation", this)) { // ... complete operation
}</code>
        </pre>
        <p>
This innocent looking C# snippet is hiding a tricky secret - the <code>using</code> statement
is being misused (no pun intended).  The <a href="http://msdn.microsoft.com/en-us/library/yh598w02%28VS.80%29.aspx">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"><code>IDisposable</code></a> implementation,
you’d be dealing with an <a href="http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx">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>
          <code>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); } }</code>
        </pre>
        <p>
The constructor starts a timer and the <code>Dispose()</code> 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">BeginForm
helper</a>.  The only “resource” it disposes of is to render a closing <code>&lt;/form&gt;</code> 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>
          <code>expression1 &amp;&amp; expression2 || expression3</code>
        </pre>
        <p>
Which is equivalent to:
</p>
        <pre>
          <code>if (expression1) expression2 else expression3</code>
        </pre>
        <p>
This takes advantage of the order of evaluation in a logical statement – it is assumed
(correctly) that <code>expression2</code> will never be evaluated if <code>expression1</code> is
evaluated as false, and instead, <code>expression3</code> will get to run.  Likewise,
if the first two evaluate to true, the truth value is known for the statement and <code>expression3</code> 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 <code>IDisposable</code>? 
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&gt;&lt;code&gt;using (new OperationTimer("MyOperation", this)) { // ... complete operation
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
This innocent looking C# snippet is hiding a tricky secret - the &lt;code&gt;using&lt;/code&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"&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"&gt;&lt;code&gt;IDisposable&lt;/code&gt;&lt;/a&gt; implementation,
you’d be dealing with an &lt;a href="http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx"&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&gt;&lt;code&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;/code&gt;&lt;/pre&gt;
&lt;p&gt;
The constructor starts a timer and the &lt;code&gt;Dispose()&lt;/code&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"&gt;BeginForm
helper&lt;/a&gt;.&amp;nbsp; The only “resource” it disposes of is to render a closing &lt;code&gt;&amp;lt;/form&amp;gt;&lt;/code&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&gt;&lt;code&gt;expression1 &amp;amp;&amp;amp; expression2 || expression3&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Which is equivalent to:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if (expression1) expression2 else expression3&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
This takes advantage of the order of evaluation in a logical statement – it is assumed
(correctly) that &lt;code&gt;expression2&lt;/code&gt; will never be evaluated if &lt;code&gt;expression1&lt;/code&gt; is
evaluated as false, and instead, &lt;code&gt;expression3&lt;/code&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;code&gt;expression3&lt;/code&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;code&gt;IDisposable&lt;/code&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>