CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

November 2005 - Posts

  • Does Effective TDD Require OOP?

    I’ve had several conversations lately around the idea that Test Driven Development either requires or enforces good Object Oriented Programming.  I’ve always been a bit unclear in my own mind on whether doing TDD leads me to writing better OOP, or whether I try to follow good OOP practices just to take advantage of TDD with a minimum of friction.  What I can tell you from painful experience is that TDD will expose badly factored OO code.  You might say that doing TDD is like an audit on your OO design.  Anytime doing TDD on your code becomes difficult you need to reexamine your class structure. 

     

    One of my coworkers likes to say that TDD just leads you to a different solution.  I’d argue instead that TDD might just be putting a microscope onto your code to find flaws, and we all dress better and stand up straighter when more eyes are upon us.  Besides, I’d say that the code qualities that lead to testability are loose coupling, high cohesion, and a thorough separation of concerns.  In other words, just plain, good, clean Object-Oriented code like we should have been writing anyway.  One of the nice facts about TDD is that testability quite happily coincides with being good code.

     

    I’ve heard a couple people say that you’ll understand OOP much better when you start trying to do TDD.  I can certainly echo this experience.  I thought I was an OO guru because I could spout off GoF patterns at will.  On my first project with TDD I was abused of this notion in a painful manner.  I understood basic layering, but I didn’t know how to truly isolate business and user interface functionality away from the database.  I let an overly complex security scheme get in the way of even the simplest business logic testing*.  I guess you’d say my first lesson in Inversion of Control/Dependency Injection was learning to push a custom permission set into code during unit tests without having to wire up data in a database or configuration files.  Once I made the refactoring to make the permission set decoupled from external dependencies, unit testing started to get easier.  The light definitely came on in my head after that.

     

    Writing top down procedural code will not lead to testable code.  You simply cannot create code that can be effectively unit tested in the TDD style with procedural code.  You can’t isolate functionality to the same degree.  Loose coupling is nearly impossible.  What’s even more difficult is trying to retrofit unit tests to existing procedural code.  A terrible, but common, trap to fall into as a TDD newbie is to forgo writing the tests first when you don’t know how to write the test.  Just starting to code thinking you’ll write the automated tests later after you make the code work is a slippery slide into technical debt hell.  The resulting code isn’t written with testability in mind and the newbie will often find retrofitting the new code with unit tests laborious or even impossible.   The usual outcome is either the tests are too coarse-grained to be easily understood and debugged, or the test coverage is lacking.

     

    You simply must make testability be in the forefront of your mind anytime you’re writing code.  For TDD, it’s a better approach to decompose a coding task into the distinct responsibilities and code the constituent classes in order to build the whole from the bottom up.  Pick off the first responsibility, code a class or method to do only one responsibility, rinse, and repeat.  Only when you have the individual pieces working and unit tested do you glue them together and test the whole.  Often times I find that it’s easier to “see” the whole picture after a few of the pieces are built.  Little self-contained pieces of code with minimal coupling on other pieces of code will be easier to test.

     

    I think a lot of people struggle with TDD because they’re used to writing procedural code.  Learning to effectively decompose code into classes with well defined responsibilities is essential to doing TDD without pulling your hair out.  I’m not trying to bash procedural coders.  I’m just trying to warn them that they’ll want to work in a more OO fashion to reap all of the benefits of TDD.

     

    Your TDD experience will be much more pleasant and successful with stronger OO skills.  You have to be cognizant of coupling and cohesion properties for every single class interaction and method signature to keep the unit tests and code flowing.  A TDD Jedi is fully steeped in OO programming and is mindful of the qualities of his OO decisions.  Writing willy-nilly procedural code without unit tests is a seductive path to the Dark Side.

     

    Am I being too harsh about procedural coding?  Is good OO synonymous with effective TDD, or am I just being an OO bigot?  Fire away.

     

    EDIT 11/30/2005:  Take a look at Bill Caputo's post at http://www.williamcaputo.com/archives/000245.html for a thoughtful look beyond OO for Agile development.

     

     

    * Dave, Levi, Vivek – if you’re out there, here’s a belated “I’m sorry”

  • Potpourri from Scott Bellware’s TDD Event

    Just some random thoughts from Scott Bellware’s TDD event in Houston yesterday…

     

    Something that I hear a lot of TDD newbies and skeptics complain about is doing things in code for no other reason than to enable automated unit testing.  My take on the subject is to just get over it.  Anything that makes it easier to write your unit tests makes it easier to make your code work.

     

    A subject that I go back and forth on in my own mind is opening up methods or members on classes for no other reason than for testability.  It definitely feels icky.  When there is an internal step in a class that is complicated enough that it needs to have a unit test all by itself but doesn’t really deserve to be part of the real public interface I might pull that step into another class or make a public static method for that step if it’s relatively stateless.  If I do just open up the intermediate member I usually rationalize it by not including this step in the abstracted interface that other classes depend on.  For example, I might create a method on a ConcreteService class for PerformIntermediateStep(), but that method would never be a part of the IService interface that ConcreteService implements.

     

    There were a couple questions on how to enforce TDD practices throughout a team.  There was also some discussion on how a developer knew his unit testing was robust enough and considered all of the cases.  One way or another success still falls into a question of the skill, work ethic, discipline, and experience of the developers doing the work.  That’s why we’re paid the big bucks.

     

    J. Sawyer, our MS evangelist in the Austin area, shared the best idea for a shame card that I’ve ever heard of.  In J. Sawyer’s version you have the shame shirt throughout the project.  When you break the build you have to wear the shirt.  The kicker is that the shirt is never washed, so it’s quite a punishment by the time you get a ways into the project.  I think we’ll pass on that though.  I’m having flashbacks to my high school football days and it’s not a pretty (smelling) memory.

     

    Whatever your coding standard is, make it easy to follow.  If you have to go out of your way to do some sort of exact spacing or code format I think you’re just wasting time.  Learn to tolerate the default formatting in VS.Net and set your naming preferences in ReSharper and just let the tools do the automated formatting.  I honestly think that your entire coding standard should fit on a single wiki page without much scrolling.  I want to be able to just go “CTRL-F” and get my code automatically formatted.  Let the computer do the grunt work formatting so the human beings can do the thinking.  Otherwise we might get Skylab and the Terminators running around.

     

    Writing that first unit test can be the most difficult task in TDD.  We’re taught that to be successful you need to deal with the riskiest, hardest tasks first.  I agree, but within any given TDD coding session starting with something easy to do first is a perfectly acceptable way to get going.  Once you have some working code it often makes it easier to “see” the rest of the code.

     

    Scott Bellware had a thoroughly humorous rip on the “Executable UML” will o’ wisp* yesterday, but UML in small dosages is not evil.  If you don’t know what or how to write the next unit test, a bit of UML sketching can be an effective way of determining the next unit test to write.  Too much UML can easily lead you astray with early complexity, but writing the tests first can clamp down on overdesign.  I still think Responsibility Driven Design with or without CRC cards to be more effective as a supplement to TDD than UML, but your mileage will vary. 

     

    Doing TDD in Visual Studio.Net without ReSharper (insert favorite refactoring tool here) shortcuts and templating is painful.  Watching Scott perform an introductory exercise on TDD while purposely forgoing any ReSharper magic was excruciating.  ReSharper definitely makes TDD faster by removing friction and automating purely mechanical tasks.

     

    Houston traffic sucks. 

     

     

    * Thank you Scott for pointing out that Executable UML requires a great deal of OCL markup, i.e. code

  • Ordering Code Construction Tasks

    I spent a couple years as an engineer on large petrochemical construction projects.  A lot of the mental work in a large construction project is determining the order of construction for the various subsystems and physical structures of the plant.  In construction that order is more or less governed by physical realities and it’s a well understood problem domain.  In the world of software development determining the order of coding construction is more nebulous because we’re not bound by physical laws.  All the same though, the success of our projects is influenced by the order in which we code the pieces of the project. 

     

    Framework Fool’s Gold

     

    I’ll freely admit it; I’m a recovering “framework-aholic” that’s had some bouts with “pattern-itis” as well.  That’s what makes me qualified to say that one of the worst project anti-patterns in software development is building an elaborate framework first.  It’s a seductive path.  On one hand there’s the whispered belief that if we can just get the core framework right, building new services/screens/controllers/applications will be easy, right?  On the other hand, it’s just plain fun to exert your creativity to the maximum to build the ultimate metadata-driven, pluggable framework so new features can be added without any new code. 

     

    There are a couple little problems with building frameworks upfront.  The main problem is that it very rarely works.  You’re essentially guessing what the framework needs to do, and even the best architects can easily guess wrong.  Even if you’re the best architect in the world, the folks feeding you the requirements are always changing their minds or coming to a different understanding of their needs.  Building an all-encompassing framework outside the scope of a specific business project almost always results in wasted effort.  The worst case scenario is that you’re framework won’t deliver any value and results in a lot of rework.  The best case scenario is that your framework does work, but you don’t reap much early feedback or have any opportunities to deploy the system early.

     

    Too much abstraction with an overarching framework early on can make an application viscous.  Uncle Bob describes design viscosity as a malady that makes a design harder to preserve as changes are made.  If it’s easier to make a change by going around a framework or drilling exception cases into a framework with “if/then” statements, you’ve created a viscous framework that’s might be causing you more harm than good.  If you start to hear the phrases “can’t we just go around the framework?” or “this would be easier without the framework” or even worse “we need to get [Some Guy] to change the framework first” you’ll know that you’re going down a painful path with your framework.

     

    The worst is when the framework turns into the mutant plant from the Little Shop of Horrors as it takes on a perverted life of its own.  “Feed me Architect, Feed me!”

     

    To give you an anecdote that’ll get me in trouble when they read this, a part of my team this past spring was embarking on an overly elaborate, future-proofed architecture for our product rewrite.  They were having fits debugging and testing the application in large part due to some unnecessary technical infrastructure (and severe performance problems down the road).  About halfway through the project they decided to simplify their architecture.  The result was what they call the “Great Refactoring of Aught Five.”  Funny name aside, there’s no such thing as a big refactoring, only rewrites.  They would have clearly been better off starting simple without the abstractions and infrastructure (plus there’s still some dead code detritus lurking in the codebase we’ve got to kill off some day). 

     

    I proscribe (now) to the view that you should harvest a framework from an application only after it has been proven to be useful or necessary.  When you do build a framework, don’t try to create something grander than what you need right now.  I’d also say that it’s much, much better to “dogfood” a new framework as part of a project rather than trying to write a “Hammer without a Nail” framework that doesn’t really fit the needs of any project. 

     

    Slice the Work Vertically, Not Horizontally

     

    The metaphor of software development as construction is common, but horribly wrong.  Approaching software development as constructing a structure on top of a foundation, then adding the finishing trim can be a slow, painful path to project failure.

    Time and time again I’ve learned or observed that projects go much smoother when you build vertically instead of horizontally.  What I mean by this is that you build a new system by creating a feature at a time with the entire stack of UI, business logic, and database work to make the feature fully functional.  As you build new features you religiously eliminate duplication to avoid creating a Stovepipe anti-pattern and harvest reusable code.

     

    One of the more disappointing project failures I’ve observed was the very first .Net pilot project in a VB6 shop.  I wasn’t involved with the project, but I had a stake in the project as it was going to prove the viability of .Net for application development and also be a pilot for doing iterative development.*  Unfortunately they blundered by crafting an iteration plan that called for them to write the entire data access layer first, the business layer second, then finally put together the user interface.  It failed miserably.  When the project was cancelled midway they didn’t have the slightest bit of deployable code because they had focused solely on the backend database.  One of the other things they found out was that the data access they coded wasn’t exactly what the business and UI layers needed.  In other words, they had wasted effort by speculatively creating code that turned out to be unnecessary. 

     

    One of the best, positive lessons I’ve learned working on Agile projects is the importance of getting feedback early.  You can’t get much usable feedback from the end user when all you have to show is database diagrams or UML class diagrams (a word of advice, don’t ever show a customer a UML diagram unless it’s some kind of experiment in hypnosis).  Besides, you know damn well that the thing most likely to change is the user interface.  You’ll also understand any piece of the system better when you’ve built the entire chain of calls.  You simply cannot know any piece of code is done until its clients are complete.

     

    I did a project about 3 years ago where I created an elaborate, overly generalized workflow framework first before creating any working user interface.  The framework worked fine and the system still humming along, but the lack of a UI became a problem.  The other members of the team didn’t truly understand the point of the framework because they didn’t have a set of screens to see the concrete usage of the framework.  The users and the project manager had no idea what our progress was because they couldn’t see anything working.  We didn’t get any usability feedback from our user base until dangerously late in the project.  Yeah, you can claim earned hours for the code you create, but what value does that really represent?  The Agile methods have gotten it exactly right by tracking progress by user stories completed, and defining that “done, done, done” for each user story requires customer approval.  And besides, the customer probably only counts a working user interface as progress anyway.

     

    One of the desirable qualities of an iterative process is being able to deploy a partially complete system if the project has to come to an unexpected stop due to changing priorities or funding problems.  The only way to accomplish that goal is to build an application feature by feature rather than layer by layer.

     

    Client First

     

    Earlier this year there was a raging argument on the web on the merits of writing web services “Code First” (generate WSDL from code) versus “Contract First” (generate code from WSDL)** that I mostly ignored.  I think there’s a time and place for both approaches, but the one constant is to write (or at least simulate) the client first whenever possible.  The requirements and the signature of any service will be largely determined by the needs of its clients.  “Client First” doesn’t just pertain to web services, it useful for any class that’s a service provider to other classes.  If you’re doing Test Driven Development (or not I suppose), you might use a mock object or a stub to be a stand in for a service while you concentrate on creating the service client.   Once they’re completed, the unit tests for the service client will very accurately define the requirements for the service.  For the obvious example, don’t write data access or persistence code long before the business logic that needs to be persisted.  I know some SOA enthusiasts who have a “build it and they will come” mentality to creating web services left and right, but what’s the use of building something that nobody wants?

     

    Configuration Last

     

    When it is time to build some sort of configurable framework, my strongly worded advice is to concentrate on the classes that do work first, and worry about the configuration strategy second.  I read that years ago in Craig Larman’s excellent book on design patterns and I clearly see the wisdom in that advice.  StructureMap is a working dependency injection tool, but it started its life as the world’s greatest ORM tool.  I got bogged down obsessing with the configuration infrastructure and never wrote a single line of actual O/R mapping code.  Only later on did I take the abandoned configuration code and turn it into a dependency injection tool to use on a project.  I still wonder what would have happened if I’d been able to release a solid O/R mapping tool for .Net 3 years ago.

     

    Besides, I think StructureMap and the other open source IoC/DI tools make it ridiculously easy to create configurable plugin strategies compared to what it took 3 or 4 years ago.  Just push all configuration information and dependencies in through constructors or setter properties and let the IoC/DI tool put things together for you later.

     

     

    * This is a terrible example of working iteratively.  The idea of working iteratively and incrementally is to build fully working pieces of the application, one at a time.  What they did was just BDUF + Waterfall with some trappings of iterative development.

     

    ** I had a colleague in my old architecture team who thought all of us should be able to write WSDL by hand as a qualification for being on the team.  I blew him off while the others nodded fervently.  There’s a long list of holes in my resume I wish I had more familiarity with, but writing WSDL by hand isn’t on that list at all.

  • Introduction to using StructureMap for Dependency Injection

    StructureMap is an open source tool I’ve written for Dependency Injection support in .Net.  This past week I’ve had a couple requests for a gentle introduction to StructureMap, so here’s my first attempt.  Typically I use StructureMap in a handful of ways:

     

    1. Configuration and creation of services (little “s”) that my code depends upon.  By service here I don’t necessarily mean SOA and web services, but web service proxy classes are an automatic candidate for StructureMap configuration.  I’m referring to subsystems of your application like logging modules, persistence, gateways to other systems, or really anything that you don’t want your code to be tightly coupled to.
    2. Object composition.  Code reuse and unit testing is easier when you favor composition over inheritance relationships, but composition implies that you need to some mechanical coding work just to put the object graph together.  This is where StructureMap comes into play.
    3. Plugin support.  Occasionally there is a real need for pluggable, extensible application frameworks.

     

     

    A Contrived Example

     

    Let’s say you’re an integration developer in a shop with a crazily mixed bag of applications floating around a big central database.  You want to write a pluggable infrastructure for batch scheduling little integration tasks.  You may need to pull data by a sql query, a stored procedure, or by opening a file – then do something with the data.  All you really know is that the possible sources of the data and the actions on the data will change over time.  You might decide to create a pluggable framework using StructureMap to handle the configuration (Okay, in reality you would probably buy something off of the shelf or utilize DTS out of the box, but I needed an example).

     

    First off, let’s ignore the configuration schema and concentrate on defining a set of interfaces and roles for our “Trigger” framework.  The main interface will be this:

     

          public interface ITrigger

          {

                // Do whatever it is that you do

                void Execute();

     

                // Tell me when you want to run next

                DateTime NextExecutionTime();

          }

     

    Configuring the most basic trigger class will define the mechanism to retrieve the data, an action to perform on the data, and a strategy for determining the next run time (next Monday, the top of the next hour, the last day of the month, etc.).  That adds the following code:

     

          [Pluggable("Basic")] // Tell StructureMap that it "builds" this class and that it's alias is "Basic"

          public class Trigger : ITrigger

          {

                private readonly IScheduler _scheduler;

                private readonly IDataSource _source;

                private readonly IAction _action;

     

                public Trigger(IScheduler scheduler, IDataSource source, IAction action)

                {

                      _scheduler = scheduler;

                      _source = source;

                      _action = action;

                }

     

                public void Execute()

                {

                      DataTable table = _source.FetchTable();

                      _action.Process(table);

                }

     

                public DateTime NextExecutionTime()

                {

                      return _scheduler.GetNextRunTime(DateTime.Now);

                }

          }

     

          public interface IDataSource

          {

                DataTable FetchTable();

          }

     

          public interface IAction

          {

                void Process(DataTable table);

          }

     

          public interface IScheduler

          {

                DateTime GetNextRunTime(DateTime currentTime);

          }

     

    The first Trigger that we’re going to need to build is going to daily run a SQL query against the central database, and then email the results to an unlucky support person for resolution.  First we create the little classes for the scheduling, data source, and the action.

     

          [Pluggable("SQL")]

          public class SqlDataSource : IDataSource

          {

                private readonly string _sql;

                private readonly IDatabase _database;

     

                public SqlDataSource(IDatabase database, string sql)

                {

                      _sql = sql;

                      _database = database;

                }

     

                public DataTable FetchTable()

                {

                      return _database.FetchDataTable(_sql);

                }

          }

     

          [Pluggable("Email")]

          public class EmailAction : IAction

          {

                public EmailAction(string to, string body){…}

                public void Process(DataTable table){…}

          }

     

          [Pluggable("Daily")]

          public class DailyScheduler : IScheduler

          {

                public DailyScheduler(){}

                public DateTime GetNextRunTime(DateTime currentTime){…}

          }

     

    SqlDataSource itself depends upon the IDatabase interface that provides access to an underlying database and handles the raw ADO.Net manipulation.  Let’s say that the central database is a Sql Server database (but I’m starting to miss good old reliable Oracle).  That adds the following to the code:

     

          public interface IDatabase

          {

                DataTable FetchDataTable(string sql);

                void SetStoredProcedureName(string storedProcedureName);

                void SetParameter(string parameterName, object parameterValue);

                DataTable FetchDataTableFromStoredProcedure();

          }

     

          public class SqlDatabase : IDatabase {…}

     

    Okay, now we can finally make our StructureMap configuration to create the object graph for our first Trigger.  The first thing we need to do is create a file named “StructureMap.config” that will be copied to the application directory.  At a minimum, we need to define four things for StructureMap:

     

    1. <Assembly> - The .Net assemblies to scan for possible interfaces and classes to be built by StructureMap
    2. <PluginFamily> - The .Net types that can be requested from StructureMap.  In our case this would be the ITrigger, IDataSource, IAction, and IScheduler interfaces.  It can also be abstract or concrete classes as well.
    3. <Plugin> - The concrete .Net types that will implement the interfaces of the PluginFamily types.
    4. <Instance> - The actual object graph configurations.  Notice that we didn’t do anything special in our classes to enable StructureMap (except the [Pluggable] attributes).  All you need to do to enable your class to be configured by StructureMap is to take in all dependencies and properties in a constructor function.  StructureMap also supports “Setter Injection,” but that’s really just meant for Types that aren’t under your control.

     

    Jumping into the StructureMap.config file we’ll first configure the default IDatabase instance to point to the central Sql Server database.

     

    <?xml version="1.0" encoding="utf-8" ?>

    <StructureMap>

          <!-- Tell StructureMap to scan the StructureMapSample.dll assembly for possible PluginFamily's and Plugin's -->

          <Assembly Name="StructureMapSample" />

         

          <!-- Define a PluginFamily for IDatabase, making the default instance "CentralDatabase" -->

          <PluginFamily Assembly="StructureMapSample" Type="StructureMapSample.IDatabase" DefaultKey="CentralDatabase">

                <!-- Tell StructureMap that the SqlDatabase class is a plugin to IDatabase called "MicrosoftSqlServer" -->

                <Plugin Assembly="StructureMapSample" Type="StructureMapSample.SqlDatabase" ConcreteKey="MicrosoftSqlServer"/>

               

                <!-- Configure the "CentralDatabase" instance of IDatabase.  The <Property> nodes define the arguments to the constructor

                functions -->

                <Instance Key="CentralDatabase" Type="MicrosoftSqlServer">

                      <Property Name="connectionString" Value="connection string to somewhere..."/>

                </Instance>

          </PluginFamily>

    </StructureMap>

     

    With this configuration we could at anytime in our code type IDatabase database = (IDatabase) ObjectFactory.GetInstance(typeof(IDatabase)); to get a configured instance of the SqlDatabase class with the connection string set.  If the database changed to Oracle or DB2 later, only the configuration to create a different concrete IDatabase class would be changed (yeah right).  In this case I defined the Plugin explicitly in the XML configuration.  The [Pluggable(“My Plugin Alias”)] attribute is an equivalent mechanism to using the <Plugin> node.  I prefer the attributes wherever possible, but some people feel differently. 

     

    Moving on to the first Trigger instance, we would add a <PluginFamily> node for ITrigger and an <Instance> node for the object graph for the first Trigger.

     

          <PluginFamily Assembly="StructureMapSample" Type="StructureMapSample.ITrigger">

                <!-- Create a BasicTrigger object, and give it a DailyScheduler, -->

                <!-- Uses the public Trigger(IScheduler scheduler, IDataSource source, IAction action) constructor function -->

                <Instance Type="Basic" Key="MyFirstTrigger">

               

                      <!-- DailyScheduler -->

                      <Property Name="scheduler" Type="Daily" />

                     

                      <!-- SqlDataSource(IDatabase database, string sql) -->

                      <!-- By leaving off an explicit definition for "database" StructureMap will use the default

                           "CentralDatabase" instance -->

                      <Property Name="source" Type="SQL">

                            <Property Name="sql" Value="select * from errorlog"/>

                      </Property>

                     

                      <!-- public EmailAction(string to, string body) -->

                      <Property Name="action" Type="Email">

                            <Property Name="to" Value="poorfool@helpdesk.company.com"/>

                            <Property Name="body" Value="Fix these now!" />

                      </Property>

                </Instance>

          </PluginFamily>

     

    The Trigger framework can now create this instance by calling into StructureMap as something like this:

     

                public void RunTriggers()

                {

                      string[] triggersToRun = this.findTriggersToRun();

     

                      foreach (string triggerName in triggersToRun)

                      {

                            // Ask StructureMap to build the object instance

                            ITrigger trigger =

                                  (ITrigger) ObjectFactory.GetNamedInstance(typeof(ITrigger), triggerName);

     

                            trigger.Execute();

                            DateTime nextTime = trigger.NextExecutionTime();

     

                            this.rescheduleTrigger(triggerName, nextTime);

                      }

                }

     

    Additional trigger’s could be created by simply configuring the object graphs into the StructureMap.config file as a new <Instance> node.  The big advantage in this case is that StructureMap can quite happily accommodate any new classes that you create later to implement the ITrigger/IDataSource/IAction interfaces.  You could even “plugin” all new assemblies to be used by your application without recompilation.  That’s potentially a huge win for my company because of the sheer number of per client customizations that we have to do.

     

    There is far more information and an actual schema document at http://structuremap.sourceforge.net.  I threw this together in a hurry, so please let me know if this is useful or if something else is necessary to make the tool more clear.  Sometime in the near-ish future I’ll post much more on some of the new capabilities for configuration management and environment testing that we’re building into StructureMap for complex build and staging scenarios.

  • Microsoft’s recommendations for Test Driven Development are wrong!

    Okay, wrong is too harsh, but I made you look.  Microsoft has published a list of recommendations for doing Test Driven Development in VS 2005 at http://msdn2.microsoft.com/en-us/library/ms182521.aspx.  Before I start bashing MS here, let me say that it *is* a good thing that MS is even talking about best practices like unit testing and build automation.  That being said though, Microsoft seems to be trying to redefine TDD to match up with their tools instead of creating tools that support TDD best practices.

     

    Let’s begin…

     

                “3.) Make a list of tests that will verify the requirements. A complete list of tests for a particular feature area describes the requirements of that feature area unambiguously and completely.

                “4.) File work items for feature requirements and for the tests that need to be written.

     

    Quickly making a list of unit tests to write in a coding session is often a useful way to break the TDD ice and get started, but don’t ever assume that that list is final or correct.  It is nice to get a full set of acceptance tests for the feature before you start the coding, but writing all of the unit tests before starting coding is not efficient.  My experience has been consistent.  Anytime you write several unit tests in a row before you start implementing the code you end up wasting effort because you always learn something in the first test that invalidates the next unit test.  Michael Feathers in his retort talks about how you use the feedback from one unit test to write the next test.  I couldn’t agree more. 

     

    Oh, and creating a “work item” for each unit test I’m going to write?  I don’t think so.  Can you say “unnecessary overhead?”

     

                “6.) Define the interfaces and classes for your feature or requirement. You can add a minimum of code, just enough to compile. Consider using the Class Designer to follow this step

                “7.) Generate tests from your interfaces and classes. For more information

     

    No, no, no.  Autogeneration of the unit tests from interfaces or classes might sound nice, but it isn’t really correct and might not be all that useful.  It’s also not Test Driven Development.  TDD is using unit tests to drive the design of the code.  One of the “crossing the Rubicon” moments in learning TDD is when you learn to define the interface of a class inside the unit test.  That’s right, use the unit test to define the intended function of a new piece of code, and then write the code to make the unit test compile and pass (ReSharper will very happily create the method stub for you).  I still do some paper and pencil work before coding, but by and large I’ve found that I have better results when you are determining a class’s signature while writing the unit tests.  Writing unit tests first will inevitably simplify the usage of a class (because you’ll want to minimize the mechanics of creating the unit tests).  I certainly don't think that you throw away traditional design techniques when you do TDD, but you can't ever let the code and design get very far ahead of the unit tests.

     

    Another thing to keep in mind with the autogeneration of unit tests is that unit tests do not map one to one with methods and properties.  You don’t test methods one by one.  You test logical pathways through the code.

     

    One of the key things to success in TDD is to very deliberately design for testability.  The best way to accomplish testability is to write code “Test First”.  If you design and create a lot of code before you switch to retroactively applying unit tests you’ll often find that the unit tests are hard to write.  Also, you will probably not achieve the same level of code coverage that you would if you code test first.  Actually, let me put this more strongly – retrofitting existing code with unit tests is hard.  It’s perfectly acceptable to spend some time contemplating the design before coding, but do write the unit tests first.  TDD will go so much better when you write the tests first.

     

    My single biggest irritation with Microsoft’s guidance to .Net developers and their development tools is Microsoft often seems to ignore anything outside of the Redmond campus.  The advice they’ve given is contrary to existing best practices for TDD that have resulted from experience.  Patterns and Practices team, are you out there?  I know you guys are using Agile methods because I read your blogs.  Do y’all really buy off on the MS TDD “recommendations?”  Can you do something about this? 

     

    My single biggest irritation with the .Net development community is our over reliance on Microsoft for development guidance. 

    A.)  We need to diversify our sources of knowledge

    B.)   We need to question any and all advice from Microsoft (or anyone else) and think for ourselves

    C.)   We need to have an active conversation with ourselves on best practices

     

    By the way, you can do TDD and Continuous Integration with free open source tools that might actually work better than the VSTS alternatives.  For doing rapid TDD, the consensus view seems to be that the combination of NUnit or MbUnit and TestDriven.Net is superior in productivity to the new VS 2005 unit testing capabilities.

     

    There’s a thread active on the Test Driven Development message board about the MS TDD guidance that’s entertaining.  Also read Peter Hancock's response

  • It's just not enough to be right...

    Have you ever been in a situation where you knew your team/company/organization was going down the wrong road, but you couldn't stop it?  One of my best courses in college was engineering communication.  One of the things we studied in the class was a case study about the Three Mile Incident in the late 70's.  The technical flaw in the nuclear plant cooling system that led to the accident had been identified by an engineer well in advance of the accident.  We studied the chain of memorandum's that the engineer sent out about the problem, and the reasons why his communication approach was ineffective in bringing attention to the potential danger.  The engineer was right, but he never got heard and might have failed to be heard because he didn't go through the correct communication channels.

    I've had a few of those situations myself where you saw failure and problems coming but couldn't or didn't manage to prevent them.  A couple years back I was the responsible architect in a "Build vs. Buy" analysis.  The business wanted to replace a goofy web application with something more robust that could handle different business workflows.  As I recall the application was primarily written as a VB6 component that created elaborate html with a whole bunch of  html = html & "<td color=red>" & rs("title") & "</td>" action.  It was created in the euphoric aftermath of the original IE 4.0 DHTML model so a bunch of user actions spawned a request back to the server to get a chunk of html to stick in an "innerHtml" property.  Needless to say, it was slow and kloogey.

    The envisioned replacement would be a very small, but usable user interface with maybe 4-5 screens total, but like the proverbial iceberg was going to require about a dozen integrations behind the scenes.  At the end of the analysis we came down to three possibilities:

    1. Just write it ourselves.  The application itself was trivial and it's always easiest to integrate your own code.
    2. 3rd party system that used ActiveX controls on the client (non-starter).  We had another system from this vendor that was apparently a maintenance nightmare and I didn't want anything to do with it.
    3. Vaporware system from a dodgy startup company.  We, inevitably, would be their very first customer.

    I made a big enough stink about the ActiveX control deployment to prevent #2, but the business didn't trust IT (and I don't blame them), so they picked #3.  Over and over again we had warned the business that #3 didn't actually exist and the company didn't look viable in the long term.  I left the company shortly afterwards.  On my way out I told the business partner that I thought the startup would be 6 months late delivering the system.  The real number was 9 months late on what was supposed to be a six month project in total.  A year later my old company decided to change their fiscal calendar schedule.  When they asked the software vendor how to do the reconfiguration, they were told that it would require a code change.  They ended up taking the system down for a full month while the vendor changed the hard-coded fiscal calendar.

    I bumped into a former colleague today who told me that the system was the only reason he had to carry a support pager.  Sigh.

     

    P.S -- I swear I'll never go back to a big internal IT shop.  No way, no how.

  • 10 worst software bugs ever

    http://www.wired.com/news/technology/bugs/0,2924,69355,00.html?tw=rss.TEK

     

    I can't say that I've ever coded something this bad.  The worst I've ever done just made a factory floor a little bit slower.

  • Agile Austin Lunch on Thursday

    By all means, if you're in the Austin area come on in to the Agile lunch on Thursday.  I had to share Bellware's announcement:

    Food and agile development talk at the Central Market Cafe at Lamar
    and 38th (
    http://tinyurl.com/brxsz). Upstairs in the gallery.

    This week's exercise - pair eating.  Each set of two people will eat
    from the same plate in an effort to explore the social dynamics of
    using XP.


    Thurs, Oct 13th, 11:30 AM

    P.S. The pair eating thing was a joke.  See you there :)

    - I have no doubt that some bunch of XP zealots would take that seriously

  • Very important advice for using CruiseControl.Net

    One of the very valuable features of CruiseControl.Net is the CCTray client that sits in your system tray and gives both visual and audio confirmations of build status.  Remember to rotate those sound files every so often because they'll get awfully old.  My colleague has had Darth Vader intoning "Impressive" after each successful build for better than 9 months now and it's not that cute anymore.  Do a service to yourself and rotate your CCTray sounds.  Just for fun mine at the moment is:

    • Success - "Yeah, baby!" from Austin Powers.  Gonna get old real quick though
    • Failure - "Freakin' Idiot" from Napolean Dynamite
    • Fixed - "It's a 106 miles... and we're wearing sunglasses"
    • Still Broken - "Miss it Noonan!  Mmmmmiss!"

    I've already been through Simpsons sounds, Star Wars quotes, "No Soup for You!," Army of Darkness quotes, Ferris Bueller, and even Big Trouble in Little China this year.

  • Using the Chain of Responsibility Pattern

    It’ll never be the most commonly used pattern in your design toolbox, but I’ve occasionally had good results using a “Chain of Responsibility” (CoR) pattern to organize wildly variable logic.  Most importantly, my team recently used the pattern a couple of times in some code that’s likely to be supported by another group.  It’s likely that I’ll need to explain the “Chain of Responsibility” pattern to another developer, so here’s my practice run.

     

    The original Gang of Four definition of CoR is:

     

    Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. There is a potentially variable number of "handler" objects and a stream of requests that must be handled. Need to efficiently process the requests without hard-wiring handler relationships and precedence, or request-to-handler mappings.

     

    One of the most common pattern usages is to use a series of Strategy classes to handle different types of requests, with some sort of Factory pattern to obtain the proper handler.  In other cases the logic to select the proper Strategy class becomes much more complex than simply running one property of the request through a case statement.  That’s when you might reach for the Chain of Responsibility pattern.  In the CoR pattern each handler contains the logic to test for its applicability to a request.  If it can’t handle the request it’ll send the request to the next handler in the chain.  The original client is completely unaware of the handler chaining.

     

    Here’s a real world scenario taken from a shipping system I prototyped several years ago (but didn’t get to build).  Let’s say your company takes orders electronically from either the web or some sort of web service.  The steps to complete and handle an order vary widely based on the customer, size of order, destination, and a host of other things.  The business logic changes rapidly as special cases crop up and disappear.  Let’s say that we have these contrived, fictional cases:

     

    1. Orders to the European Community require a different set of value added tax calculations and we do international shipping through a different set of carriers
    2. Large bulk orders are handled separately within the warehouse and might go to different shipping carriers
    3. Most Favored Customer.  Your largest customer has special requirements for shipping and tracking, and they’re gonna get what they want.  “Thank you sir, may I have another?”  Minor geek points for nailing the movie and actor.
    4. All other domestic and international orders are handled in a generic manner

     

    First off, we’ve got an Order class and an abstract OrderHandler class that for the “Chain of Responsibility” handlers.  They look like the following:

     

          public class Order

          {

                private string _countryCode;

                private double _orderAmount;

                private double _customer;

     

                public string CountryCode

                {

                      get { return _countryCode; }

                      set { _countryCode = value; }

                }

     

                public double OrderAmount

                {

                      get { return _orderAmount; }

                      set { _orderAmount = value; }

                }

     

                public double Customer

                {

                      get { return _customer; }

                      set { _customer = value; }

                }

          }

     

          public abstract class OrderHandler

          {

                private OrderHandler _nextHandler;

     

                // Entry point

                public void ProcessOrder(Order order)