<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Wrong.net</title>
	<atom:link href="http://www.wrong.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wrong.net</link>
	<description>Game development done right</description>
	<pubDate>Fri, 10 Oct 2008 10:41:32 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>Interfaces - necessary, but not sufficient</title>
		<link>http://www.wrong.net/2008/10/10/interfaces-necessary-but-not-sufficient/</link>
		<comments>http://www.wrong.net/2008/10/10/interfaces-necessary-but-not-sufficient/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 10:41:32 +0000</pubDate>
		<dc:creator>Kyoryu</dc:creator>
		
		<category><![CDATA[General development]]></category>

		<guid isPermaLink="false">http://www.wrong.net/?p=49</guid>
		<description><![CDATA[Using an interface seems like one of those rules.  Everybody knows they should do it, because it makes your code more abstracted and&#8230; stuff.
However, if you have a gigantic class with a ton of methods, or methods that are very specific to its implementation, then simply providing an interface that mirrors the public methods of [...]]]></description>
			<content:encoded><![CDATA[<p>Using an interface seems like one of those rules.  Everybody knows they should do it, because it makes your code more abstracted and&#8230; stuff.</p>
<p>However, if you have a gigantic class with a ton of methods, or methods that are very specific to its implementation, then simply providing an interface that mirrors the public methods of the class is of little value.  To successfully swap one implementation for another, you would need to understand the behavior of the first implementation so well that you could accurately mimic it - and, frankly, that&#8217;s not very likely unless you&#8217;re the one to write the first implementation anyway (and probably not too likely even then).  While you&#8217;ve avoided implementation coupling, you&#8217;ve got a kind of <em>conceptual</em> coupling in its place.  This is doubly true if the interface specifies that it returns objects that implement another interface (likely as thick as the first).</p>
<p>So, using interfaces as kind of headers doesn&#8217;t really help us too much in this case.  We&#8217;re still realistically tied to an implementation, and now we have interface versioning issues to deal with (which, for C# at least, are worse than class versioning issues, as adding a member to a class does not break backwards compatability - but it does for an interface).</p>
<p>That doesn&#8217;t mean that I&#8217;m against interfaces.  In fact, I love interfaces.  I just think that there&#8217;s better ways to use them than as sim-headers.</p>
<p>Interfaces should be used to define questions that, as the class you&#8217;re writing, you want to ask of your dependencies.  This is a bit of an inversion - typically, interfaces are defined from the POV of the class implementing them, not the class using them.  But, by controlling the interfaces you use, there&#8217;s less chance of them breaking and causing major headaches throughout your codebase.</p>
<p>Interfaces should also be as small as possible, and represent a single aspect of what you can do with an object.  IEnumerable&lt;&gt; is a great example - it only lists things that you need to do to enumerate a collection.  And because of that, it can be very stable.  The more things an interface does, the more likely it is to need to change, and the more code that will be broken when it does.</p>
<p>So, if you&#8217;re using interfaces in this way, how do you implement them?  Especially if the class you&#8217;re dealing with didn&#8217;t own the interface to begin with?  This isn&#8217;t too hard - write a small adapter class that implements the interface, and calls the underlying object in the appropriate way.  This has the added advantage of keeping all your dependent code in one spot, making it much, much easier to fix if the dependency ever changes underneath you (assuming that you don&#8217;t control it).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wrong.net/2008/10/10/interfaces-necessary-but-not-sufficient/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Anti-pattern:  Silk-pursing</title>
		<link>http://www.wrong.net/2008/06/20/anti-pattern-silk-pursing/</link>
		<comments>http://www.wrong.net/2008/06/20/anti-pattern-silk-pursing/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 22:51:10 +0000</pubDate>
		<dc:creator>Kyoryu</dc:creator>
		
		<category><![CDATA[General development]]></category>

		<guid isPermaLink="false">http://www.wrong.net/?p=47</guid>
		<description><![CDATA[&#8220;You can&#8217;t turn a pig&#8217;s ear into a silk purse.&#8221;
&#8220;It&#8217;s not a global, it&#8217;s a singleton!&#8221;
Description:
An &#8220;acceptable&#8221; design pattern is placed on top of a concept that is generally avoided
Symptoms:

Problems associated with a known poor development practice start cropping up. 
Problem areas are defended by spouting off the name of the design pattern that they superficially [...]]]></description>
			<content:encoded><![CDATA[<p><em>&#8220;You can&#8217;t turn a pig&#8217;s ear into a silk purse.&#8221;</em></p>
<p><em>&#8220;It&#8217;s not a global, it&#8217;s a singleton!&#8221;</em></p>
<p><strong>Description:</strong></p>
<p>An &#8220;acceptable&#8221; design pattern is placed on top of a concept that is generally avoided</p>
<p><strong>Symptoms:</strong></p>
<ul>
<li>Problems associated with a known poor development practice start cropping up. </li>
<li>Problem areas are defended by spouting off the name of the design pattern that they superficially resemble.</li>
<li>Patterns are used when the problem that the pattern solves is not demonstrated, but for tangental reasons.</li>
</ul>
<p><strong>Examples:</strong></p>
<p>The most common form of silk-pursing is singletonitis.  Too often, globals are wrapped up in singletons, because that somehow makes them &#8220;okay.&#8221;  Inappropriate use of Service Providers seems to be the next version of singletonitis, in that it is often used to propagate globals rather than the actual purposes (extensibility, etc.)</p>
<p>Usage of try/catch/finally to simulate goto is another example.</p>
<p>Silk-pursing is related to cargo-cult-programming.  In both cases, a useful pattern is used inappropriately.  In cargo-cult-programming usually involves adding patterns/structures/algorithms/etc. for no apparent reason whatsoever.  Silk-pursing is different in that the pattern being abused is applied solely for the purpose of hiding a practice that is frowned upon.</p>
<p>Silk-pursing may seem like gold-plating, but it is different.  Gold-plating involves putting extra, unnecessary features into code.  Silk-pursing simply hides poor development practices.</p>
<p>Silk-pursing may or may not be a deliberate attempt to conceal.  In many cases, developers will actively believe that because they are using something they&#8217;ve heard is a beneficial practice, that what they are doing is actually better.</p>
<p><strong>Fixing:</strong></p>
<p>Treat the code as if it were the underlying development practice - treat silk-purse singletons as globals, etc.</p>
<p>Educate developers on the purpose of the design patterns that are being abused.</p>
<p>Educate developers on the fact that concealing a bad practice doesn&#8217;t make it any better.</p>
<p>Educate developers on ways to design code that doesn&#8217;t involve using the poor practice.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wrong.net/2008/06/20/anti-pattern-silk-pursing/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Types of Dependencies</title>
		<link>http://www.wrong.net/2008/05/23/types-of-dependencies/</link>
		<comments>http://www.wrong.net/2008/05/23/types-of-dependencies/#comments</comments>
		<pubDate>Fri, 23 May 2008 20:04:40 +0000</pubDate>
		<dc:creator>Kyoryu</dc:creator>
		
		<category><![CDATA[General development]]></category>

		<guid isPermaLink="false">http://www.wrong.net/?p=46</guid>
		<description><![CDATA[Dependencies and coupling seem to cause the greatest pain in software development.  I think it&#8217;s useful to look at the types of dependencies that can exist.
Contained Dependencies
A contained dependency is a dependency that a class has, but which is not communicated externally.  The class uses the contained object, and so is dependent upon it, but does [...]]]></description>
			<content:encoded><![CDATA[<p>Dependencies and coupling seem to cause the greatest pain in software development.  I think it&#8217;s useful to look at the types of dependencies that can exist.</p>
<h2>Contained Dependencies</h2>
<p>A contained dependency is a dependency that a class has, but which is not communicated externally.  The class <em>uses </em>the contained object, and so is dependent upon it, but does not <em>propagate </em>the dependency.  This is the most benign dependency, as if the dependency breaks, the class may break, but it can not (directly) break other objects.</p>
<h2>Direct Dependency</h2>
<p>A direct dependency is a dependency which is exposed directly by the class, either in a return value, a parameter, or a base class.  Direct dependencies are worse than contained dependencies, as they can directly break classes that use the class under discussion, both in terms of compilation and functionality.</p>
<h2>Indirect Dependency</h2>
<p>An indirect dependency is a dependency exposed by another dependency.  This is worse than direct dependencies, as this is how dependencies propagate, causing the system to become brittle.</p>
<h2>Hidden Dependency</h2>
<p>A hidden dependency is arguably the worst kind of dependency.  A hidden dependency is a contained dependency that can cause side effects, causing other components to fail.  Globals are, generally, hidden dependencies.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wrong.net/2008/05/23/types-of-dependencies/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Defining Value Types</title>
		<link>http://www.wrong.net/2008/05/14/defining-value-types/</link>
		<comments>http://www.wrong.net/2008/05/14/defining-value-types/#comments</comments>
		<pubDate>Wed, 14 May 2008 18:04:19 +0000</pubDate>
		<dc:creator>Kyoryu</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.wrong.net/?p=45</guid>
		<description><![CDATA[One of the things that&#8217;s been floating around in my mind is the difference between object types and value types.  Conceptually, it&#8217;s the difference between a dialog and an integer or string.  In my mind, I understand the intent, but I dislike categorizing by intent, as that leads to fuzzy definitions that eventually become meaningless.
The [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that&#8217;s been floating around in my mind is the difference between object types and value types.  Conceptually, it&#8217;s the difference between a dialog and an integer or string.  In my mind, I understand the intent, but I dislike categorizing by intent, as that leads to fuzzy definitions that eventually become meaningless.</p>
<p>The other way to define value types is by storage - stack vs. heap.  I really don&#8217;t like this, though, as one of the advantages of value types is that it&#8217;s safe to pass them by reference.  I would say a string in C# is a value type, even though it&#8217;s a class (and therefore passed by reference).</p>
<p>So, here&#8217;s a set of criteria to determine if something is a value type or not.  These criteria don&#8217;t specify whether a particular type <strong>could</strong> be implemented as a value type, only whether or not they <strong>are</strong> a value type as implemented.</p>
<ol>
<li>The type must be immutable.  Any methods called on the type cannot change its members.  If a modification is desired, the method should create a new object of the same type, and return that object.</li>
<li>The type must only reference other value types.</li>
</ol>
<p>If your type follows these two rules, it is guaranteed to be side-effect free.</p>
<p>Rule #2 might be relaxed, if the reference type that the type holds is guaranteed to never be exposed (so nothing can ever modify it), and the object never modifies it itself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wrong.net/2008/05/14/defining-value-types/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Thoughts on Pair Programming</title>
		<link>http://www.wrong.net/2008/04/26/thoughts-on-pair-programming/</link>
		<comments>http://www.wrong.net/2008/04/26/thoughts-on-pair-programming/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 20:11:46 +0000</pubDate>
		<dc:creator>Kyoryu</dc:creator>
		
		<category><![CDATA[General development]]></category>

		<guid isPermaLink="false">http://www.wrong.net/?p=44</guid>
		<description><![CDATA[Ahhh, probably the most controversial subject in development.  I don&#8217;t know of any single issue that is more likely to get people riled up, either for or against it.
My experience is pretty simple.  I&#8217;ve never done pair programming &#8220;full-time.&#8221;  But, like most programmers, I&#8217;ve done it at times - working with another programmer over a [...]]]></description>
			<content:encoded><![CDATA[<p>Ahhh, probably the most controversial subject in development.  I don&#8217;t know of any single issue that is more likely to get people riled up, either for or against it.</p>
<p>My experience is pretty simple.  I&#8217;ve never done pair programming &#8220;full-time.&#8221;  But, like most programmers, I&#8217;ve done it at times - working with another programmer over a problem.</p>
<p>When I&#8217;ve done that, I&#8217;ve generally found that a few things happened:</p>
<ul>
<li>My knowledge increased</li>
<li>Hopefully, the knowledge of the other guy increased</li>
<li>We both understood the system better</li>
<li>We generally produced better quality code than I was used to seeing from either of us, individually</li>
<li>We remained more highly focused</li>
</ul>
<p>There&#8217;s been a number of studies done on pair programming.  Most of them use similar methodologies, and reach similar conclusions.  I&#8217;ll dig up references later.</p>
<p>In general, they take developers or students, and divide them into two groups.  One of the groups will pair up and work on a project, while the other group will approach the project individually.</p>
<p>In general, these projects are rather small.  In one experiment, they were class assignments, over a period of time.</p>
<p>The results generally found were that the pair took less clock-time, but more man-hours to complete the task.  Generally, the results from pairs were of higher quality.</p>
<p>One particular study continued the experiment over time, and found that the tax on man-hours dropped from 40% at the beginning, to only about 15% at the end of the experiment.</p>
<p>Now, that&#8217;s pretty impressive by itself.  If a project would normally take 40 hours for a single developer, and with two, you can get it done in 22 hours and with higher quality, I think that&#8217;s a win.</p>
<p>But, I think that the experiments described are testing the wrong things.</p>
<p>I see the real benefit of pair programming not in coming from initial productivity, but from the ability to sustain productivity over time, and to allow higher levels of scaling.</p>
<p>One effect noted was that for pairs to become highly effective, it took some amount of time for both the developers to get used to pair programming, as well as to get used to each other.  When doing an experiment on a small or micro basis (projects taking &lt;1 day to complete), the initial cost of this can easily outweight any benefits.</p>
<p>Secondly, one advantage that I see is increased knowledge of systems.  By pairing, especially if pairs are not static, developers will work on multiple areas of the project, and will gain understanding of the &#8220;big picture&#8221; of what they&#8217;re doing, rather than their isolated area.  This should increase overall quality, as well as remove the &#8220;hit-by-a-bus&#8221; category of risks.  When working on small projects that can be completed in under a week of work, this is mostly irrelevant.</p>
<p>Third, by pairing developers, you will remove some level of the communication tax.  Given four developers, if they work individually, they must all coordinate.  If they are paired off, you now only have two groups that need to coordinate, instead of 4 individuals.  Because the &#8220;individual&#8221; developers worked as exactly that, they had zero communication tax.  Comparing paired developers to two developers collaborating as individuals would be an interesting metric.</p>
<p>Fourth, maintenance is very important.  While &#8220;passing tests&#8221; is an important measure of quality, the ability to maintain and expand code is also extremely important, if hard to measure.  If pairing increases quality, then this additional quality should (in theory) allow code to be more easily changed in the future - especially when a larger number of developers have insight into it.  Again, on small projects, this benefit is unimportant.</p>
<p>None of these benefits can be measured on toy or small projects.</p>
<p>So, how would I design a pair programming test, in an ideal world?</p>
<p>I think you&#8217;d only need to change a few things.</p>
<p>First, projects need to be somewhat longer - at least 40 hours to completion.  And that is minimum.</p>
<p>Secondly, to be fair, the test needs to compare equal-sized groups of developers to each other, working individually or in pairs.  In my experience, I&#8217;m not sure that adding a single additional developer actually increases productivity, but this is a more realistic comparison.</p>
<p>And last, ideally you&#8217;d want to scale out even further - four developers or more, working in pairs, against a team of the same size working as individuals.  Generally, in a realistic environment, the question is not &#8220;should we hire twice as many developers and have them pair?&#8221;  The typical question is &#8220;we have this many developers - should they work alone, or in pairs?&#8221;</p>
<p>I honestly don&#8217;t know how these results would turn out.  But, I suspect that they&#8217;d turn out very well for pair programming, especially if combined with other practices (such as TDD).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wrong.net/2008/04/26/thoughts-on-pair-programming/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Development, the fall line, and process</title>
		<link>http://www.wrong.net/2008/04/26/development-the-fall-line-and-process/</link>
		<comments>http://www.wrong.net/2008/04/26/development-the-fall-line-and-process/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 06:43:43 +0000</pubDate>
		<dc:creator>Kyoryu</dc:creator>
		
		<category><![CDATA[General development]]></category>

		<guid isPermaLink="false">http://www.wrong.net/?p=43</guid>
		<description><![CDATA[In skiing, there&#8217;s a concept called the &#8220;fall line.&#8221;  The fall line just means down.  But it doesn&#8217;t mean &#8220;towards the bottom of the hill,&#8221; it just means what is immediately down from the exact location you&#8217;re at - if you dropped a ball, or poured some water, which way would it go?
This is important [...]]]></description>
			<content:encoded><![CDATA[<p>In skiing, there&#8217;s a concept called the &#8220;fall line.&#8221;  The fall line just means down.  But it doesn&#8217;t mean &#8220;towards the bottom of the hill,&#8221; it just means what is immediately down from the exact location you&#8217;re at - if you dropped a ball, or poured some water, which way would it go?</p>
<p>This is important in skiing, because you have to stand perpendicular to the fall line if you don&#8217;t want to keep moving.</p>
<p>There&#8217;s also a fall line in development.  The fall line in development is simply what is the easiest thing to do at the time to solve the immediate problem.  This can also vary by individual knowledge level.</p>
<p>In C, if you want to add some function, the fall line is just to declare it before you need it, and go on your merry way.  If you need it multiple places in the same file, put a declaration at the top of the file.  Only if you actually need it in another file is there any reason to expose it via header.</p>
<p>C++ is kind of opposite in this aspect - if you want something to be a class member, it <strong>has</strong> to be included in the header.  So a lot of little internal methods that, in C, may have been completely hidden from the world become exposed, at least to the extent that they&#8217;re in your header.  Yes, you can do things like separate implementation classes, but that&#8217;s, again, adding more work.</p>
<p>Because of this, I firmly believe that in any kind of API development, you want the easy thing to be the safe thing to do.  If something takes more effort to do, people will avoid it unless they absolutely need to do it.</p>
<p>Sometimes, the easy thing to do in a language, or application, isn&#8217;t the right thing to do.  Sometimes it&#8217;s the wrong thing to do.  And the typical answer to that is to add process, and force people to do the right thing.</p>
<p>I&#8217;m going to suggest that all process can do is add inefficiency, and by doing so, change the fall line of development.</p>
<p>That&#8217;s not necessarily a bad thing - if the fall line leads you to write bad code, then adding inefficiencies to gain later benefits can be very useful!</p>
<p>But, there&#8217;s two things to keep in mind:</p>
<p>First, if people don&#8217;t understand the purpose of the process, they&#8217;ll just follow the steps of the process while continuing, essentially, the same underlying behaviors.  Imposing a process alone will generally not change the mindset of anybody.  If you&#8217;re really looking to get a change in behavior, you&#8217;ll have to do that through education.</p>
<p>Secondly, the reason that process works is because it adds inefficiencies, and people will avoid inefficiencies.  The more heavyweight a process is, the less it will be used.  If your checkin process takes a day to navigate, then people will avoid checkins, and do them in large batches.  And that might partially defeat the purpose of your checkin process in the first place.</p>
<p>You should always use the minimum process that you can, and use process deliberately to add inefficiencies.  The goal of a process should never be to get people to do what they <strong>should </strong>do, but rather to get people to not do things that they shouldn&#8217;t.</p>
<p>I&#8217;m a big TDD proponent.  I would never suggest that somebody institute a TDD process that was mandatory.  If you don&#8217;t &#8220;get&#8221; TDD, then all you&#8217;ll do is write bad tests, and make the unit test suite less usable to me, without even getting any of the benefits.</p>
<p>Instead, I&#8217;d make a process (probably as part of the build) that ran the appropriate suites, and make some kind of check to make sure they were either run prior to checkin, or immediately afterwards.</p>
<p>Yes, I <strong>want</strong> people to write tests.  But the best way to get them to do that is by example, and showing them how useful the tests can be.  What I <strong>don&#8217;t</strong> want is for people to break the tests, rendering them useless.  And, I don&#8217;t want poorly written tests that are likely to fail due to configuration issues, or take forever to run.</p>
<p>If I make users manually run tests, it&#8217;s another process that they have to do - they&#8217;ll put it off as long as they can.  And if there&#8217;s a break, they&#8217;ll probably have worked well past the original error, and have to rework a lot of code in order to get everything to work again.  They&#8217;ll end up hating the unit tests, and the quality of the suite will degrade.</p>
<p>If the tests are in another directory, developers will have to sync two directories, and switch directories to build/run tests.  Again, this will make them run them less often.</p>
<p>By making the tests run as part of the build process, I make it hard to break the tests.  If the tests break, you don&#8217;t get a successful build.  Now, you have to circumvent the build to get anything done.  If it&#8217;s your change that broke the tests, it&#8217;s probably easier to just fix your code, since you&#8217;ll have to do it anyway.</p>
<p>And, if the tests are part of the build, slow or failure prone tests will become painful for everyone - as long as it&#8217;s easier to move the tests to the appropriate suite than it is to circumvent the build, they&#8217;ll do that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wrong.net/2008/04/26/development-the-fall-line-and-process/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A common unit testing pattern, part 3</title>
		<link>http://www.wrong.net/2008/03/19/a-common-unit-testing-pattern-part-3/</link>
		<comments>http://www.wrong.net/2008/03/19/a-common-unit-testing-pattern-part-3/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 04:55:02 +0000</pubDate>
		<dc:creator>Kyoryu</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.wrong.net/2008/03/19/a-common-unit-testing-pattern-part-3/</guid>
		<description><![CDATA[And this time I&#8217;m going to drift into theory-land for a bit, and I&#8217;m not particularly fond of theory-land.&#160; I prefer &#34;getting-stuff-done-land,&#34; which is quite often located on another continent.
Conceptually, objects are these things that sit in space and receive and spit out messages.&#160; And if we look at our User class in that way, [...]]]></description>
			<content:encoded><![CDATA[<p>And this time I&#8217;m going to drift into theory-land for a bit, and I&#8217;m not particularly fond of theory-land.&#160; I prefer &quot;getting-stuff-done-land,&quot; which is quite often located on another continent.</p>
<p>Conceptually, objects are these things that sit in space and receive and spit out messages.&#160; And if we look at our User class in that way, it kind of makes sense.&#160; We define the behavior of the User object as receiving a message to set its name, and when it does so, it should then emit a message that it should be saved.</p>
<p>And this is a pretty convenient way to think of objects.&#160; It also isn&#8217;t modeled well by most object-oriented languages.</p>
<p>C++-derived languages handle relationships of &quot;is-a&quot; and &quot;has-a&quot; types very well.&#160; But, what we really want here is a &quot;talks-to-a&quot; relationship.&#160; There&#8217;s no real first-class way to do that.</p>
<p>Also, they handle incoming messages very well, by means of the public interface of an object.&#160; But, outgoing messages are a little tougher.&#160; Sure, you can do all sorts of things with message receivers and emitters, but realistically, people are going to use the built-in constructs of the language.&#160; So, if we want to model objects as message receivers (easy) and emitters (not easy), we should do it in as basic a way as possible.</p>
<p>And now we get back to &quot;getting-stuff-done-land.&quot;&#160; Looking at our User class from the past two entries, we can look at the interface we define as the outgoing messages that the object can emit.&#160; Viewed in that way, it makes sense that the object would own that interface - who else would?&#160; And why would an outgoing message from a user know anything about a database?</p>
<p>There&#8217;s other ways to model an outgoing message, and they all pretty much boil down to function pointers.</p>
<p>First, you can use virtual protected methods to define messages, and then override them in derived classes to translate them to the incoming messages that are desired by another component/object.&#160; This method has the advantages of looking more traditional, but the disadvantage of not being able to mix-and-match receivers at runtime.</p>
<p>Secondly, you can use events and delegates.&#160; Combined with lambda expressions in C# 3, this can be an effective way to define outgoing messages.&#160; It has the advantage of not requiring additional classes, but the disadvantage is that you have to hook up each event individually.&#160; Also, events can provide multiple dispatch, but that can become wonky if you want to remove individual anonymous delegates, so I&#8217;m counting that as a neutral.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wrong.net/2008/03/19/a-common-unit-testing-pattern-part-3/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A Common Unit Testing Pattern, Part 2</title>
		<link>http://www.wrong.net/2008/03/10/a-common-unit-testing-pattern-part-2/</link>
		<comments>http://www.wrong.net/2008/03/10/a-common-unit-testing-pattern-part-2/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 18:03:53 +0000</pubDate>
		<dc:creator>Kyoryu</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.wrong.net/2008/03/10/a-common-unit-testing-pattern-part-2/</guid>
		<description><![CDATA[So, reading the last post, it might seem like I&#8217;m advocating a lot of extra work to do anything.&#160; Having each class create its own interfaces for everything probably seems a bit insane.&#160; Wouldn&#8217;t it be better to define a single, universal interface for, say, a database?
Short answer:&#160; no.&#160; By doing so, you&#8217;ll inevitably end [...]]]></description>
			<content:encoded><![CDATA[<p>So, reading the last post, it might seem like I&#8217;m advocating a lot of extra work to do anything.&nbsp; Having each class create its own interfaces for everything probably seems a bit insane.&nbsp; Wouldn&#8217;t it be better to define a single, universal interface for, say, a database?</p>
<p>Short answer:&nbsp; no.&nbsp; By doing so, you&#8217;ll inevitably end up intermingling aspects of the dependency with your core code.&nbsp; In the User example in the previous post, even if you abstract the DBConnection to an IDatabase, you still have to deal with the fact that you&#8217;re now tied to a database, and have intermingled knowledge of the database layout and SQL variant you&#8217;re using into your User class.</p>
<p>The problem is that each class has its &#8220;natural&#8221; language.&nbsp; The &#8220;natural&#8221; language of the User class is &#8220;save the user with this id and this name&#8221; (and whatever other information is appropriate).&nbsp; The natural language of the database is &#8220;run this sql command.&#8221;</p>
<p>Just as it would be inappropriate to put code specific to the User object in the database, it&#8217;s also inappropriate to put database specific code in the user.</p>
<p>So, instead, what we can do is create small classes that take the output of one class (the User, in this case), and turn it into the input expected by the other class (the database, file system, etc.).&nbsp; There&#8217;s no real extra code required to do this, as all of the code would have to be written ayway.&nbsp; By doing this, you remove the knowledge of this glue code out of either of the two classes, and allow it to live in its own classes, possibly even its own assemblies.</p>
<p>There are times, though, that you do want to maintain a common interface between classes.&nbsp; But only do this if the classes in question naturally end up with the same interface, or it&#8217;s a &#8220;natural&#8221; dependency - if you&#8217;re writing something that retrieves data and writes to a disk, then a mocked file system is appropriate.&nbsp; An FTP client would be a good example of this, as there wouldn&#8217;t really be a point to writing an FTP client that saved to a database.</p>
<p>Additionally, if the &#8220;glue&#8221; classes are more than simple pass-through classes, it may be worthwhile to test them more thoroughly, and so mocking their underlying data store may be appropriate.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wrong.net/2008/03/10/a-common-unit-testing-pattern-part-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A common unit testing pattern</title>
		<link>http://www.wrong.net/2008/03/06/a-common-unit-testing-pattern/</link>
		<comments>http://www.wrong.net/2008/03/06/a-common-unit-testing-pattern/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 06:52:02 +0000</pubDate>
		<dc:creator>Kyoryu</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.wrong.net/2008/03/06/a-common-unit-testing-pattern/</guid>
		<description><![CDATA[There&#8217;s two things you frequently hear when talking to developers about TDD.&#160; It&#8217;s either &#34;our code is too complicated, you can&#8217;t separate it&#34; or &#34;how can you test a database&#34; (or graphics system, or insert other complicated system).
Here&#8217;s an example of code that someone might have in mind.
public class User
{
    private DBConnection [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s two things you frequently hear when talking to developers about TDD.&#160; It&#8217;s either &quot;our code is too complicated, you can&#8217;t separate it&quot; or &quot;how can you test a database&quot; (or graphics system, or insert other complicated system).</p>
<p>Here&#8217;s an example of code that someone might have in mind.</p>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">User
</span>{
    <span style="color: blue">private </span>DBConnection connection;
    <span style="color: blue">public string </span>Name
    {
        <span style="color: blue">set </span>{ SetName(<span style="color: blue">value</span>); }
    }

    <span style="color: blue">private void </span>SetName(<span style="color: blue">string </span>name)
    {
        <span style="color: blue">this</span>.name = name;
        <span style="color: green">// other stuff&#8230;

        </span>connection.ExecuteSQL(<span style="color: #a31515">&quot;update users set name = &#8216;&quot; </span>+ name + <span style="color: #a31515">&quot;&#8217; where id = &quot; </span>+ id);
    }
}</pre>
<p>So, usually someone will want to create a mock object for the database for testing purposes.&#160; That&#8217;s one way to do it.&#160; But, I&#8217;m not sure it&#8217;s the best way.&#160; After all, you want to test your class, not your understanding of the database!</p>
<p>There&#8217;s another issue here.&#160; The User class knows waaaaay too much about the database for its own good.&#160; I mean, what if we wanted to save to a file system?&#160; We can&#8217;t, at least without serious rework.&#160; While we could replace the database connection with a different type, we&#8217;re still tightly coupling to the idea of a database.</p>
<p>What we really want to do is save the user info.&#160; So let&#8217;s just make a method that does that.</p>
<pre class="code"><span style="color: blue">private void </span>SetName(<span style="color: blue">string </span>name)
{
    <span style="color: blue">this</span>.name = name;
    <span style="color: green">// other stuff&#8230;
    </span>SaveUser();
}

<span style="color: blue">private void </span>SaveUser()
{
    connection.ExecuteSQL(<span style="color: #a31515">&quot;update users set name = &#8216;&quot; </span>+ name + <span style="color: #a31515">&quot;&#8217; where id = &quot; </span>+ id);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>Okay, that&#8217;s a little better.&#160; Now we&#8217;ve removed the implementation details of saving the user from the SetName method.&#160; It now pretty much says what, conceptually, we want to do.&#160; All the database stuff is in its own method.</p>
<p>But, being in its own method doesn&#8217;t really allow us to change anything.&#160; So, let&#8217;s extract an interface.</p>
<pre class="code"><span style="color: blue">public interface </span><span style="color: #2b91af">IUserPersistence
</span>{
    <span style="color: blue">void </span>SaveUser(<span style="color: blue">int </span>id, <span style="color: blue">string </span>name);
}

<span style="color: #2b91af">IUserPersistence </span>persistence;

<span style="color: blue">private void </span>SetName(<span style="color: blue">string </span>name)
{
    <span style="color: blue">this</span>.name = name;
    <span style="color: green">// other stuff&#8230;
    </span>persistence.SaveUser(id, name);
}</pre>
<p>And now, our user class doesn&#8217;t even have to know about a database at all, or maintain a reference to its connection.&#160; Instead of giving it a DBConnection, we just give it a IUserPersistence object.&#160; We can then test away to our heart&#8217;s content, throw whatever exceptions we want, without having to worry about trying to mock anything as heavyweight as a whole database.&#160; We also remove all database specific code from our user class, allowing it to be reused elsewhere.</p>
<p>It&#8217;s also conceptually cleaner - the user class now only has to know about the idea of saving itself, not the specific details.</p>
<p>The drawback of this is that it does potentially pollute the API space a bit.&#160; One way to get around this is to set the instance of the interface to protected, and not expose them on the default object.&#160; Then, you can write a test object which inherits from the normal class which exposes a setter.</p>
<p>So, the answer to &quot;how do I mock a database&quot; is simple - you don&#8217;t.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wrong.net/2008/03/06/a-common-unit-testing-pattern/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Little Games</title>
		<link>http://www.wrong.net/2008/03/04/little-games/</link>
		<comments>http://www.wrong.net/2008/03/04/little-games/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 19:20:35 +0000</pubDate>
		<dc:creator>Kyoryu</dc:creator>
		
		<category><![CDATA[Game development]]></category>

		<guid isPermaLink="false">http://www.wrong.net/2008/03/04/little-games/</guid>
		<description><![CDATA[Recently, I&#8217;ve been finding myself spending way too much time thinking about tools for game development, and not doing enough game development.  So, after I get through the current crunch state I&#8217;m in, I think I&#8217;m going to set a goal for myself to write a game in a week, no matter how small in [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been finding myself spending way too much time thinking about tools for game development, and not doing enough game development.  So, after I get through the current crunch state I&#8217;m in, I think I&#8217;m going to set a goal for myself to write a game in a week, no matter how small in scope.</p>
<p>When you have no timeframes and no set deliverables, it&#8217;s easy to get lost in theory-land, or what-is-the-best-way-to-do-this-land, or this-tool-isn&#8217;t-quite-perfect-land, or any number of other traps.  A hard deadline of a week seems like a great way to force myself past those.</p>
<p>The rules will be simple.  I will set a start date.  Seven days from then, I am done.  Anything not completed, is not completed.</p>
<p>The basic plan I have is to have a minimal basic idea of gameplay, and incrementally develop it primarily using TDD.  I am going to focus on a minimal, complete game before I focus on adding a ton of features.  The hope is that by doing so, I&#8217;ll have a game that&#8217;s ready to &#8220;ship&#8221; early on, and additional features will just be that.</p>
<p>I&#8217;m allowing myself the time before I start to investigate tools and think of gameplay ideas.  I will not be using the time before the &#8220;official&#8221; start to write code/do spikes/etc.</p>
<p>I&#8217;m looking forward to this.  It&#8217;s a chance to look at several of my development theories, and see how they pan out.</p>
<p>OTOH, I don&#8217;t think I&#8217;ll repeat it indefinitely.  There&#8217;s stuff I want to do that&#8217;s definitely not possible within a week.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wrong.net/2008/03/04/little-games/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
