Tuesday, 21 February 2012

Windows Workflow: Re-introducing old anti-patterns?

As part of my day job I've been experimenting with Windows Workflow in both modifying the existing TFS2010 build templates and as a way of controlling the process flow in our new suite of applications. On the most part I've been really impressed; when you sit in a process workshop watching the business users mapping the existing steps out on a white board (or even a wall) it is quickly apparent that showing them a similar flow should hold significant benefits. Gherkin goes some way towards creating a syntax/language that works for both technical and non-technical people, but it is a test language verifying the application is working as intended - you don't write the process itself in gherkin. We've also found (from experience) that gherkin has a reasonable learning curve for both technical and non-technical users; whilst most people seem to find it easy to relate to the visual element of workflow with little or no training.

But as I opened I've been impressed for the most part with what I've seen of workflow so far. Over the past 10 years or so there has been a significant push to improve the quality of code that is produced. We have unit tests and TDD, guidelines such as SOLID and DRY all designed to aid the developer in creating code that should be easier to maintain and less bug-ridden. This has all helped and it's not often that you should come across methods inside of a class that remind you of the days of classic ASP and/or VB6 with massive blocks of procedural code containing large conditional sections of code; harbouring numerous responsibilities, reasons to change and worse still, code that can't be tested in isolation(*).

Getting back to workflow re-introducing old anti-patterns, step forward the default build template for TFS2010. I'm not sure how many people have taken a look at this workflow, or worse still had to work with / modify it? That nice happy feeling of replicating what the business wants quickly vanishes and it's like taking a step back in to code from the early 90's. Everything (and I mean everything) is in one single workflow. In the designer, even at 50% scale, you have to scroll down several pages to move through one conditional block of code. Want to find where a particular exception is thrown, it's probably quicker to open up the workflow as plain XAML and text search for the exception type and/or message text. Even if you figured out how to host the workflow to test it, if you want to test the "gated check-in" functionality you'll have to run the entire workflow from start to finish just to reach the code under test. Want to isolate a hard to test entry point into the gated check-in workflow; you'll have to figure out the exact scenario to replicate it because you can't set it up or Moq the areas you're not interested in. Sound familiar, I'm sure everyone's worked on a code base that suffered the same problems but in real code we're mostly past those problems now.

It doesn't have to be this way, workflow allows sequences and flows to be declared in separate XAML files and nested inside a larger workflow. There's absolutely no reason why the gated check-in sequence could not have been it's own XAML file; with it's own in/out arguments. It quickly becomes SOLID and DRY - it only needs to change when the requirements or process for gated check-in changes and can easily be tested in isolation. I might not be able to figure out how to host / run the entire build template, but even now I'd probably be able to throw together a workflow host application that loaded, set-up and ran a "gated check-in" XAML file.

So workflow doesn't have to re-introduce old anti-patterns but all the time we have real-world examples that contain bad practises it will be harder for less experienced developers not to replicate. It's probably worth remembering that there are many developers that have come into the workplace never having to suffer the pain of procedural code that generated many of the recognised anti-patterns. It would be a big step back for development (and workflow) if examples like TFS became common place! As a side project I'm trying to gain a full understanding of the default build template (something that also seems to be missing from the on-line Microsoft TFS documentation) and break the XAML into smaller, focused sequences / flows that are easier to understand. Workflow does look like it can successfully be the procedural glue that handles the transition between state and complex / long running process flows; but it does need to adhere to the same testing and principles as the code it contains!

(*) This sort of code might still exist but I'm happy to say that I've been lucky to work in and with teams that don't produce it.

No comments:

Post a Comment