<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Dennis being all subjective again</title><link>http://blogs.msdn.com/b/dennisg/</link><description>A no-nonsense take on programming and technology</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>The Challenges of Testability: State</title><link>http://blogs.msdn.com/b/dennisg/archive/2012/06/22/the-challenges-of-testability-state.aspx</link><pubDate>Fri, 22 Jun 2012 07:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10303990</guid><dc:creator>Dennis Gurzhii</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/rsscomments.aspx?WeblogPostID=10303990</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/commentapi.aspx?WeblogPostID=10303990</wfw:comment><comments>http://blogs.msdn.com/b/dennisg/archive/2012/06/22/the-challenges-of-testability-state.aspx#comments</comments><description>&lt;p style="text-align: justify;"&gt;In the &lt;a href="http://blogs.msdn.com/b/dennisg/archive/2012/05/06/the-challenges-of-testability-monolith.aspx"&gt;previous article&lt;/a&gt; of the series, we have explored the problems of testing a monolithic component and how said problems can be mitigated. This time let's take on another issue that often stands in the way of testing: maintaining a state.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;As far as a software component is concerned, state comes in two kinds:&lt;/p&gt;
&lt;ul style="text-align: justify;"&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Internal state&lt;/em&gt;&amp;nbsp;is represented by mutable data values that are encapsulated in the component. For instance, if a component is in fact a class, which is by far the most popular case, its internal state is typically stored in non-constant member variables.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;External state&lt;/em&gt; is constituted by conditions and values that, while not being part of the component, exert influence on its behavior. One case of external state is variables in the same scope or above the component itself, such as global variables for instance. Another, slightly more complicated example of external state is a global function that returns a dynamic value, such as &lt;span style="font-family: 'courier new', courier;"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408(v=vs.85).aspx"&gt;GetTickCount()&lt;/a&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="text-align: justify;"&gt;Consider this contrived example from a video game:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white; margin-left: 20px; border-left: thin solid #000000; padding-left: 10px;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; &lt;span style="color: #020002;"&gt;LevelRules&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;extern&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;monsterSpeed&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;struct&lt;/span&gt; &lt;span style="color: #020002;"&gt;Position&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Position&lt;/span&gt;() : &lt;span style="color: #020002;"&gt;x&lt;/span&gt;(-1), &lt;span style="color: #020002;"&gt;y&lt;/span&gt;(-1), &lt;span style="color: #020002;"&gt;z&lt;/span&gt;(-1) { }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;x&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;y&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;z&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;IGameObject&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; ~&lt;span style="color: #020002;"&gt;IGameObject&lt;/span&gt;() { }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Will be called from the game loop on each iteration&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// to update the object.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;DoStep&lt;/span&gt;() = 0;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;Player&lt;/span&gt;; &lt;span style="color: green;"&gt;// defined elsewhere&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;Monster&lt;/span&gt; : &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: #020002;"&gt;IGameObject&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;Move&lt;/span&gt;(&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;Position&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;position&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;Attack&lt;/span&gt;(&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;Player&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;player&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;DoStep&lt;/span&gt;()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;DWORD&lt;/span&gt; &lt;span style="color: #020002;"&gt;currentStep&lt;/span&gt; = &lt;span style="color: #020002;"&gt;GetTickCount&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;DWORD&lt;/span&gt; &lt;span style="color: #020002;"&gt;deltaMs&lt;/span&gt; = &lt;span style="color: #020002;"&gt;currentStep&lt;/span&gt; - &lt;span style="color: #020002;"&gt;_previousStep&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Change _currentPosition (depending on LevelRules::mosterSpeed)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// and _animationPhase according to how many milliseconds&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// have passed since the previous step (deltaMs).&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;_previousStep&lt;/span&gt; = &lt;span style="color: #020002;"&gt;currentStep&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;struct&lt;/span&gt; &lt;span style="color: #020002;"&gt;Order&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;enum&lt;/span&gt; &lt;span style="color: #020002;"&gt;Action&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Action_None&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Action_Move&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Action_Attack&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Order&lt;/span&gt;() :&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;action&lt;/span&gt;(&lt;span style="color: #020002;"&gt;Action_None&lt;/span&gt;),&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;pTargetPosition&lt;/span&gt;(),&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;pTargetPlayer&lt;/span&gt;() { }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Action&lt;/span&gt; &lt;span style="color: #020002;"&gt;action&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Position&lt;/span&gt;* &lt;span style="color: #020002;"&gt;pTargetPosition&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Player&lt;/span&gt;* &lt;span style="color: #020002;"&gt;pTargetPlayer&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Position&lt;/span&gt; &lt;span style="color: #020002;"&gt;_currentPosition&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Order&lt;/span&gt; &lt;span style="color: #020002;"&gt;_currentOrder&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;_animationPhase&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;DWORD&lt;/span&gt; &lt;span style="color: #020002;"&gt;_previousStep&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;With the current design, it's going to be a pain to unit test the &lt;span style="font-family: 'courier new', courier;"&gt;Monster&lt;/span&gt; class. Let's see what concrete problems we can identify and fix.&lt;/p&gt;
&lt;h3 style="text-align: justify;"&gt;External state&lt;/h3&gt;
&lt;p style="text-align: justify;"&gt;This is usually the nasty kind. In this case, external state relative to the&amp;nbsp;&lt;span&gt;Monster&lt;/span&gt;&amp;nbsp;class&amp;nbsp;is represented by:&lt;/p&gt;
&lt;ul style="text-align: justify;"&gt;
&lt;li&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier;"&gt;LevelRules::monsterSpeed&lt;/span&gt;&amp;nbsp;is a configuration variable that looks innocent enough and probably changes every once in a while when the user advances to a new game level. As with any other global or static value, our main concern is that it's shared by all threads, meaning that running several unit tests in parallel becomes impossible. Since monster objects most likely don't carry over from one level to another, it seems like a perfectly safe alternative to make it a constructor argument for &lt;span style="font-family: 'courier new', courier;"&gt;Monster&lt;/span&gt;&amp;nbsp;instead of a global.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier;"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408(v=vs.85).aspx"&gt;GetTickCount&lt;/a&gt;&lt;/span&gt;&amp;nbsp;is a Win32 API often used to measure time spans with average accuracy. The problem here is that for unit testing purposes we need to be able to simulate the flow of time so that game objects can be tested faster than in realtime. Indeed, we want our tests to run as quickly as possible, preferably in a matter of seconds, which is orders of magnitude faster than how the game pace would appear to a human player. Defining an abstract interface for time measurement and providing that to the &lt;span style="font-family: 'courier new', courier;"&gt;Monster&lt;/span&gt; class would fix the problem entirely, with unit tests being capable of returning fabricated time stamps while the real game code still implicitly relies on &lt;span style="font-family: 'courier new', courier;"&gt;GetTickCount&lt;/span&gt; for the underlying implementation. Depending on the semantics of &lt;span style="font-family: 'courier new', courier;"&gt;Monster::DoStep&lt;/span&gt;, it may instead be possible to make the timestamp delta, deltaMs, into an argument of&amp;nbsp;&lt;span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;Monster::DoStep&lt;/span&gt;, which would simplify testing even further.&lt;/span&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style="text-align: justify;"&gt;Internal state&lt;/h3&gt;
&lt;p style="text-align: justify;"&gt;The internal state of the &lt;span style="font-family: 'courier new', courier;"&gt;Monster&lt;/span&gt; class is clearly represented by its data members. The class changes these values as orders are being issued and time passes, but what we want for testing is to be able to easily and momentarily transition a monster object to any kind of conceivable valid state to test various scenarios. As it is, in order to arrive at a certain known hypothetical state, any number of non-trivial manipulations may need to be performed on a monster object, and that would complicate the unit testing code to such an extent as to make it almost not worth it.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;The solution? A variation on what is known in the industry as&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Memento_pattern"&gt;the memento pattern&lt;/a&gt;. If we were to provide a safe way for a monster to dump its internal state to an externally accessible data structure and to be able to initialize itself from that state object when required, we'd have a perfect way to manipulate a monster's internal state from our test code. Making it safe for production code, so that it doesn't accidentally mess with a monster object's internal state, is a little bit of a challenge though, so stay tuned.&lt;/p&gt;
&lt;h3 style="text-align: justify;"&gt;How all the pieces fit together&lt;/h3&gt;
&lt;p style="text-align: justify;"&gt;Considering the analysis above, here's what a unit testing friendly version of the code in question might look like:&lt;/p&gt;
&lt;div style="font-family: 'Courier New'; font-size: 10pt; background-color: white; margin-left: 20px; border-left-width: thin; border-left-style: solid; border-left-color: #000000; padding-left: 10px; background-position: initial initial; background-repeat: initial initial;"&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: blue;"&gt;struct&lt;/span&gt; &lt;span style="color: #020002;"&gt;ITimer&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;virtual&lt;/span&gt; ~ITimer() { }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span color="#0000ff" style="color: #0000ff;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: #0000ff;"&gt;unsigned long long&lt;/span&gt;&amp;nbsp;&lt;span style="color: #020002;"&gt;GetTimeStamp() = 0&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: blue;"&gt;struct&lt;/span&gt; &lt;span style="color: #020002;"&gt;Position&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Position&lt;/span&gt;() : &lt;span style="color: #020002;"&gt;x&lt;/span&gt;(-1), &lt;span style="color: #020002;"&gt;y&lt;/span&gt;(-1), &lt;span style="color: #020002;"&gt;z&lt;/span&gt;(-1) { }&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;x&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;y&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;z&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;IGameObject&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; ~&lt;span style="color: #020002;"&gt;IGameObject&lt;/span&gt;() { }&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Will be called from the game loop on each iteration&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// to update the object.&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;DoStep&lt;/span&gt;() = 0;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;Player&lt;/span&gt;; &lt;span style="color: green;"&gt;// defined elsewhere&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;Monster&lt;/span&gt; : &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: #020002;"&gt;IGameObject&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; Monster(&lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;speed,&amp;nbsp;&lt;span&gt;ITimer* pTimer&lt;/span&gt;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;Move&lt;/span&gt;(&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;Position&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;position&lt;/span&gt;);&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;Attack&lt;/span&gt;(&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;Player&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;player&lt;/span&gt;);&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;DoStep&lt;/span&gt;()&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;DWORD&lt;/span&gt; &lt;span style="color: #020002;"&gt;currentStep&lt;/span&gt; = &lt;span&gt;_pTimer-&amp;gt;&lt;/span&gt;&lt;span style="color: #020002;"&gt;GetTimeStamp&lt;/span&gt;();&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;DWORD&lt;/span&gt; &lt;span style="color: #020002;"&gt;deltaMs&lt;/span&gt; = &lt;span style="color: #020002;"&gt;currentStep&lt;/span&gt; - &lt;span style="color: #020002;"&gt;_previousStep&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Change _currentPosition (depending on LevelRules::mosterSpeed)&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// and _animationPhase according to how many milliseconds&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// have passed since the previous step (deltaMs).&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;_previousStep&lt;/span&gt; = &lt;span style="color: #020002;"&gt;currentStep&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;struct&lt;/span&gt; &lt;span style="color: #020002;"&gt;Order&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;enum&lt;/span&gt; &lt;span style="color: #020002;"&gt;Action&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Action_None&lt;/span&gt;,&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Action_Move&lt;/span&gt;,&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Action_Attack&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Order&lt;/span&gt;() :&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;action&lt;/span&gt;(&lt;span style="color: #020002;"&gt;Action_None&lt;/span&gt;),&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;pTargetPosition&lt;/span&gt;(),&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;pTargetPlayer&lt;/span&gt;() { }&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Action&lt;/span&gt; &lt;span style="color: #020002;"&gt;action&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Position&lt;/span&gt;* &lt;span style="color: #020002;"&gt;pTargetPosition&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Player&lt;/span&gt;* &lt;span style="color: #020002;"&gt;pTargetPlayer&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;struct&lt;/span&gt; State&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; {&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span&gt;State();&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span&gt;Position&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;_currentPosition&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span&gt;Order&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;_currentOrder&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;int&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;_animationPhase&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span&gt;DWORD&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;_previousStep&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; };&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;friend class&lt;/span&gt; MonsterManipulator;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; GetState(State&amp;amp; state) &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;bool&lt;/span&gt; SetState(&lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; State&amp;amp; state);&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span color="#020002" style="color: #020002;"&gt;ITimer* _pTimer&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;_speed&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;State&lt;/span&gt; &lt;span style="color: #020002;"&gt;_state&lt;/span&gt;;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: #008000;"&gt;// This class is only defined in the unit testing code,&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: #008000;"&gt;// not in production code.&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: #0000ff;"&gt;class&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;&lt;span&gt;MonsterManipulator&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span style="color: #0000ff;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;static&amp;nbsp;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;&lt;span&gt;&amp;nbsp;GetMonsterState(&lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; Monster&amp;amp; monster, State&amp;amp; state)&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; { monster.GetState(state); }&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span&gt;static&amp;nbsp;&lt;/span&gt;bool&lt;/span&gt;&lt;span&gt;&amp;nbsp;SetMonsterState(&lt;span&gt;Monster&amp;amp; monster,&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;const&lt;/span&gt;&lt;span&gt;&amp;nbsp;State&amp;amp; state)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; { &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; monster.SetState(state); }&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="color: black; margin: 0px;"&gt;};&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;When designing with testability in mind, the second version is what you will normally come up with on the first try. It is unit testing friendly without sacrificing readability, security, functionality or ease of use. Of course, as ever, there are many ways to skin it. I'm sure you can come up with other ways to improve this interface, so please feel free to comment below.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10303990" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/testability/">testability</category><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/object+oriented+design/">object oriented design</category><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/state/">state</category></item><item><title>Of subject and object</title><link>http://blogs.msdn.com/b/dennisg/archive/2012/05/08/subject-vs-object.aspx</link><pubDate>Mon, 07 May 2012 22:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10301534</guid><dc:creator>Dennis Gurzhii</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/rsscomments.aspx?WeblogPostID=10301534</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/commentapi.aspx?WeblogPostID=10301534</wfw:comment><comments>http://blogs.msdn.com/b/dennisg/archive/2012/05/08/subject-vs-object.aspx#comments</comments><description>&lt;p style="text-align: justify;"&gt;Object-oriented programming is all about recreating real-life objects and patterns of their interaction in code. However, since most of the concepts we deal with in our daily programming routine in fact have little to no physical representation (JSONParser, AccountPayable, MediaStream, you name it), we instead create a mental model of what these objects and their behavior&amp;nbsp;&lt;em&gt;would be&lt;/em&gt; like if they were to materialize. Even if a software class stands for a physically existing class of objects, it's likely to be but an abstraction, an imitation that only contains the properties and exhibits the behaviors that are directly relevant to the application intended by the author of the class.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;Quite often, the model that we come up with strays far from how an object would behave under normal circumstances in a presumable real-life situation or assigns unnatural responsibilities to classes. Consider the following code:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white; margin-left: 20px; border-left: thin solid #000000; padding-left: 10px;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;StreetAddress&lt;/span&gt;; &lt;span style="color: green;"&gt;// defined elsewhere&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;GeoCoordinates&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;GeoCoordinates&lt;/span&gt;(&lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;latitude&lt;/span&gt;, &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;longitude&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;GetLatitude&lt;/span&gt;() &lt;span style="color: blue;"&gt;const&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;GetLongitude&lt;/span&gt;() &lt;span style="color: blue;"&gt;const&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;StreetAddress&lt;/span&gt; &lt;span style="color: #020002;"&gt;GetNearestStreetAddress&lt;/span&gt;() &lt;span style="color: blue;"&gt;const&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;Car&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;Drive&lt;/span&gt;(&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;GeoCoordinates&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;destination&lt;/span&gt;, &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;speedMph&lt;/span&gt; = 40);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;Stop&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;RefillGas&lt;/span&gt;(&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;GeoCoordinates&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;gasStation&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;What's the problem here? Cynical&amp;nbsp;disregard for the passive nature of the classes involved. Stop for a second and ponder over these simple questions: can geographical coordinates actually &lt;em&gt;do&lt;/em&gt; something, like look up a street address for instance? Can a car drive or stop itself? Or does it perhaps make sense for a car to fill itself up on gas? We tend to think of these classes as&amp;nbsp;objects of actions, not exhibiting any active behaviors but rather acting as supplement and arguments to the actions of other parties.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;Those classes look innocent enough because our eye is trained to ignore such "minor" inconsistencies in abstractions. After all, every abstraction has its limits, and we often and&amp;nbsp;quite rightly let things slip. However, as it turns out, ignoring the nature of a class by wrongfully assigning it the role of a passive&amp;nbsp;object or conversely&amp;nbsp;that of an active&amp;nbsp;subject in an interaction can often be&amp;nbsp;the telltale sign of a more concrete&amp;nbsp;and measurable problem in the making.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;Consider what happens if at some point a new competing algorithm for street address lookup is found and the implementation of &lt;span style="font-family: courier new,courier; font-size: small;"&gt;&lt;span style="color: #020002;"&gt;GetNearestStreetAddress&lt;/span&gt;()&lt;/span&gt;&amp;nbsp;becomes the second best, but not completely obsolete. You would definitely want to provide the client with the ability to invoke either method of address lookup, so you come up with the following:&lt;/p&gt;
&lt;div style="background: white; color: black; padding-left: 10px; font-family: Courier New; font-size: 10pt; margin-left: 20px; border-left-color: #000000; border-left-width: thin; border-left-style: solid;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;GeoCoordinates&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// ...&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;enum&amp;nbsp;&lt;span style="color: #020002;"&gt;LookupMethod&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: #020002;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: #020002;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;LookupMethod_One,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: #020002;"&gt;&lt;span style="color: #020002;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #020002;"&gt;LookupMethod_Two&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: #020002;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;StreetAddress&lt;/span&gt; &lt;span style="color: #020002;"&gt;GetNearestStreetAddress&lt;/span&gt;(&lt;span style="color: #020002;"&gt;LookupMethod method&lt;/span&gt;) &lt;span style="color: blue;"&gt;const&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;It's all well and good until a third lookup method appears. That method has&amp;nbsp;a parameter&amp;nbsp;that's vital to its operation. Not to worry, there's always a way to fix this:&lt;/p&gt;
&lt;div style="background: white; color: black; padding-left: 10px; font-family: Courier New; font-size: 10pt; margin-left: 20px; border-left-color: #000000; border-left-width: thin; border-left-style: solid;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #020002;"&gt;StreetAddress&lt;/span&gt; &lt;span style="color: #020002;"&gt;GetNearestStreetAddress&lt;/span&gt;( &lt;span style="color: #020002;"&gt;LookupMethod method,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #020002;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int&lt;/span&gt; someParameterForLookupMethodThree = 0 &lt;/span&gt;) &lt;span style="color: blue;"&gt;const&lt;/span&gt;;&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;To add insult to injury, now&amp;nbsp;you've got to have an extra argument just for that method which is useless for the other two. Meh. As you can see, a seemingly innocent flaw in an abstraction can have dire consequences on the quality and longevity of the code. This code above is now officially a&amp;nbsp;throwaway grade mess.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;So what went wrong? Hopefully, it's painfully&amp;nbsp;obvious at this stage that there has to be a set of&amp;nbsp;separate active classes tasked with performing operations on &lt;span style="font-family: courier new,courier; font-size: small;"&gt;GeoCoordinates&lt;/span&gt; in their own unique ways and on their terms so that the coordinates themselves are relegated to a passive data container:&lt;/p&gt;
&lt;div style="background: white; color: black; padding-left: 10px; font-family: Courier New; font-size: 10pt; margin-left: 20px; border-left-color: #000000; border-left-width: thin; border-left-style: solid;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;StreetAddress&lt;/span&gt;; &lt;span style="color: green;"&gt;// defined elsewhere&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;GeoCoordinates&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;GeoCoordinates&lt;/span&gt;(&lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;latitude&lt;/span&gt;, &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;longitude&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;GetLatitude&lt;/span&gt;() &lt;span style="color: blue;"&gt;const&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;GetLongitude&lt;/span&gt;() &lt;span style="color: blue;"&gt;const&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;" color="#020002"&gt;AddressLookupAgentOne&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;StreetAddress&lt;/span&gt; &lt;span style="color: #020002;"&gt;GetNearestStreetAddress&lt;/span&gt;(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; const &lt;/span&gt;&lt;span style="color: #020002;"&gt;GeoCoordinates&amp;amp; coordinates,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #020002;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; someArgument&lt;/span&gt;) &lt;span style="color: blue;"&gt;const&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;//&amp;nbsp;A few more agents go here, with their&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;// own unique interfaces if necessary&amp;nbsp;...&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;With the freedom that this latter approach gives you, the code is more resilient to changes in requirements and generally stands a better chance of remaining structurally sound in the long run*. Please do always take the time to scan your abstractions for possible violations of the "subject vs object" rule and restructure the code whenever you spot inconsistencies in how roles are assigned to various&amp;nbsp;entities. Although being just one example of what Joel Spolsky calls a &lt;a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html"&gt;leaky abstraction&lt;/a&gt;, this one can really ruin your day.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;An attentive reader will notice that the &lt;span style="font-family: courier new,courier; font-size: small;"&gt;Car&lt;/span&gt; class from the first example was thereupon left untouched, although it's&amp;nbsp;haunted by the same problem we've just discussed. I leave it as&amp;nbsp;an exercise to the reader to fix the car so it looks less like something out of a &lt;a href="http://en.wikipedia.org/wiki/DARPA_Grand_Challenge"&gt;DARPA challenge&lt;/a&gt; and more like a vehicle you can actually&amp;nbsp;drive.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;&lt;span style="font-size: x-small;"&gt;(*) One of the senior architects at the Microsoft&amp;nbsp;Skype Division says there is no such thing as "the long run" where code is concerned. I wish I could disagree.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10301534" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/C_2B002B00_/">C++</category><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/object+oriented+design/">object oriented design</category></item><item><title>The Challenges of Testability: Monolith</title><link>http://blogs.msdn.com/b/dennisg/archive/2012/05/06/the-challenges-of-testability-monolith.aspx</link><pubDate>Sun, 06 May 2012 11:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10300104</guid><dc:creator>Dennis Gurzhii</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/rsscomments.aspx?WeblogPostID=10300104</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/commentapi.aspx?WeblogPostID=10300104</wfw:comment><comments>http://blogs.msdn.com/b/dennisg/archive/2012/05/06/the-challenges-of-testability-monolith.aspx#comments</comments><description>&lt;p style="text-align: justify;"&gt;One can often hear that "well-designed software is easy to test". Truth be told, testability is rarely just a lucky side-effect of good design, but a competent architect will keep testability in mind when planning for any sufficiently large or important body of code. In this series I will explore the various challenges of designing components for testability (not to be confused with &lt;a href="http://en.wikipedia.org/wiki/Test-driven_development"&gt;test-driven development&lt;/a&gt;, which is a somewhat broader concept) and how these challenges can be addressed in production quality code.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;Perhaps the main elephant in the room full of testability problems is monolithic encapsulated design. A huge black box with enormous surface area and obscure contents is the hardest thing to test, as anyone ever involved in manual exploratory UI testing will tell you. But let's set UI aside for now as a special case of the more general problem: being able to test something efficiently requires the means to access its internals.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;Consider an online billing component with the following (entirely speculatory) interface:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white; margin-left: 20px; border-left: thin solid #000000; padding-left: 10px;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;struct&lt;/span&gt; &lt;span style="color: #020002;"&gt;CreditCardInfo&lt;/span&gt;; &lt;span style="color: green;"&gt;// defined elsewhere&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;typedef&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;ErrorCode&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;typedef&lt;/span&gt; &lt;span style="color: blue;"&gt;long&lt;/span&gt; &lt;span style="color: blue;"&gt;long&lt;/span&gt; &lt;span style="color: #020002;"&gt;Money&lt;/span&gt;; &lt;span style="color: green;"&gt;// for simplicity&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;IBilling&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; ~&lt;span style="color: #020002;"&gt;IBilling&lt;/span&gt;() { }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: #020002;"&gt;ErrorCode&lt;/span&gt; &lt;span style="color: #020002;"&gt;Charge&lt;/span&gt;( &lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;CreditCardInfo&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;creditCard&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style="color: #020002;"&gt;Money&lt;/span&gt; &lt;span style="color: #020002;"&gt;amount&lt;/span&gt; ) = 0;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;If this interface is all you have access to when implementing tests, you are in trouble. Consider for a second what this component would do internally to accomplish its function. You bet it involves data verification, numeric computations, complex networking over a secure protocol, robust transaction support and whatnot. Notice how all of it is cleverly hidden under the hood, which is Well and Good. After all, you wouldn't want clients to poke around the billing code, of all things, right? Encapsulation is often quite rightly the #1 priority, but it does apparently get in the way of testing.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;So how &lt;em&gt;do&lt;/em&gt; we approach the problem of testing this beast? There are several valid solutions to the problem, and they very much depend on the domain specifics of the code as well as its internal design (which is what the quote in the beginning was all about). Let's explore our options below.&lt;/p&gt;
&lt;h4&gt;The Back Door&lt;/h4&gt;
&lt;p style="text-align: justify;"&gt;The typical way to test a monolithic component without compromising its structure is to provide debugging entry points that are normally invisible to the end user, but only available to debugging clients. Similar to how complex integrated boards and chips are often tested via special extra pins that provide direct access to internal state information and allow the tester to perform certain privileged actions, we might create a special interface that would allow us to easily debug the various aspects of a component's functionality separately. Consider the following example:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white; margin-left: 20px; border-left: thin solid #000000; padding-left: 10px;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;IBillingDebug&lt;/span&gt; : &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: #020002;"&gt;IBilling&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; ~&lt;span style="color: #020002;"&gt;&lt;span&gt;IBillingDebug&lt;/span&gt;&lt;/span&gt;() { }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: #020002;"&gt;ErrorCode&lt;/span&gt; &lt;span style="color: #020002;"&gt;ValidateCC&lt;/span&gt;(&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;CreditCardInfo&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;creditCard&lt;/span&gt;) = 0;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span&gt;ErrorCode&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: #020002;"&gt;VerifyEnoughMoney&lt;/span&gt;( &lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;CreditCardInfo&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;creditCard&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Money&lt;/span&gt; &lt;span style="color: #020002;"&gt;amount&lt;/span&gt; ) = 0;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: #020002;"&gt;ErrorCode&lt;/span&gt; &lt;span style="color: #020002;"&gt;CheckConnectionToBillingServer&lt;/span&gt;() = 0;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// etc. etc.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;There can be all sorts of weird requirements and restrictions on the usage of these debug functions, and the requirements are not frozen, i.e. they are allowed to change regularly as the system evolves. These may be, for instance, the order of execution or specific states in which execution is allowed, the accepted argument values etc. The ever changing requirements often stem from the fact that debug functions access the component's internals, which are legitimately subject to frequent changes.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;While not perfect, this approach allows for testing to be performed on a monolithic component's internal logic units without having to decouple them, which isn't always possible.&amp;nbsp;However, the question of decoupling brings us to the second technique.&lt;/p&gt;
&lt;h4&gt;Decomposition&lt;/h4&gt;
&lt;p style="text-align: justify;"&gt;If all you've got in front of you is the tip of the iceberg, it's time to blast that iceberg into smaller, more manageable pieces with larger surface area. But herein lie the challenges: if the pieces end up being too small, the whole iceberg will disintegrate and melt away; and if the ice is too thick, it may not be possible to crack so easily. Before I take the metaphor too far, let me translate this to geek speak: functional decomposition of a component facilitates testing by providing the test code with privileged access to a number of smaller sub-components that can be tested individually in isolation.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;This boils down to formalizing interfaces of sub-components and making sure that those sub-components are usable on their own without being integrated into a larger system. It is important to ensure that:&lt;/p&gt;
&lt;ul style="text-align: justify;"&gt;
&lt;li&gt;
&lt;p&gt;The additional levels of abstraction created by introducing internal isolated sub-components actually make sense. Too much functional decomposition can also ruin a component by making its structure too ad hoc to be resilient to possible changes in the requirements.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The large component is using nothing but these internal interfaces to interact with its sub-components in order for them to be unit-testable separately from the component's code.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Sub-component inter-dependencies are minimized. Whenever any two or more sub-components depend on each other, testing becomes more complicated, but it's still possible using&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Mock_object"&gt;mocking&lt;/a&gt;, which is a fit topic for a whole another discussion.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="text-align: justify;"&gt;This is the preferred technique as it essentially prevents a monolithic design from even occurring in the first place, although to the end users the components still appears to be a monolithic black box.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;Did I forget any other techiques? I'm sure I have. Various domains and implementations will have their own unique manifestations of the monolith problem, and these will call for special approaches to ensuring testability, so your mileage may vary. Please feel free to contribute your thoughts in the comments below.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10300104" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/testability/">testability</category><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/monolith/">monolith</category><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/black+box/">black box</category><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/object+oriented+design/">object oriented design</category></item><item><title>Exceptions considered harmful</title><link>http://blogs.msdn.com/b/dennisg/archive/2012/04/28/exceptions-considered-harmful.aspx</link><pubDate>Sat, 28 Apr 2012 08:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10298691</guid><dc:creator>Dennis Gurzhii</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/rsscomments.aspx?WeblogPostID=10298691</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/commentapi.aspx?WeblogPostID=10298691</wfw:comment><comments>http://blogs.msdn.com/b/dennisg/archive/2012/04/28/exceptions-considered-harmful.aspx#comments</comments><description>&lt;p style="text-align: justify;"&gt;The doctrine of object-oriented programming dictates that exceptions are the mechanism of choice to raise (and, possibly, handle) severe error conditions that cannot be safely ignored by the client code. Let me just take a step back to explain why I think &lt;em&gt;exceptions are all but inappropriate in most situations by definition&lt;/em&gt;.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;Now let's forget about exceptions for a second.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;I have implemented or used logging in several companies I've worked for. It's a pretty common thing to do, and any logger will usually provide several levels of log messages so that any messages below a certain threshold level can be automatically ignored to avoid cluttering the log with the less useful information. This implies that &lt;em&gt;every&lt;/em&gt; log message has to be assigned some level value, or severity if you will. These typically range from Debug (safe to ignore unless you're trying to debug an error in the surrounding code) to Error (unexpected failure that could be causing problems for the user) to Fatal (so bad that the program will have to be abnormally terminated), with a few intermediate levels in between. This is all fairly trivial and seemingly off topic, but bear with me.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;The single most annoying problem with logging levels is that severity (or level) of a particular log event is highly context dependent. What one client of a particular piece of code considers to be a truly fatal condition can be safely shrugged off by another. However, since logging itself occurs inside the aforementioned piece of code and not inside its client code, severity is forever fixed at some assumed, one-size-fits-all kind of value. Let's take an example. Here's client #1:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white; margin-left: 20px; border-left: thin solid #000000; padding-left: 10px;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #020002;"&gt;std&lt;/span&gt;::&lt;span style="color: #020002;"&gt;ifstream&lt;/span&gt; &lt;span style="color: #020002;"&gt;file&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"accounts.db"&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;(!&lt;span style="color: #020002;"&gt;file&lt;/span&gt;.&lt;span style="color: #020002;"&gt;good&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;die_a_painful_death&lt;/span&gt;();&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;and client #2:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white; margin-left: 20px; border-left: thin solid #000000; padding-left: 10px;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #020002;"&gt;std&lt;/span&gt;::&lt;span style="color: #020002;"&gt;ifstream&lt;/span&gt; &lt;span style="color: #020002;"&gt;file&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"debug_dump.dat"&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;(&lt;span style="color: #020002;"&gt;file&lt;/span&gt;.&lt;span style="color: #020002;"&gt;good&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// dump some debug information&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;If, for the sake of argument,&amp;nbsp;&lt;span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;ifstream&lt;/span&gt; were to do logging, what kind of severity level would it assign to the error message when a file cannot be opened, let's say, due to lack of access rights? It wouldn't have a clue. Depending on the choice, we'd either have an overreacting log message or a message that can be easily lost when it in fact bears some real meaning. And all of this, mind you, is purely context dependent. This kind of confusion happens on most levels of abstraction in pretty much every corner of any sufficiently large source code base, because you bet that the client code I just gave you as an example also has some kind of client code of its own, and this goes all the way to the top.&lt;/span&gt;&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;&lt;span&gt;Now take a moment and think about what exceptions are. An exception is an &lt;em&gt;assumption&lt;/em&gt; that the error which is being propagated using that exception is severe enough that it has to be handled by client code and cannot be ignored, or else the program will have to terminate abnormally. But really, who is to judge, at any level of abstraction but the highest one (user interface in most cases), what is or is not a potentially fatal error condition? It's all highly dependent on what the client's intentions are, and any assumptions to that end are simply a waste of breath.&lt;/span&gt;&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;&lt;span&gt;So what assumptions are normally made by exception-reliant code? I think you can make an educated guess. &lt;em&gt;Every marginally important error condition in code that relies on exceptions is, and in fact has to be, treated as a potentially fatal error&lt;/em&gt;. And there's your problem right there. What ensues is what my former boss (a fervent supporter of exceptions, no less) called "exception spam". It happens all the more often when code is reused across several projects and severity assumptions of certain errors don't hold true for all of the reusing clients. Exceptions just keep coming (and being handled) on all sorts of seemingly innocent occasions.&amp;nbsp;&lt;/span&gt;The problem is, once the "break on exceptions" debugger&amp;nbsp;functionality is off the table due to exception spam, you are totally on your own. There are literally no techniques in your toolbox&amp;nbsp;other than manual code analysis at this point. Consider this familiar client code snippet:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white; margin-left: 20px; border-left: thin solid #000000; padding-left: 10px;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #020002;"&gt;std&lt;/span&gt;::&lt;span style="color: #020002;"&gt;ifstream&lt;/span&gt; &lt;span style="color: #020002;"&gt;file&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"accounts.db"&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;//if(!file.good())&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp; die_a_painful_death();&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;If&amp;nbsp;&lt;span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;ifstream&lt;/span&gt; were to throw an exception on error, there would possibly be no if clause afterwards to check for that exception because it may well be handled a few call frames above, by the client code at some distant level of abstraction. You can't set a breakpoint on a line of code that doesn't exist, can you?&amp;nbsp;&lt;/span&gt;Bottom line, in order to figure out what the hell is going on one has to get their hands dirty, disable their debugger's "break on exceptions" functionality and go find that bug the old-fashioned way, using eyeball Mk 1. Well, either that or add try-catch blocks &lt;em&gt;immediately&lt;/em&gt; around every theoretically throwing function call. Good luck with that!&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;If you have it in you to endure another rant on the pain of debugging exceptions, see &lt;a href="http://www.joelonsoftware.com/items/2003/10/13.html"&gt;this post&lt;/a&gt; on Joel's blog.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;Exceptions, while a theoretically sound and clean concept that allows you to write less code and handle errors more rigorously, eventually introduce more severe (if less obvious) problems than they solve. I recommend you stay away from them unless there's a very compelling reason to do otherwise.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10298691" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/C_2B002B00_/">C++</category><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/exceptions/">exceptions</category></item><item><title>C++ Component Interface Design: Basic Isolation</title><link>http://blogs.msdn.com/b/dennisg/archive/2012/04/12/c-library-interface-design-part-1.aspx</link><pubDate>Thu, 12 Apr 2012 18:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10293275</guid><dc:creator>Dennis Gurzhii</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/rsscomments.aspx?WeblogPostID=10293275</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/commentapi.aspx?WeblogPostID=10293275</wfw:comment><comments>http://blogs.msdn.com/b/dennisg/archive/2012/04/12/c-library-interface-design-part-1.aspx#comments</comments><description>&lt;p style="text-align: justify;"&gt;Designing an efficient interface that decouples a component from its client is a fairly routine task that's easy to mess up. A component is supposed to be highly reusable, flexible and easy to use, not to mention well isolated. Satisfying these diverse (and often mutually exclusive!) requirements is no easy feat and often comes down to certain tradeoffs in the component interface.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;There's no one right answer to the big question of how exactly a component's classes should be exposed to the client in terms of isolation, but the problem boils down to three main issues: exposing the interface, encapsulating the implementation details and providing a way to instantiate the component's classes. We'll have a look at some of the popular choices, just as you'd probably do when considering various alternatives for a real-life production quality C++ component interface.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;We'll use a classical Car &amp;amp; Garage example for the code snippets below.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;1. The simplistic approach&lt;/b&gt;&lt;/h4&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white; margin-left: 20px; border-left: thin solid #000000; padding-left: 10px;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;// Component header, take #1&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;Car&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Car&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ~&lt;span style="color: #020002;"&gt;Car&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; &lt;span style="color: #020002;"&gt;Drive&lt;/span&gt;(&lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;speed&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; &lt;span style="color: #020002;"&gt;Stop&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: #020002;"&gt;SoundAlarm&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;speed_&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ~&lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;AddCar&lt;/span&gt;(&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;Car&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;car&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;std&lt;/span&gt;::&lt;span style="color: #020002;"&gt;vector&lt;/span&gt;&amp;lt;&lt;span style="color: #020002;"&gt;Car&lt;/span&gt;&amp;gt; &lt;span style="color: #020002;"&gt;cars_&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;&lt;span&gt;This is a working interface that will probably do its job, and in many cases it is in fact the model of choice. For one thing, it's the fastest method of all. No complications, no problems. Instantiate the classes as you're all set. However, several potential issues are worth noting if this code is supposed to be a component interface:&lt;/span&gt;&lt;/p&gt;
&lt;ul style="text-align: justify;"&gt;
&lt;li&gt;
&lt;p&gt;&lt;i&gt;Unchecked instantiation&lt;/i&gt;. A client can spawn as many instances of the component's classes as they please (both on the stack and on the heap). This becomes a nasty issue if those classes grow in size (storing large objects on the stack can be sub-optimal) or come to require certain instantiation restrictions (typically such as only one Garage object per client application). Since you never really know what the future requirements to the component might be, taking precautions against these problems early on seems reasonable enough.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Lack of insulation&lt;/i&gt;&lt;span&gt;. Note how the client is exposed to a technically unnecessary detail of implementation in the form of private member variables and methods. These cannot be invoked by the client directly for apparent reasons, but they do present a temptation to manually edit the component header or use other nasty tricks to access the private implementation details of the component. As if this was not enough, there's the added inconvenience of having to recompile each client every time you alter the private stuff in the exposed classes, as opposed to merely having to re-link if those private things were duly insulated.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="text-align: justify;"&gt;Albeit somewhat flawed, I find this isolation model useful for libraries that contain a loose affiliation of unrelated and relatively simple helper classes that just do not warrant a more sophisticated approach.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;Let's try and refine the above interface to deal with its inherent problems.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;2. &lt;b&gt;The pImpl idiom and the singleton&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white; margin-left: 20px; border-left: thin solid #000000; padding-left: 10px;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;// Component header, take #2&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;CarImpl&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;Car&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Car&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ~&lt;span style="color: #020002;"&gt;Car&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; &lt;span style="color: #020002;"&gt;Drive&lt;/span&gt;(&lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;speed&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; &lt;span style="color: #020002;"&gt;Stop&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;CarImpl&lt;/span&gt;* &lt;span style="color: #020002;"&gt;pImpl_&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;GarageImpl&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;GetInstance&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;AddCar&lt;/span&gt;(&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;Car&lt;/span&gt;&amp;amp; &lt;span style="color: #020002;"&gt;car&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ~&lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;(&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;&amp;amp;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;&amp;amp; &lt;span style="color: blue;"&gt;operator&lt;/span&gt;=(&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;&amp;amp;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #020002;"&gt;GarageImpl&lt;/span&gt;* &lt;span style="color: #020002;"&gt;pImpl_&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;This time we applied standard solutions to control instantiation of the component classes and insulate their private contents:&amp;nbsp;&lt;a href="http://www.gamedev.net/reference/articles/article1825.asp"&gt;the singleton pattern&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Opaque_pointer"&gt;the pImpl idiom&lt;/a&gt;&amp;nbsp;respectively. Note how the client doesn't have the definition of the pImpl classes and therefore cannot mess with them. Neither can the client create more than one instance of the Garage class (you can also impose other instantiation restrictions similarly). Job done? Umm... Not quite.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;It appears that by addressing the problems with the initial interface we've inadvertently introduced other serious issues:&lt;/p&gt;
&lt;ul style="text-align: justify;"&gt;
&lt;li&gt;
&lt;p&gt;&lt;i&gt;Object's lifetime&amp;nbsp;control is far from perfect&lt;/i&gt;. Singletons and similar instantiation restrictions imply that either a) the class obscurely controls the lifetime of its own object (e.g. Meyers singleton) or b) the client has to manually destroy the object (e.g. Gamma singleton). Any special function that produces a class object is haunted by this ugly ambiguity - unless it's returning a&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Smart_pointer"&gt;smart pointer&lt;/a&gt;&amp;nbsp;that is. Sigletons are also subject to static initialization and finalization issues that are somewhat beyond the scope here, but let's just say I don't recommend using them without a very good reason.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;i&gt;Performance overhead&lt;/i&gt;. PImpl has to be allocated dynamically, thus introducing a minor performance penalty for every class instantiation.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;i&gt;Code editing is burdensome&lt;/i&gt;. Insulation has its price: adding even a single function to the pImpl'ed class means you'll have to edit the back-end class as well as the interface class. If both classes are non-inline (which is probably the way they are supposed to be), it means editing too much code for comfort.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="text-align: justify;"&gt;Singletons surely have their valid uses, but they almost never make it to interfaces of my components except as occasional supplements to make pre-fabricated shared versions of some of the other classes. As for the pImpl idiom, I find it to be only marginally useful compared to other alternatives.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;Looks like the interface may need yet another makeover. Oh well.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;3.&amp;nbsp;&lt;b&gt;&lt;b&gt;Interfaces and smart pointers&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white; margin-left: 20px; border-left: thin solid #000000; padding-left: 10px;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;// Component header, take #3&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;struct&lt;/span&gt; &lt;span style="color: #020002;"&gt;ICar&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; ~&lt;span style="color: #020002;"&gt;Car&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; &lt;span style="color: #020002;"&gt;Drive&lt;/span&gt;(&lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: #020002;"&gt;speed&lt;/span&gt;) = 0;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; &lt;span style="color: #020002;"&gt;Stop&lt;/span&gt;() = 0;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;typedef&lt;/span&gt; &lt;span style="color: #020002;"&gt;boost&lt;/span&gt;::&lt;span style="color: #020002;"&gt;shared_ptr&lt;/span&gt;&amp;lt;&lt;span style="color: #020002;"&gt;ICar&lt;/span&gt;&amp;gt; &lt;span style="color: #020002;"&gt;PCar&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;struct&lt;/span&gt; &lt;span style="color: #020002;"&gt;IGarage&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; ~&lt;span style="color: #020002;"&gt;Garage&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: #020002;"&gt;AddCar&lt;/span&gt;(&lt;span color="#020002" style="color: #020002;"&gt;PCar&lt;/span&gt;&amp;nbsp;&lt;span style="color: #020002;"&gt;car&lt;/span&gt;) = 0;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;typedef&lt;/span&gt; &lt;span style="color: #020002;"&gt;boost&lt;/span&gt;::&lt;span style="color: #020002;"&gt;shared_ptr&lt;/span&gt;&amp;lt;&lt;span style="color: #020002;"&gt;IGarage&lt;/span&gt;&amp;gt; &lt;span style="color: #020002;"&gt;PGarage&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #020002;"&gt;PCar&lt;/span&gt; &lt;span style="color: #020002;"&gt;CreateCar&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #020002;"&gt;PGarage&lt;/span&gt; &lt;span style="color: #020002;"&gt;CreateGarage&lt;/span&gt;();&lt;/p&gt;
&lt;/div&gt;
&lt;p style="text-align: justify;"&gt;The classes have gone abstract, and there's a simple factory in place to produce objects of those classes. Note how the factory is returning smart pointers this time, the magnificent&amp;nbsp;&lt;a href="http://www.boost.org/doc/libs/1_41_0/libs/smart_ptr/shared_ptr.htm"&gt;boost::shared_ptr&lt;/a&gt;&amp;nbsp;to be precise (you can also use &lt;a href="http://msdn.microsoft.com/en-us/library/bb982026.aspx"&gt;std::shared_ptr&lt;/a&gt; if your compiler supports C++11, which it should). For some purposes&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Auto_ptr"&gt;std::auto_ptr&lt;/a&gt;&amp;nbsp;would also do a satisfactory job in this case. So what do we have here?&lt;/p&gt;
&lt;ul&gt;
&lt;li style="text-align: justify;"&gt;
&lt;p&gt;&lt;i&gt;Perfect encapsulation and insulation&lt;/i&gt;. Absolutely no class internals are revealed, in fact even the &amp;nbsp;implementation classes themselves are hidden.&lt;/p&gt;
&lt;/li&gt;
&lt;li style="text-align: justify;"&gt;
&lt;p&gt;&lt;i&gt;Controlled instantiation&lt;/i&gt;. The factory functions are in complete control over how the classes are instantiated, how many instances can be created etc.&lt;/p&gt;
&lt;/li&gt;
&lt;li style="text-align: justify;"&gt;
&lt;p&gt;&lt;i&gt;Convenient lifetime control&lt;/i&gt;. The client can use the objects in the "fire and forget" mode most of the time while easily delaying or precipitating destruction of the objects as they please through controlling the lifetime of the original smart pointers and copies thereof. shared_ptr supports custom deleters that don't affect the type of shared_ptr template instantiation (see &lt;a href="http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_ptr.htm#constructors"&gt;here&lt;/a&gt;, for instance), so this solution also supports allocating memory for the objects with something other than operator new.&lt;/p&gt;
&lt;/li&gt;
&lt;li style="text-align: justify;"&gt;
&lt;p&gt;&lt;i&gt;Minor editing inconvenience&lt;/i&gt;. Adding new functions to the classes entails changes to both back-end implementations and front-end interfaces, but we're talking pretty small changes here. Definitely worse than the naive approach #1 but much better than the pImpl case.&lt;/p&gt;
&lt;/li&gt;
&lt;li style="text-align: justify;"&gt;
&lt;p&gt;&lt;i&gt;Minor performance hit&lt;/i&gt;. Creating objects on the heap and using virtual functions makes for a barely noticeable but positively present performance hit. It's worse than both #1 and #2 in this respect because as many as two dynamic memory allocations will be done per class instantiation: one for the object itself and another for the reference counter internal to shared_ptr.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p style="text-align: justify;"&gt;Unfortunate&amp;nbsp;&lt;i&gt;susceptibility to the infamous&amp;nbsp;&lt;/i&gt;&lt;a href="http://www.codeproject.com/KB/cpp/Short_Story_VCPP_CRT.aspx"&gt;&lt;i&gt;new/delete issue&lt;/i&gt;&lt;/a&gt;&lt;i&gt;&amp;nbsp;with statically linked CRT in a DLL library&lt;/i&gt;. For this reason, I prefer not to author&amp;nbsp;DLL class&amp;nbsp;libraries if at all possible.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p style="text-align: justify;"&gt;Where does this leave us? In my humble opinion, the interface &amp;amp; shared_ptr solution by far outclasses other conceivable alternatives and makes for an almost perfect all-round instantiation model for a component interface in C++. I've used it too many times to count, and never had any significant problems or encountered major limitations except for the abovementioned&amp;nbsp;CRT issue.&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;I will continue looking at other, more complicated aspects of component interface design in future articles, but this is it for today.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10293275" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/C_2B002B00_/">C++</category><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/object+oriented+design/">object oriented design</category></item><item><title>Design and productivity</title><link>http://blogs.msdn.com/b/dennisg/archive/2012/04/12/design-and-productivity.aspx</link><pubDate>Thu, 12 Apr 2012 17:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10293253</guid><dc:creator>Dennis Gurzhii</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/rsscomments.aspx?WeblogPostID=10293253</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dennisg/commentapi.aspx?WeblogPostID=10293253</wfw:comment><comments>http://blogs.msdn.com/b/dennisg/archive/2012/04/12/design-and-productivity.aspx#comments</comments><description>&lt;p style="text-align: justify;"&gt;Each programmer has their own way of thinking. Some of us churn out countless lines of code per day by concentrating on what they are trying to achieve rather than how exactly they are doing it. Others, often regarded as slower thinkers, tend to ponder over details of implementation and produce fewer lines of code resulting in apparently lower performance than that of their hasty colleagues. Obviously a programmer's efficiency is adequately measured by the amount of code they produce in a given unit of time. Or is it?&lt;/p&gt;
&lt;p style="text-align: justify;"&gt;Sometimes I find myself feeling guilty for staring at the IDE and thinking, apparently wasting precious work time that I'm being paid for. None of my superiors have ever scolded me for that, but a subconscious guilt is always there, prompting me to write that code as it comes to mind and never think twice or look back. Having seen lots of open-source code I can safely assume most GPL developers tend to do just that. Hardly structured, vastly copied-and-pasted code lacking any sensible architecture at all - that's what you're likely to get on SourceForge or wherever you prefer going for open-source inspiration. Now before you get all excited and start telling me about a gift horse's mouth, allow me to remind you that this was just an example to illustrate a common truth: poorly thought out code sucks. To name a few reasons why:&lt;/p&gt;
&lt;div&gt;
&lt;ul style="text-align: justify;"&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;Low reusability&lt;/b&gt;&lt;span&gt;. Code monolith is what you're likely to get unless you deliberately think what goes where in the code and how it's going to be (re)used later. Unless you make a habit of deliberately thinking about reusing code it will seldom be fit for any purpose outside its immediate intended environment.&lt;/span&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;span&gt;&lt;b&gt;Excessive complexity&lt;/b&gt;&lt;span&gt;. Unstructured code tends to get complex. It is not unknown for such code to overwhelm its own authors let alone any support programmers unfortunate enough to be assigned to fix/update that code! Excessive code complexity creates new bugs and prevents the old ones from being fixed or even identified.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&lt;b&gt;Bad example&lt;/b&gt;&lt;span&gt;. Thinking less often means coding more (and I do mean literally coding rather than programming). This sets a very wrong example for teammates if a manager were to praise such a coder for superior productivity.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&lt;span&gt;&lt;b&gt;Domino effect&lt;/b&gt;&lt;span&gt;. When assigned to support junk code, even careful and thoughtful programmers often subconsciously seek overly simplistic (i.e. wrong) ways to fix/update the code in order to get rid of it ASAP. The "this code is so screwed up, why bother thinking, let's just patch it up a bit" attitude creates a chain reaction whereby code evolves from bad to worse.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="text-align: justify;"&gt;&lt;span&gt;Writing the initial code is just 20% of the job, and I'm being optimistic here. The other 80% is supporting and reusing that code hopefully for years to come. By speeding up the initial 20% coders often neglect the other 80% and make someone's (or their own) job much harder at later stages of the code's life cycle. Which brings me to a conclusion:&amp;nbsp;&lt;/span&gt;&lt;i&gt;writing hasty code is utterly counterproductive&lt;/i&gt;&lt;span&gt;. As a matter of fact, thinking about the grand scheme of things is one of the most productive things a programmer can do. Maybe that'll make me feel less guilty next time I stare at the screen, doing "nothing".&lt;/span&gt;&lt;/p&gt;
&lt;div style="text-align: justify;"&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10293253" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/productivity/">productivity</category><category domain="http://blogs.msdn.com/b/dennisg/archive/tags/sustainable+development/">sustainable development</category></item></channel></rss>