<?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>Factory Automation Software Blog &#187; .NET Framework Development</title>
	<atom:link href="http://factoryswblog.org/category/development-techniques/dotnet-dev/feed/" rel="self" type="application/rss+xml" />
	<link>http://factoryswblog.org</link>
	<description>Merging modern software development with electrons and metal</description>
	<lastBuildDate>Fri, 30 Jul 2010 00:00:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The Case of the Disappearing Exception</title>
		<link>http://factoryswblog.org/2008/06/24/case-disappearing-exception/</link>
		<comments>http://factoryswblog.org/2008/06/24/case-disappearing-exception/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 19:07:03 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[.NET Framework Development]]></category>
		<category><![CDATA[Exceptions]]></category>
		<category><![CDATA[vb.net]]></category>

		<guid isPermaLink="false">http://factoryswblog.org/?p=57</guid>
		<description><![CDATA[I was doing some debugging recently on an inherited Visual Basic.NET codebase.  Something odd was going on &#8211; the code that reads a PLC&#8217;s input was always returning True, but I could see the input changing.  The problem?  The original programmer, who I&#8217;ll call &#8220;Bugs Bunny&#8221;, didn&#8217;t understand exception handling at all.
The [...]]]></description>
			<content:encoded><![CDATA[<p>I was doing some debugging recently on an inherited Visual Basic.NET codebase.  Something odd was going on &#8211; the code that reads a PLC&#8217;s input was always returning True, but I could see the input changing.  The problem?  The original programmer, who I&#8217;ll call &#8220;Bugs Bunny&#8221;, didn&#8217;t understand exception handling at all.</p>
<p>The function, part of a class that communicated with a PLC, went roughly like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://factoryswblog.org/wp-content/plugins/wp-codebox/wp-codebox.php?p=57&amp;download=Disappearing-Exception.vb">Disappearing-Exception.vb</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p573"><td class="code" id="p57code3"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Function</span> bubba<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span>
    bubba <span style="color: #008000;">=</span> <span style="color: #0600FF;">True</span>
    <span style="color: #0600FF;">Try</span>
        bubba <span style="color: #008000;">=</span> getResultFromPlc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Catch</span> ex <span style="color: #FF8000;">As</span> Exception
        logException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Try</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></pre></td></tr></table></div>

<p>Since all Exceptions are caught and <strong>not handled</strong> any Exceptions raised in bubba()&#8217;s Try block just disappear.  So all the code calling bubba() has no way of knowing if there are problem (such as the PLC isn&#8217;t powered or the serial port isn&#8217;t connected) .  Instead, because the return value is initially set to True (<em>bubba = True</em>), if an Exception occurs, bubba() always return True.</p>
<p>Sometimes you do need to ignore Exceptions.  For example, on a maintenance screen that repeatedly reads a PLC&#8217;s inputs, it&#8217;s good to indicate if there are any problems reading the inputs, but it&#8217;s not helpful to pop up a dialog box or exit the maintenance screen.  Exceptions should be ignored occasionally as needed in the calling code, not completely swallowed up in the base library code.</p>
<p>Another common anti-pattern in Bug&#8217;s code is:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://factoryswblog.org/wp-content/plugins/wp-codebox/wp-codebox.php?p=57&amp;download=Useless_try-catch.vb">Useless_try-catch.vb</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p574"><td class="code" id="p57code4"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Sub</span> joe<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Try</span>
        blah<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        blahblah<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Catch</span> ex <span style="color: #FF8000;">As</span> Exception
        logException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">Throw</span> ex
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Try</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
<span style="color: #0600FF;">Sub</span> groovy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Try</span>
        joe<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Catch</span> ex <span style="color: #FF8000;">As</span> Exception
        logException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">Throw</span> ex
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Try</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></td></tr></table></div>

<p>What&#8217;s the point?  All that happens is the exception gets logged multiple times (adding confusion to the log file), more useless code is added (making the code harder to understand), and the program runs slower (throw, catching, and re-throwing Exceptions takes time).</p>
<p><em>Tony</em></p>
 <img src="http://factoryswblog.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=57" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://factoryswblog.org/2008/06/24/case-disappearing-exception/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Write Microsoft .NET code faster with Boo</title>
		<link>http://factoryswblog.org/2007/09/26/write-microsoft-net-code-faster-with-boo/</link>
		<comments>http://factoryswblog.org/2007/09/26/write-microsoft-net-code-faster-with-boo/#comments</comments>
		<pubDate>Thu, 27 Sep 2007 04:23:40 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[.NET Framework Development]]></category>

		<guid isPermaLink="false">http://factoryswblog.org/2007/09/26/write-microsoft-net-code-faster-with-boo/</guid>
		<description><![CDATA[even if you&#8217;re using C# or VB.NET.  How?  By using Boo&#8217;s interpreter to try out ideas, test usage of .NET framework functions, and interact with components (COM, .NET).  Then when you&#8217;re comfortable, you can write the polished code in the language of your choice.
This is one use for mixed language programming, mentioned [...]]]></description>
			<content:encoded><![CDATA[<p>even if you&#8217;re using C# or VB.NET.  How?  By using <a href="http://boo.codehaus.org/">Boo&#8217;s</a> interpreter to try out ideas, test usage of .NET framework functions, and interact with components (COM, .NET).  Then when you&#8217;re comfortable, you can write the polished code in the language of your choice.</p>
<p>This is one use for mixed language programming, mentioned by me <a href="http://factoryswblog.org/2007/06/21/software-development-trends/">here</a>, but where the final software might not use both languages.</p>
<p>In fact, any .NET interpreter such as IronPython or IronRuby could be used.  And it&#8217;s a great technique for other than .NET &#8211; I&#8217;ve used it a lot with Python on Win32, and you can use it in Java with JRuby, Jython, Groovy, etc. This approach could be very useful for embedded and factory software development.</p>
<p>On .NET IronPython and IronRuby have advantages because they&#8217;re official Microsoft languages (and IP has a book coming out &#8211; <a href="http://www.manning.com/foord/">IronPython In Action</a>) and the languages are already in wide use (Win32, *nix, Java).  I&#8217;m using Boo because it comes with the open source <a href="http://sharpdevelop.net/OpenSource/SD/Default.aspx">SharpDevelop IDE</a>, and right now it integrates better with .NET (IP does not support attributes well, and it&#8217;s not easy to make an IP assembly callable by other .NET languages, etc).</p>
<p>Short example &#8211; interactively using DirectoryInfo in Boo (text I typed in bold):</p>
<p>&gt;&gt;&gt; <strong>di = DirectoryInfo(&#8220;C:\\Download&#8221;)</strong><br />
C:\Download<br />
&gt;&gt;&gt; <strong>files = di.GetFiles()</strong><br />
(AdbeRdr80_en_US.exe, AdbeRdr80_en_US_Nosso_error.log, Firefox Setup 2.0.0.1.exe, SharpDevelop_2.2.1.2648_Setup.msi, TortoiseSVN-1.4.3.8645-win32-svn-1.4.3.msi)<br />
&gt;&gt;&gt; <strong>files = di.GetFiles(&#8216;t*&#8217;)</strong><br />
(TortoiseSVN-1.4.3.8645-win32-svn-1.4.3.msi)</p>
<p>Interactively calling a COM object in Boo:</p>
<p>&gt;&gt;&gt; <strong>ieType = System.Type.GetTypeFromProgID(&#8216;InternetExplorer.Application&#8217;)</strong><br />
System.__ComObject<br />
&gt;&gt;&gt;<strong> ie = System.Activator.CreateInstance(ieType)</strong><br />
System.__ComObject<br />
&gt;&gt;&gt; <strong>ie.Visible</strong><br />
false<br />
&gt;&gt;&gt; <strong>ie.Visible = true</strong><br />
true<br />
&gt;&gt;&gt; <strong>ie.Visible</strong><br />
true</p>
<p>So far I haven&#8217;t had to use InvokeMember to call COM functions.  At least for C# and VB.NET (and probably Boo) it appears you need to use it if you are using late binding.  BTW, it is possible to use late binding with COM events, but it is significant extra work.</p>
<p><em>Tony</em></p>
 <img src="http://factoryswblog.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=26" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://factoryswblog.org/2007/09/26/write-microsoft-net-code-faster-with-boo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
