<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Developer.Reflection</title>
	<atom:link href="http://devreflection.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://devreflection.wordpress.com</link>
	<description>Josh Adams' Ramblings and Reflections on Software Development</description>
	<lastBuildDate>Thu, 10 Feb 2011 00:28:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='devreflection.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Developer.Reflection</title>
		<link>http://devreflection.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://devreflection.wordpress.com/osd.xml" title="Developer.Reflection" />
	<atom:link rel='hub' href='http://devreflection.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Caliburn Micro Recipe: Controlling Focus from the ViewModel</title>
		<link>http://devreflection.wordpress.com/2010/10/01/caliburn-micro-recipe-controlling-focus-from-the-viewmodel/</link>
		<comments>http://devreflection.wordpress.com/2010/10/01/caliburn-micro-recipe-controlling-focus-from-the-viewmodel/#comments</comments>
		<pubDate>Fri, 01 Oct 2010 01:20:55 +0000</pubDate>
		<dc:creator>adajos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Caliburn Micro]]></category>
		<category><![CDATA[Focus]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://devreflection.wordpress.com/?p=37</guid>
		<description><![CDATA[ The Common Problem  Setting the focus in the UI is tricky when using MVVM because the ViewModel doesn&#8217;t &#8220;know&#8221; about the View, and certainly doesn&#8217;t have the ability to make calls like &#8220;myView.myControl.Focus().&#8221; In the WPF application I am building with Caliburn Micro I had the need to control the focus from the ViewModel in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=37&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p> <strong>The Common Problem</strong></p>
<p> Setting the focus in the UI is tricky when using MVVM because the ViewModel doesn&#8217;t &#8220;know&#8221; about the View, and certainly doesn&#8217;t have the ability to make calls like &#8220;myView.myControl.Focus().&#8221;</p>
<p>In the WPF application I am building with Caliburn Micro I had the need to control the focus from the ViewModel in a number of places.  For instance, on my login page, if the user entered incorrect credentials and click the &#8220;Login&#8221; button, I needed the ability to set the focus back to the TextBox for the user name. <br />
Additionally when the login page first gets displayed, I needed the focus to be initially set to the user name TextBox to minimize unnecessary mouse clicks and tabbing for the user.  </p>
<p>All of the above issues are demonstrated in the attached sample solution.  (Please note that the sample solution is going against a one-off version of the Caliburn Micro binaries&#8211;I describe the minor changes that I made to the Caliburn source below).</p>
<p> <strong>How to Set the Focus From the ViewModel</strong></p>
<p> So, how can the ViewModel set the focus in the View?  Very simply.  If there is a LoginView with this XAML:</p>
<p>             &lt;TextBox x:Name=Username/&gt;</p>
<p>&#8230;and a LoginViewModel with this property:</p>
<p>            public string Username { get; set;}</p>
<p>&#8230;then simply create a new property of type bool that follows the naming convention Is<strong>MyPropertyName</strong>Focused.  So for the current example you would add</p>
<p>             public bool IsUsernameFocused { get; set;}</p>
<p>If the same LoginViewModel then has a LogIn() method that gets invoked when the user clicks a button named Login, it then has the ability to control when the UIElement bound to the Username property receives focus like so:</p>
<p> public void LogIn()<br />
{<br />
            //just assume login fails for simplicity<br />
            DisplaySomeErrorMessageToUser();<br />
            IsUsernameFocused = true;<br />
            NotifyOfPropertyChange(() =&gt; IsUsernameFocused);<br />
}</p>
<p> I have tested this approach with WPF and it should also work with Silverlight since this &#8220;MVVM focus by convention&#8221; approach works via attached behaviors.  I have no idea whether this will work with WP7 or not.</p>
<p> <strong>Changes to Caliburn Micro Source Code</strong></p>
<p>I can&#8217;t take the credit for inventing the ideas I use to accomplish this.  All I&#8217;ve done is take the clever code of others that I&#8217;ve found online and incorporate them into Caliburn Micro&#8217;s &#8220;Convention Over Configuration&#8221; approach to make this sort of thing easier to do.  The central idea of using a &#8220;FocusExtension&#8221; attached behavior to set a control&#8217;s focus from a ViewModel I got from StackOverflow user &#8220;Anvaka&#8221; <a href="http://stackoverflow.com/questions/1356045/set-focus-on-textbox-in-wpf-from-view-model-c-wpf/1356781#1356781">here</a>.</p>
<p>I did make some minor code changes to Anvaka&#8217;s approach, namely setting the focus on the Dispatcher with a Background priority so that it will still get set even if the control isn&#8217;t loaded when the ViewModel tries to set it and also flipping the focus attached property back to false immediately after it gets set.</p>
<p>Here are the changes I made:</p>
<p><strong>1. Add the FocusExtension class in the Caliburn Micro Silverlight project (with a few preprocessor directives so it will work for WPF also) and then in the Caliburn Micro WPF project pick Add Existing Item and select FocusExtension and click the dropdown arrow on the Add button and choose &#8220;Add as Link&#8221;.</strong></p>
<p> //this great implementation taken from Anvaka&#8217;s answer on StackOverflow here:<br />
 //http://stackoverflow.com/questions/1356045/set-focus-on-textbox-in-wpf-from-view-model-c-wpf/1356781#1356781<br />
    public static class FocusExtension<br />
    {<br />
        public static bool GetIsFocused(DependencyObject obj)<br />
        {<br />
            return (bool)obj.GetValue(IsFocusedProperty);<br />
        }</p>
<p>         public static void SetIsFocused(DependencyObject obj, bool value)<br />
        {<br />
            obj.SetValue(IsFocusedProperty, value);<br />
        }</p>
<p>#if SILVERLIGHT<br />
        public static readonly DependencyProperty IsFocusedProperty =<br />
                DependencyProperty.RegisterAttached(<br />
                 &#8220;IsFocused&#8221;, typeof(bool), typeof(FocusExtension),<br />
                new PropertyMetadata(false, OnIsFocusedPropertyChanged));<br />
#else<br />
        public static readonly DependencyProperty IsFocusedProperty =<br />
         DependencyProperty.RegisterAttached(<br />
          &#8220;IsFocused&#8221;, typeof(bool),  typeof(FocusExtension),<br />
          new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));<br />
#endif<br />
        private static void OnIsFocusedPropertyChanged(DependencyObject d,<br />
                DependencyPropertyChangedEventArgs e)<br />
        {<br />
#if SILVERLIGHT<br />
            var uie = (Control)d;<br />
#else<br />
            var uie = (UIElement)d;<br />
#endif<br />
            if ((bool)e.NewValue)<br />
            {<br />
                uie.Dispatcher.BeginInvoke(new System.Action(delegate<br />
                {<br />
                    uie.Focus(); // Don&#8217;t care about false values.<br />
#if !SILVERLIGHT<br />
                    Keyboard.Focus(uie);<br />
#endif<br />
                    //need to switch it back to false right away so we can detect future change notifications<br />
                    //back to true<br />
                    SetIsFocused(d, false);<br />
#if !SILVERLIGHT<br />
                }), DispatcherPriority.Background, null);<br />
#else<br />
                }), null); //this SHOULD work in Silverlight because I understand that the only DispatcherPriority is &#8220;Background&#8221;<br />
#endif<br />
            }<br />
        }<br />
    }</p>
<p><strong>2.  Add the CheckForFocusProperty method in ViewModelBinder</strong></p>
<p>private static void CheckForFocusProperty(string focusPropertyName, PropertyInfo[] properties, FrameworkElement foundControl)<br />
        {<br />
            if (properties.FirstOrDefault(prop =&gt; prop.Name.Equals(focusPropertyName, StringComparison.InvariantCultureIgnoreCase)) != null)<br />
            {<br />
                ViewModelBinder.SetBindingOnFocusExtensionAttachedBehavior(focusPropertyName, foundControl);<br />
            }<br />
        }</p>
<p><strong>3. Add the new SetBindingOnFocusExtensionAttachedBehavior method in the ViewModelBinder class in the Caliburn Micro Silverlight project</strong></p>
<p>private static void SetBindingOnFocusExtensionAttachedBehavior(string bindingPath, FrameworkElement foundControl)<br />
{<br />
            Binding b = new Binding(bindingPath);<br />
            b.Mode = BindingMode.TwoWay;<br />
            BindingOperations.SetBinding(foundControl, FocusExtension.IsFocusedProperty, b);<br />
}</p>
<p><strong>4. Call the CheckForFocusProperty method from the BindProperties and BindActions methods in ViewModelBinder.</strong></p>
<p> That&#8217;s it.  Now if you build Caliburn Micro and reference the appropriate Caliburn Micro binaries in your WPF or Silverlight project you can start adding boolean properties with the Is<strong>MyPropertyName</strong>Focused convention and then set them to true and raise a property change notification to set focus in the view.</p>
<p><strong>Other Useful Things Shown in the Sample Solution</strong></p>
<p>Have a look at the attached sample solution to see this kind of functionality in action.In addition to setting focus from the view model there are a couple of other helpful things for MVVM developers that are illustrated:</p>
<p>1. using the WPF PasswordBox with MVVM via PasswordBoxHelper attached behavior (not my idea, I got this from Samuel Jack <a href="http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html">here</a>)&#8211;note that is only necessary for the WPF PasswordBox, it is not a problem in SL</p>
<p>2. selecting all the Text in a TextBox when it receives focus via attached behavior (not my idea, I got this from Sergey Aldoukhov <a href="http://stackoverflow.com/questions/660554/how-to-automatically-select-all-text-on-focus-in-wpf-textbox">here</a>)</p>
<p><strong>The Code</strong></p>
<p>Here&#8217;s the source for both the same project and the modified version of Caliburn.Micro.  (I suck at blogging so right click the links and Save Target As&#8230;then rename them from .doc to .zip).<br />
<a href='http://devreflection.files.wordpress.com/2010/10/caliburn-micro-focusexample.doc'>Caliburn.Micro.FocusExample</a><br />
<a href='http://devreflection.files.wordpress.com/2010/10/caliburn-micro-focuschanges.doc'>Caliburn.Micro.FocusChanges</a></p>
<p>Enjoy,</p>
<p>Josh</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devreflection.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devreflection.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devreflection.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devreflection.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devreflection.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devreflection.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devreflection.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devreflection.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devreflection.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devreflection.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devreflection.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devreflection.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devreflection.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devreflection.wordpress.com/37/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=37&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devreflection.wordpress.com/2010/10/01/caliburn-micro-recipe-controlling-focus-from-the-viewmodel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/511ad00ee089323b327d01b6a83a24f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">adajos</media:title>
		</media:content>
	</item>
		<item>
		<title>Favor ViewModels Over Converters</title>
		<link>http://devreflection.wordpress.com/2010/04/06/favor-viewmodels-over-converters/</link>
		<comments>http://devreflection.wordpress.com/2010/04/06/favor-viewmodels-over-converters/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 23:35:54 +0000</pubDate>
		<dc:creator>adajos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://devreflection.wordpress.com/?p=18</guid>
		<description><![CDATA[So in the little MVVM game app I&#8217;m building I need to display the game timer.  First I exposed an Int property on my ViewModel called ElapsedSeconds which did the usual INotifyPropertyChanged stuff.  In my ViewModel, I incremented this Property when my Timer ticked, I also zeroed it out and occassionally added large blocks of time [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=18&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So in the little MVVM game app I&#8217;m building I need to display the game timer.  First I exposed an Int property on my ViewModel called ElapsedSeconds which did the usual INotifyPropertyChanged stuff.  In my ViewModel, I incremented this Property when my Timer ticked, I also zeroed it out and occassionally added large blocks of time to it for penalties when a player used the &#8220;hint&#8221; feature.  </p>
<p>Next, I created a converter which took that integer value and converted it into an appropriate string value to display time in minutes:seconds format, or hours:minutes, depending on how long they were playing the game.  Then in a TextBlock in my View I had a binding to the ElapsedSeconds property on the ViewModel and used my custom converter.</p>
<p>This worked, and I was reasonably happy with it&#8211;I even unit tested my converter and felt good about it. </p>
<p>But in the back of my mind I was still batting around the ideas about using ViewModels instead of converters.  At first I disagreed with that approach. After all, why should the ViewModel care about the display format of their values, which seems to be a purely UI concern?  Furthermore I had unit test coverage of my Converter so I could test my formatting logic.  It didn&#8217;t make sense. </p>
<p>Then, for the heck of it, I decided to try it that way.  I kept my ElapsedSeconds property on the ViewModel and added a property called ElapsedTime of type string.  To keep change notification working for Bindings to this property I raise property changed in the setter of ElapsedSeconds which is the property that the ViewModel uses internally. </p>
<p>The best part is that the functionality to convert from an integer of seconds to a formatted string of &#8220;time&#8221; is now in an extenstion method I wrote for type Int32. The getter for ElapsedTime simply calls that extension method on the member variable which holds the number of elapsed seconds.  The extension method can of course be fully unit tested just like the converter could, but it has requires less code both in the method itself andthe unit test class, because unlike a converter it is typesafe. </p>
<p>So, overall I think I like it better.  There&#8217;s  less &#8220;ceremony code&#8221; than there used to be in the extension method than in the converter (Int32.TryParse() calls with the value passed into the converter, and returning Binding.DoNothing for bogus inputs).  I no longer have to have unit tests that passed in bogus values to the converter to make sure it would react appropriately&#8211;the unit tests can focus on only testing valid cases now since the extension method can only be called on Ints. That alone saved me quite a few lines of code.</p>
<p>Of course there is nothing preventing UI layer developers from building and using their own value converters in their bindings in XAML.  The way I see is that the ViewModel offers up the &#8220;raw&#8221; format (seconds as integers) as well as a fancy format that will work in many UIs so that many UI developers wouldn&#8217;t have to write their own converter.</p>
<p>I still think there may be uses for converters. But I&#8217;m hard-pressed to think of any<em> in which you have the ability to modify the ViewModel source</em>.</p>
<p>At first I was uncomfortable with the idea of  UI formatting or Focus change events (see Josh Smith&#8217;s blog) ocurring in the ViewModel because it seems to not be separating concerns appropriately.  But I think the key is to remember that the ViewModel really is modelling a view.</p>
<p>Anyhow, that&#8217;s what is on my mind tonight.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devreflection.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devreflection.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devreflection.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devreflection.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devreflection.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devreflection.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devreflection.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devreflection.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devreflection.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devreflection.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devreflection.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devreflection.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devreflection.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devreflection.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=18&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devreflection.wordpress.com/2010/04/06/favor-viewmodels-over-converters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/511ad00ee089323b327d01b6a83a24f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">adajos</media:title>
		</media:content>
	</item>
		<item>
		<title>How To Write Untestable Code</title>
		<link>http://devreflection.wordpress.com/2010/03/29/how-to-write-untestable-code/</link>
		<comments>http://devreflection.wordpress.com/2010/03/29/how-to-write-untestable-code/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 00:58:29 +0000</pubDate>
		<dc:creator>adajos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://devreflection.wordpress.com/?p=15</guid>
		<description><![CDATA[So I&#8217;m going to sound off a bit on an awful bit of code I&#8217;ve come across.  I was tasked with adding a feature that involved creating a couple of structs that just hold data (of course) and then displaying that data in a &#8220;pop ussp&#8221; of sorts on a webpage.  The problem was that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=15&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m going to sound off a bit on an awful bit of code I&#8217;ve come across.  I was tasked with adding a feature that involved creating a couple of structs that just hold data (of course) and then displaying that data in a &#8220;pop ussp&#8221; of sorts on a webpage.  The problem was that in order to build up the collection of structs I had to use a massive class that is the central piece of the system.  I will call that class &#8220;R&#8221;.  The R class holds TONS of references to other complex types and it also has some rather complex logic for doing some filtering on those types.  Therefore my &#8220;Builder&#8221; class which needs to build up the structs for display in the popup had to have the R class injected into it.  It needed not only data from R, but also some of R&#8217;s methods.</p>
<p>Because the API was poorly designed my &#8220;Builder&#8221; class needed to iterate and do some complex filtering and conditional logic on 4 different collections off of R.  I carefully designed my Builder to follow the Single Responsibility principle, and thus while the data it returned would feed a UI, it had no UI concerns of any kind within it.  It would be a great bit of code to unit test, especially since it was brand new.</p>
<p>Unfortunately the R class made this impossible.  It implemented no interfaces.  It had 5 constructors and the one with the fewest arguments had 24 different parameters, many  of which were other complex types.  Incidentally only 1 of the many complex types implemented an interface, the others were just like the R class and took in many othe subobjects in their constructor params.  This made unit test setup a downright impossibility. </p>
<p>Some of the objects could literally only be newed up with data from the database. I tried subclassing R with &#8220;TestableR&#8221; so I could pass that into my Builder, but R had no default constructor so my TestableR class would&#8217;ve been required to invoke one of R&#8217;s constructors which left me right back where I started.</p>
<p>Why,  you may ask, did I not extract an interface for R, which would be a non-breaking change? Because in the organization who owns the code this is (for reasons unknown to me) and controversial and politically charged issue.  While that goes unresolved, most new code added to the system, no matter how well designed, is effectively untestable. Put this down as a Daily WTF.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devreflection.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devreflection.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devreflection.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devreflection.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devreflection.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devreflection.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devreflection.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devreflection.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devreflection.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devreflection.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devreflection.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devreflection.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devreflection.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devreflection.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=15&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devreflection.wordpress.com/2010/03/29/how-to-write-untestable-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/511ad00ee089323b327d01b6a83a24f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">adajos</media:title>
		</media:content>
	</item>
		<item>
		<title>Poor (And) Nonstandard Design</title>
		<link>http://devreflection.wordpress.com/2007/06/15/poor-and-nonstandard-design/</link>
		<comments>http://devreflection.wordpress.com/2007/06/15/poor-and-nonstandard-design/#comments</comments>
		<pubDate>Fri, 15 Jun 2007 05:40:15 +0000</pubDate>
		<dc:creator>adajos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://devreflection.wordpress.com/2007/06/15/poor-and-nonstandard-design/</guid>
		<description><![CDATA[I know this isn&#8217;t actually a post about &#8220;software design&#8221; per se, but I think it can be relevant to us as software engineers.  So I live in Phoenix and it&#8217;s June.  This means it is scorchingly hot every day.  We won&#8217;t see high temps below 100 degrees until late September. This means that as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=14&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I know this isn&#8217;t actually a post about &#8220;software design&#8221; per se, but I think it can be relevant to us as software engineers.  So I live in Phoenix and it&#8217;s June.  This means it is scorchingly hot every day.  We won&#8217;t see high temps below 100 degrees until late September.</p>
<p>This means that as someone who tries to stay in decent shape, I have to go the gym and run mindlessly on a treadmill rather than the great outdoors.  For those that use treadmills, you&#8217;ll know that they usually have a &#8220;stop&#8221; button on the main front panel&#8211;the same panel where you typically enter your speed and incline.  Some treadmills also have a special failsafe device that has a cord that is attached to you or your clothing&#8211;the idea being that if you fall off the treadmill it will trip the failsafe mechanism and instantly stop moving.  The photo <a target="_blank" href="http://www.usedgymequipment.com/treadmills/lifefitness_ti.jpg">here</a> is typical of a treadmill and it&#8217;s red stop button.</p>
<p>The other day the gym was busy and I had to use an older cheaper treadmill in the back row, far from the TVs at the front.  It had a unique &#8220;feature&#8221;. </p>
<p>It&#8217;s &#8220;Stop&#8221; button was located in the center on the left handrailing of the treadmill.  It&#8217;s a non-standard design.  Being non-standard in and of itself isn&#8217;t bad&#8211;especially if it improves on the design flaws of the standard design.  However, this design has a uniquely annoying design flaw.  See if you can think of it on your own before highlighting the blank area below to see the answer:</p>
<p><font color="#ffffff">A runner can easily bump the stop button inadvertantly if they lower their arms a bit while running.  This happened to me twice, and I didn&#8217;t appreciate having my run stopped just as I was getting into the rhythm.  In fact, I cannot think of any advantage at all&#8211;from a usability perspective&#8211;to having the button on the railing. Maybe it&#8217;s cheaper or easier to manufacture.</font></p>
<p>How does this apply to software design?  A developer can certainly feel free to innovate and think of &#8220;out of the box&#8221; solutions (after all, if we didn&#8217;t try news things we&#8217;d all still be doing FORTRAN)&#8211;but they need to be sure that they aren&#8217;t being different just for the sake of originality&#8211;and it must absolutely be USABLE.  There needs to be a meaningful advantage to implementing an off-beat solution.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/devreflection.wordpress.com/14/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/devreflection.wordpress.com/14/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devreflection.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devreflection.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devreflection.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devreflection.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devreflection.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devreflection.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devreflection.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devreflection.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devreflection.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devreflection.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devreflection.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devreflection.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devreflection.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devreflection.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=14&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devreflection.wordpress.com/2007/06/15/poor-and-nonstandard-design/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/511ad00ee089323b327d01b6a83a24f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">adajos</media:title>
		</media:content>
	</item>
		<item>
		<title>Cyclomatic Complexity and Refactoring</title>
		<link>http://devreflection.wordpress.com/2007/06/11/cyclomatic-complexity-and-refactoring/</link>
		<comments>http://devreflection.wordpress.com/2007/06/11/cyclomatic-complexity-and-refactoring/#comments</comments>
		<pubDate>Mon, 11 Jun 2007 19:32:28 +0000</pubDate>
		<dc:creator>adajos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://devreflection.wordpress.com/2007/06/11/cyclomatic-complexity-and-refactoring/</guid>
		<description><![CDATA[For those of you not familiar with Cyclomatic Complexity as a code metric, it&#8217;s basically a number which expresses &#8220;the number of linearly independent paths through a program&#8217;s source code.&#8221;  As outlined in this article, you can estimate it by: Start with 1 for a straight path through the routine. Add 1 for each of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=12&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For those of you not familiar with <a target="_blank" href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">Cyclomatic Comple</a>xity as a code metric, it&#8217;s basically a number which expresses &#8220;the number of linearly independent paths through a program&#8217;s source code.&#8221;  As outlined in this <a target="_blank" href="http://www.codeproject.com/dotnet/Cyclomatic_Complexity.asp">article</a>, you can estimate it by:</p>
<ul>
<li>Start with 1 for a straight path through the routine.</li>
<li>Add 1 for each of the following keywords or their equivalent: <code>if</code>, <code>while</code>, <code>repeat</code>, <code>for</code>, <code>and</code>, <code>or</code>.</li>
<li>Add 1 for each <code>case</code> in a <code>switch</code> statement.</li>
</ul>
<p>For me, the most interesting thing about cyclomatic complexity is that it can point you to areas in your app that need to be refactored&#8211;high cyclomatic complexity numbers indicate methods and classes that need to be extracted.  The higher the complexity, the harder to change the code later and the harder to find and fix bugs in the code.</p>
<p>Raymond Lewallen made a <a target="_blank" href="http://codebetter.com/blogs/raymond.lewallen/archive/2005/06/13/64527.aspx">blog post</a> about his personal thresholds for cyclomatic complexity and I agree with him.  Jeff Atwood also had a great blog post about &#8220;<a target="_blank" href="http://www.codinghorror.com/blog/archives/000486.html">Flattening Arrow Code&#8221;</a> to reduce cyclomatic complexity.  After reading these posts, I was inspired to see how my Micro-ISV app is doing in this area.</p>
<p>So I ran Reflector with the <a target="_blank" href="http://www.codeplex.com/reflectoraddins">CodeMetrics Add-In</a> against my assemblies.  The results were interesting&#8212;most of my classes were below 10, quite a few were in the teens and low 20s (good enough for me for the time being since all my code has unit tests) and 2 classes had very high complexity.  Overall, I was pretty happy with this, because most of the code is pretty well refactored.</p>
<p>One class had a cyclomatic complexity of 36.  This class is the most frequently-used class in the API I&#8217;m building and has lots of different pieces of information as it is basically an html node.  Thus it stores information about node name, type, attributes, inner text, parent node, additional nodes contained within it, etc.  It also knows how to run run XPath queries against itself.  There aren&#8217;t too many actual conditionals or loops in it, and it doesn&#8217;t contain any nested if&#8217;s/whiles, etc&#8211;it just has alot of methods.  I&#8217;ll have to see if I can extract some of them to another class&#8211;I want to make sure my class follows the Single Responsibility Principle.</p>
<p>The other class with a high cyclomatic complexity number of 43! Ugh.  This didn&#8217;t surprise me however.  It is my implementation of Microsoft&#8217;s XPathNavigator class, which is where the real processing of XPath queries happens.  It&#8217;s an internal class as consumers of my API don&#8217;t need to know about it&#8217;s existence.  Unfortunately there&#8217;s just a TON of methods in it&#8211;and again, none of them have deeply nested conditionals, but most of them have at least 1-3 conditionals.  The abstract System.Xml.XPath.XPathNavigator class has lots of abstract methods defined on it so there&#8217;s not much I can do about that.  I could factor out some of what happens inside these methods into other classes.  I&#8217;ll have to think about whether or not that is worth pursuing.  Any reader thoughts?</p>
<p>Because my Navigator implementation is internal, I chose not to write any unit tests against it even though I could have&#8211;I typically mark my unit test project as a friend assembly so I can test internal methods if I need to.  In this case though, I have quite a few unit tests (and some separate system tests) that verify that XPath queries are returning the expected results.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/devreflection.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/devreflection.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devreflection.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devreflection.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devreflection.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devreflection.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devreflection.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devreflection.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devreflection.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devreflection.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devreflection.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devreflection.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devreflection.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devreflection.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devreflection.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devreflection.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=12&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devreflection.wordpress.com/2007/06/11/cyclomatic-complexity-and-refactoring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/511ad00ee089323b327d01b6a83a24f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">adajos</media:title>
		</media:content>
	</item>
		<item>
		<title>Design Patterns and OO Principles</title>
		<link>http://devreflection.wordpress.com/2007/06/07/design-patterns-and-oo-principles/</link>
		<comments>http://devreflection.wordpress.com/2007/06/07/design-patterns-and-oo-principles/#comments</comments>
		<pubDate>Thu, 07 Jun 2007 15:45:21 +0000</pubDate>
		<dc:creator>adajos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://devreflection.wordpress.com/2007/06/07/design-patterns-and-oo-principles/</guid>
		<description><![CDATA[So I see alot of value in design patterns in software development.  It just intuitively makes sense that as developers we run into similar problems over and over again which have similar solutions or patterns.  For those not familiar with design patterns, I&#8217;d suggest Heads-First Design Patterns as a good introduction to them&#8212;it has a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=10&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So I see alot of value in design patterns in software development.  It just intuitively makes sense that as developers we run into similar problems over and over again which have similar solutions or patterns.  For those not familiar with design patterns, I&#8217;d suggest <a target="_blank" href="http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124/ref=pd_bbs_sr_1/105-3117730-7766806?ie=UTF8&amp;s=books&amp;qid=1181229956&amp;sr=8-1">Heads-First Design Patterns</a> as a good introduction to them&#8212;it has a Java focus, but most of what it talks about is just good object-oriented programming.</p>
<p>However, I have definitely seen the over-use of patterns or the applying of patterns too soon.  This tends to create un-needed flexibility and un-needed <a target="_blank" href="http://en.wikipedia.org/wiki/Coupling_%28computer_science%29">loose coupling</a>.  Basically code that won&#8217;t change alot is abstracted and made very easy to change.   Clearly work is wasted when this happens.</p>
<p>My take is that often patterns are most useful as a language for communicating a design with other developers.  Sure, sometimes you can identify the need for a design pattern just from requirements rather than from code, but often you guess wrong if you try to do that.</p>
<p>My preferred approach is interative Test-Driven Development.  Along the way I incrementally follow good object-oriented design principles ensuring that I don&#8217;t introduce abstraction until I need it.  This means interfaces and abstract classes usually aren&#8217;t created until a refactoring step.  Oftentimes while doing this I will find that by virtue of following these OO concepts I have produced a solution that is a <a target="_blank" href="http://www.dofactory.com/Patterns/Patterns.aspx">design pattern</a>.</p>
<p> What basic OO principles am I referring to?</p>
<p>1.   <a target="_blank" href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">The DRY (Don&#8217;t Repeat Yourself) Principle</a> </p>
<p>2.  <a target="_blank" href="http://c2.com/cgi/wiki?OpenClosedPrinciple">The Open/Closed Principle</a></p>
<p>3.  <a target="_blank" href="http://c2.com/cgi/wiki?DependencyInversionPrinciple">The Dependency Inversion Principle</a></p>
<p>4.  <a target="_blank" href="http://c2.com/cgi/wiki?LawOfDemeter">The Law of Demeter</a></p>
<p>5.  <a target="_blank" href="http://www.artima.com/lejava/articles/designprinciples4.html">Favor Composition Over Inheritance</a></p>
<p>6.  <a target="_blank" href="http://en.wikipedia.org/wiki/Separation_of_concerns">Separation of Concerns</a></p>
<p>7.  <a target="_blank" href="http://c2.com/cgi/wiki?SingleResponsibilityPrinciple">Single Responsibility Principle</a></p>
<p>This basic approach to development, unit-testing, and design that I use is my interpretation of what the author of <a target="_blank" href="http://www.amazon.com/Refactoring-Patterns-Addison-Wesley-Signature-Kerievsky/dp/0321213351/ref=pd_bbs_sr_1/105-3117730-7766806?ie=UTF8&amp;s=books&amp;qid=1181230558&amp;sr=1-1">Refactoring to Patterns </a>was saying.</p>
<p>I plan on doing a series of additional posts talking about each of the above principles in turn.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/devreflection.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/devreflection.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devreflection.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devreflection.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devreflection.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devreflection.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devreflection.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devreflection.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devreflection.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devreflection.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devreflection.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devreflection.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devreflection.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devreflection.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devreflection.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devreflection.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=10&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devreflection.wordpress.com/2007/06/07/design-patterns-and-oo-principles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/511ad00ee089323b327d01b6a83a24f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">adajos</media:title>
		</media:content>
	</item>
		<item>
		<title>Software Entropy and Broken Window Theory</title>
		<link>http://devreflection.wordpress.com/2007/06/06/software-entropy-and-broken-window-theory/</link>
		<comments>http://devreflection.wordpress.com/2007/06/06/software-entropy-and-broken-window-theory/#comments</comments>
		<pubDate>Wed, 06 Jun 2007 16:34:04 +0000</pubDate>
		<dc:creator>adajos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://devreflection.wordpress.com/2007/06/06/software-entropy-and-broken-window-theory/</guid>
		<description><![CDATA[If you write code for a living or a hobby you need to read &#8220;The Pragmatic Programmer&#8220;.  I don&#8217;t care if you write in COBOL&#8211;it&#8217;s info that is great for anyone.  It has lots of short, self-contained chapters that can be read in 5-15 minutes.  They are all very valuable and emphasize the theme of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=9&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you write code for a living or a hobby you need to read &#8220;<a target="_blank" href="http://www.amazon.com/Pragmatic-Programmer-Journeyman-Master/dp/020161622X/ref=pd_bbs_sr_2/105-3117730-7766806?ie=UTF8&amp;s=books&amp;qid=1181147119&amp;sr=8-2">The Pragmatic Programmer</a>&#8220;.  I don&#8217;t care if you write in COBOL&#8211;it&#8217;s info that is great for anyone.  It has lots of short, self-contained chapters that can be read in 5-15 minutes.  They are all very valuable and emphasize the theme of the programmer as a craftsman.</p>
<p>One of their more interesting chapters talks about<a target="_blank" href="http://www.pragmaticprogrammer.com/ppbook/extracts/no_broken_windows.html"> Software Entropy and Broken Window Theory</a>.  They basically liken a codebase to an inner city building.  If a window is broken and left unfixed, people stop caring and before you know it there is trash everywhere, graffiti, grass growing through cracks in the sidewalk and you have a great deal of disorder (AKA entropy). </p>
<p>The same is true of code.  Perhaps you just put in some crappy hard-to-read code just to &#8220;get it done&#8221; for a looming deadline.  Perhaps you have some code that &#8220;works&#8221; even though you don&#8217;t know how or why it works.  These are broken windows.  They need to be fixed ASAP, or at least marked as broken so they can be fixed as quickly as possible.  Failure to fix these broken windows has a big psychological impact&#8212;I guarantee that before you know it you&#8217;ll be thinking &#8220;this code is crap already, I&#8217;ll just tack on another quick and dirty solution.&#8221;</p>
<p>Before you know it, you have code entropy&#8211;code so unreadable, convoluted and mysterious that you can longer change it without breaking the system altogether.  Then the system dies a slow death or has to be rewritten from scratch.  I know I&#8217;ve been there, which is why I&#8217;ve learned to fix all broken windows.</p>
<p> That said, you&#8217;ve still got to balance the principle of fixing windows ASAP with the idea the &#8220;perfect is the enemy of good&#8221;, which is another principle the Pragmatic Progammers address (thought they don&#8217;t have that excerpt from the book online). </p>
<p>The Pragamtic Progammer should be required reading for all Computer Science students&#8211;as you can see it covers psychological issues related to progamming in addition to more &#8220;hard&#8221; topics like orthogonality, cohesion, coupling, automation, planning, etc.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/devreflection.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/devreflection.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devreflection.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devreflection.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devreflection.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devreflection.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devreflection.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devreflection.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devreflection.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devreflection.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devreflection.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devreflection.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devreflection.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devreflection.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devreflection.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devreflection.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=9&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devreflection.wordpress.com/2007/06/06/software-entropy-and-broken-window-theory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/511ad00ee089323b327d01b6a83a24f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">adajos</media:title>
		</media:content>
	</item>
		<item>
		<title>The Value of Rapid Prototyping</title>
		<link>http://devreflection.wordpress.com/2007/06/06/the-value-of-rapid-prototyping/</link>
		<comments>http://devreflection.wordpress.com/2007/06/06/the-value-of-rapid-prototyping/#comments</comments>
		<pubDate>Wed, 06 Jun 2007 16:21:46 +0000</pubDate>
		<dc:creator>adajos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://devreflection.wordpress.com/2007/06/06/the-value-of-rapid-prototyping/</guid>
		<description><![CDATA[First of all, let me state from the beginning that I&#8217;m a big proponent of Agile practices.  All of my code is developed with Test-Driven Development.  I practice continuous design rather than Big Design Up Front&#8211;I try to put off design decisions until the last responsible moment.  I constantly refactor.  I&#8217;ve found scrumming, collective code ownership, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=7&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>First of all, let me state from the beginning that I&#8217;m a big proponent of Agile practices.  All of my code is developed with Test-Driven Development.  I practice continuous design rather than Big Design Up Front&#8211;I try to put off design decisions until the last responsible moment.  I constantly refactor.  I&#8217;ve found scrumming, collective code ownership, iterative development, and pair programming to be indispensible.</p>
<p> That said, I think that the not-terribly-Agile practice of <a target="_blank" href="http://en.wikipedia.org/wiki/Prototyping">Rapid Prototyping </a>it too often overlooked. In my current app development I did a Rapid Prototype that is proving to be very useful&#8211;my only regret is that I didn&#8217;t do it sooner as it could have saved me some serious work. </p>
<p>For instance, several times lately I&#8217;ve been doing some UI code with the <a target="_blank" href="http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/default.aspx">Model-View-Presenter pattern</a>.  I start by test driving my Presenter and <a target="_blank" href="http://en.wikipedia.org/wiki/Mock_object">mocking out </a>of the View&#8211;deferring presentation layer decision until later.  Unfortunately several times I have not considered the necessary things that the View will need in order to do some presentation specific things&#8211;such as highlighting specific strings or nodes.</p>
<p>I then stepped back and did some rapid prototyping around this screen (yielding the wrapped TreeView solution spelled out in my previous post) and found it to be very helpful.  Armed with new knowledge about what my View would really need to do its job I was able to continue to test drive my actualy production code and make it work nice and pretty cleanly. </p>
<p> My first regret was not starting my rapid prototype sooner as it would&#8217;ve kept me from wasting time on throwaway work the first time around.  My second regret was not adding one final bit of functionality to my rapid prototype which is now causing me some similar problems in my production code.</p>
<p>If any readers have any thoughts about when to use Rapid Prototyping and how best to combine it with Agile development processes, I&#8217;d be interested to hear it.  For myself, I&#8217;m beginning to realize that it can be really helpful with MVP UI development.  I guess my rapid prototyping is on a very small scale&#8212;mostly just a screen-by-screen basis rather than building a complete prototyped system.</p>
<p>For those interested in learning more about the Model-View Presenter design pattern in .NET and why you&#8217;d want to use it check out Jeremy Miller&#8217;s excellent series on it at <a target="_blank" href="http://codebetter.com/blogs/jeremy.miller/archive/2007/06/05/build-your-own-cab-part-7-what-s-the-model.aspx">CodeBetter.com</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/devreflection.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/devreflection.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devreflection.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devreflection.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devreflection.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devreflection.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devreflection.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devreflection.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devreflection.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devreflection.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devreflection.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devreflection.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devreflection.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devreflection.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devreflection.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devreflection.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=7&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devreflection.wordpress.com/2007/06/06/the-value-of-rapid-prototyping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/511ad00ee089323b327d01b6a83a24f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">adajos</media:title>
		</media:content>
	</item>
		<item>
		<title>TreeView Complaints</title>
		<link>http://devreflection.wordpress.com/2007/06/06/treeview-complaints/</link>
		<comments>http://devreflection.wordpress.com/2007/06/06/treeview-complaints/#comments</comments>
		<pubDate>Wed, 06 Jun 2007 16:06:26 +0000</pubDate>
		<dc:creator>adajos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://devreflection.wordpress.com/2007/06/06/treeview-complaints/</guid>
		<description><![CDATA[Ok, so my app heavily uses the System.Windows.Forms.TreeView control that comes out of the box with the .NET Framwork 2.0.  I&#8217;m finding it to be insufficient.  Perhaps it&#8217;s just my small mind and misuse of the control and if so, nail me on the comments.  Without further adieu, here are my gripes: 1.  No MultiSelect [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=8&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ok, so my app heavily uses the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.treeview.aspx">System.Windows.Forms.TreeView </a>control that comes out of the box with the .NET Framwork 2.0.  I&#8217;m finding it to be insufficient.  Perhaps it&#8217;s just my small mind and misuse of the control and if so, nail me on the comments.  Without further adieu, here are my gripes:</p>
<p>1.  No MultiSelect functionality built in.</p>
<p>My app requires this kind of functionality, and while it&#8217;s certainly possible to add it, I&#8217;d rather not have to.  Seems like it should come with it.  Expecially if you consider the TreeViews used in Windows Explorer which allow you to select multiple folder nodes.  Am I asking for too much here?</p>
<p>2.  No ability to populate the TreeView by binding directly to an XML Document.</p>
<p>I know that the web version of the TreeView control supports that kind of functionality out of the box.  Instead I have to recurse through by object graph and build up an object graph of <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.treenode.aspx">TreeNodes</a> inside of <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.treenodecollection.aspx">TreeNodeCollections </a>that mirrors my object graph structure.  Not a huge deal I guess.</p>
<p>3.  Why, oh why did Microsoft make the constructor of TreeNodeCollection internal rather than public?</p>
<p>Quick primer on the way the TreeView works.  The TreeView control has a TreeNodeCollection which contains TreeNodes.  Each TreeNode can also have it&#8217;s own TreeNodeCollection.  In my app, I needed a way to associate each TreeNode with an XMLNode.  The idea being that I wanted to be able to have the user input an XPath query and then have the TreeView select the node that the query returns. </p>
<p>Rather than come up with some lame way to map back and forth between TreeNodes and XmlNodes I decided to extend the TreeView control.  So now I have XmlTreeView and XmlTreeNodeCollection and XmlTreeNode.  They inherit from the out of the box objects and in fact the only real new functionality is that XmlTreeNode now has a public XmlNode property.  That allows me to recurse through all the XmlTreeNodes in the XmlTreeView until I find a node that contains the XmlNode that was returned by the XPath query.  Then I just say something like XmlTreeView.SelectedNode = currentXmlTreeNode. </p>
<p> XmlTreeNodeCollection and XmlTreeNode both wrap their corresponding base objects and the XmlTreeNodeCollection keeps itself in synch with it&#8217;s hidden TreeNode collection so that I had to reimplement as little of the TreeNode functionality as possible so that the wrapped TreeView control could still act upon the objects it expects in order to manage its state and such.</p>
<p> Pretty simple right?  For whatever reason though, my task in extending those objects was made more difficult and less unit testable by the fact that the only constructor for TreeNodeCollection is internal.  This meant that I either had to do what Microsoft did&#8211;when a TreeNode is created it then calls the internal contructor on TreeNodeCollection (same goes for the creation of a TreeView)&#8212;or I could make my XmlTreeNodeCollection constructor public but then have it create a temporary TreeNode just for the purpose of initializing it&#8217;s wrapped TreeNodeCollection or I had to use reflection to call it&#8217;s internal constructor.</p>
<p>I ultimately went with option 1&#8211;having an XmlTreeNodeCollection with an internal constructor.  It means that in my unit test class I have to create a throwaway XmlTreeNode and grab it&#8217;s Nodes collection in order to initialize my XmlTreeNodeCollection.  Not thrilled with it, but it works.</p>
<p>I&#8217;m totally in favor of OOP&#8211;which means I believe in encapsulation and information hiding.  However I found this design choice to be annoying.  I&#8217;ve made my share of internal classes and methods, and even internal constructors&#8212;but this really cramped my style.</p>
<p> Let me know what you think about this.  Maybe that&#8217;s what I get for trying to use their objects in a  way they never intended.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/devreflection.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/devreflection.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devreflection.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devreflection.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devreflection.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devreflection.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devreflection.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devreflection.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devreflection.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devreflection.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devreflection.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devreflection.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devreflection.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devreflection.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devreflection.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devreflection.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devreflection.wordpress.com&amp;blog=1140079&amp;post=8&amp;subd=devreflection&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devreflection.wordpress.com/2007/06/06/treeview-complaints/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/511ad00ee089323b327d01b6a83a24f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">adajos</media:title>
		</media:content>
	</item>
	</channel>
</rss>
