Thursday, February 14, 2013

Book Review: Working Effectively with Legacy Code

I just finished reading Working Effectively with Legacy Code by Michael C. Feathers (Amazon link).  I'll dispense with the standard review since this book has been around since 2005 and has many reviews and recommendations.

The short recommendation: Read this book.  Even if you aren't working with existing code that needs to be updated, the techniques can be used to recognize potential issues on new code that you're writing.  And if you are trying to untangle an existing code base, this book is invaluable.

Refactoring for Testability
The best way to describe this book is "refactoring for testability".  The idea behind this is that if you need to make modifications to code, you need to first have tests in place.  If you do not have tests for the existing code, then if your changes break the current functionality, you won't know about it.  With tests in place, you will know if you break something as soon as you make those changes.

This is a "Do no harm" approach.  The code that you are walking up to is working code (you're adding features, not necessarily fixing bugs).  The last thing you want to do is to remove the "working" part.

This is especially critical if you are working with code that you didn't write yourself (or even code you did write yourself several years ago).  You need to recognize that if you are going to be successful in working with unfamiliar code, you need to make small changes one step at a time.

Three Parts
The book is broken down into 3 parts.

Part I: The Mechanics of Change
Part one focuses on why we need to make changes to code, how unit testing can help us make confident changes, and several tools and techniques (such as finding/creating "seams", refactoring tools, and mocking). This is a high-level overview of why we need to start working this way (refactoring for testability).

Part II: Changing Software
Part two focuses on specific problems that you may encounter in the code -- things that make the code hard to get into a test harness.  Personally, I just like reading the chapter titles.  Here are a few:
  • I Don't Have Much Time and I Have to Change It
  • I Need to Make a Change, but I Don't Know What Tests to Write
  • Dependencies on Libraries Are Killing Me
  • I Don't Understand the Code Well Enough to Change It
  • My Application Has No Structure
  • My Test Code Is in the Way
  • My Project Is Not Object Oriented. How Do I Make Safe Changes?
  • This Class Is Too Big and I Don't Want It to Get Any Bigger
  • I Need to Change a Monster Method and I Can't Write Tests for It
  • How Do I Know That I'm Not Breaking Anything?
Part III: Dependency-Breaking Techniques
Part three consists of a catalog of 24 techniques that you can use to bring code under test.  These are all presented in a short (usually 3-5 page) format with an example and specific steps to follow.  These techniques are referenced in Part II, so you'll probably be familiar with the concepts.

Some examples:
  • Break Out Method Object
  • Encapsulate Global References
  • Expose Static Method
  • Extract Implementer
  • Extract Interface
  • Parameterize Constructor
  • Pull Up Feature
  • Push Down Dependency
  • Subclass and Override Method
This seems kind of "design-patterny" in the sense of having a catalog of well-named techniques.  These techniques aren't perfect.  Michael Feathers notes in several places that some of these techniques actually make your code "worse" structurally.  But they are still valuable because they increase the testability.  The theory is that the more you work with the code, these bad structural patterns will be refactored into more organized code.  But the point is that these changes are all made incrementally -- one, small change at a time.

An Example
One chapter that especially jumped out at me (not sure why -- probably because I've seen this before) is "I'm Changing the Same Code All Over the Place."  This has to do with reducing the duplication of code between 2 classes.

What I really liked about this chapter is that it shows the reduction in duplication is small steps.  There is no, "just extract everything to a superclass and you'll be fine."  Instead, it starts by figuring out what the commonalities are between the classes -- starting with properties and methods.

The first step was to create a superclass with *one* common method.  Then the implementation methods (in each class) are updated to make them more similar (by extracting out a method to make the body of the common method identical).  Then this implementation can be moved up to the superclass with only the differences (the extracted method) in each subclass.

The next step was to move the common properties up to the superclass.  Then the refactoring continues one step at a time.  When we are left with some unique properties in each class, the question is can we combine these into a common collection.

I don't expect that this description will make much sense out of context.  The point is that instead of thinking about the refactoring in big chunks, we are thinking about it in very small steps.  At each step, we can make sure that the tests still work as expected.  And if a test fails, we can immediately know what happened.  If we were to do a "big blast" change and the tests fail, we won't know specifically what the problem is.

As someone who has rolled back big chunks of changes (because it didn't work and I wasn't quite sure why), I really appreciate this approach.

Wrap Up
To wrap things up, whether you need to modify an existing code base or you are just looking at techniques to make your code more testable, Working Effectively with Legacy Code is an excellent read.  Note: none of the examples are in C#, but they are in C-family languages (C, C++, and Java), but C# is mentioned in many of the techniques.  (There's also one Ruby example for a technique that deals with dynamic languages.)  If you are comfortable reading C#, you should be able to understand the examples just fine.

I'm always looking to improve my coding techniques, and I'm sure that I will be thumbing through this book many times in the coming years.

Happy Coding!

Wednesday, February 13, 2013

Development and the Business - A Partnership

A couple of months ago, I posted some thoughts on The Clean Coder.  Part of this included how developers should be in a partnership with the business area.

This was reinforced in a recent episode of .NET Rocks! (Is Agile Dead at CodeMash).  A panel member (sorry, I don't remember which one) polled the audience.  First, he asked how many people were part of the "business".  Result: one or two people.  Then, he asked how many people were part of "I.T."  The result was most of the audience.  He immediately said that this was wrong -- that everyone is part of the business.  That's why we (as developers) exist.

As another example, we can look at the Principles behind the Agile Manifesto. Specifically, "Business people and developers must work together daily throughout the project."

Forming a Partnership
So, how do we go about forming this partnership?  Previously, I mentioned that I really didn't know.  But it turns out, I did.  I just needed to look at my experiences in a bit of a different way.  I mentioned how I have worked in an environment where the development teams and business areas had very good relationships.  But even if we don't have this particular environment, we can work towards it.

Note: I'm approaching this from the standpoint of a corporate developer -- a developer who is building applications to help the company get work done.  You will need to modify these ideas just a bit if you work for a software company or build products for people who don't work for your company.

Watch: Learn How the Business Works Today
The first step is to see how your users do their day-to-day work.  Start by spending a day with one of your users.  Watch everything that he or she does.  Your job at this stage is to simply see how the folks in the business area get their job done.

Keep your mouth shut.  Think of yourself as a wildlife photographer -- you record what takes place in front of you, but you're not allowed to intervene.  If you see someone fumbling with a process or a piece of software, make a note of it, but let him fumble.  You're not there to give advice; you are there to see how things are done.

Take notes, but don't immediately start thinking about how you would change things.  As developers and problem solvers, we have a tendency to let our brains run wild.  But this distraction can keep us from making some important observations.  So, just keep watching.

Listen: Learn How the Business Wants to Work
The next step is to talk to your users.  Ask them what they think would make their jobs easier.  Find out how they want to work.  This is still part of the observation stage.  Don't immediately start trying to solve problems; just keep collecting information.  The key is to listen to how they would change things.  Periodically review your notes with the people you are talking to.  This will ensure that you are collecting the correct information and has the added bonus of giving the users confidence that you are really there to listen and help.

If you're dealing with front-line workers, also talk with their management team.  Ask the managers what they think works well and what doesn't work well.  You may find out that the management team would like the front-line workers to focus on different priorities -- maybe there are short-term goals that change from time to time.  If you can, try to sit in on one of their team meetings.  This will give you better feel for what is important to them.

As before, don't let your brain start problem solving.  You're still collecting information.

Analyze: Review What You Learned
After you've collected your information, it's time to start analyzing.  Now is time to let your brain run wild.  Think about the software that is being used (or not used).  Review the stumbling blocks the users may have had (for example, did they have trouble finding a particular function?).  Think about the processes that were not automated that could be.  Maybe you noticed that someone took data from an email attachment and re-keyed it into a different system.

At this stage, don't be afraid to follow up with anyone that you've talked to out in the business area.  Often, after you start analysis, you find out that you don't have all of the information that you need.  You can use some quick follow-up questions to fill in the gaps.

Contribute: Take Your Plan to the Business
Now that you've had a chance to come up with some suggestions, it's time to take them to the business.  Start by grouping your solutions into "quick wins", short-term, and long-term time frames.  Once you have this, go back to talk to the folks in the business area.

Hopefully you have a couple of "quick wins" to start with.  These are items that would be easy to implement from a technological standpoint (with minimal cost and resources) and would add good value to the business area.  This is where you start to build trust.  The quick wins show that you understand the business and that you are able to help.

If you "missed" on the quick wins, don't get discouraged.  If the business area did not like your ideas, it's your chance to gather some more information.  Ask some clarification questions and listen.  Figure out what you misunderstood and work toward correcting that.

If the "quick wins" are a hit, then give brief descriptions of your short-term and long-term solutions.  This is an opportunity to plant some ideas for future projects.

Continue: Constant Interaction
Congratulations, if you've gotten this far, then you are well on your way toward a good partnership.  Now you need to make sure that you continue this relationship.  If you start implementing some of the solutions, stay in constant contact with the business area throughout the process.  Give them prototypes and get feedback as you go.  Back to the principle we mentioned earlier: "Business people and developers must work together daily throughout the process".

Benefits of a Partnership
Ultimately, the business benefits from this relationship.  As a developer, you are more in tune with how the folks in the business area think.  Your software will naturally head in the direction that the business needs.  In addition, the folks in the business area will trust your judgment.  Once they see that you are all working toward the same goal (the improvement of the business), then they are more likely to accept your suggestions.

A partnership is about building on the strengths of both members.  The business area has operational experience -- they know what work needs to be done.  The development area has technical experience -- they know how to automate processes and build working software solutions.  Working together, the entire business benefits.

Getting Buy-In
One of the issues you may face is getting buy-in from your management team.  It may sound like you will be spending a lot of time "not developing" as you are doing "partnership" work.  But this really isn't the case.  Plan on spending one day a month in the field -- this really isn't all that much.  The rest of the time is touching base from a few minutes to an hour at a time.  You might spend a bit more time getting things started, but it will just become a natural part of your work after that.

"Agile" is a very popular term to use.  Many development teams claim to be Agile, but often they are only using a couple of buzzwords and work in "sprints".  But ultimately, Agile is about getting working (and useful) software into the hands of your users as efficiently as possible.  If you don't understand how the business works, then this becomes very difficult.  (Note: this is part of the discussion of "Is Agile Dead" from the .NET Rocks! episode mentioned above.)

Wrap Up
I have been fortunate enough to work in a very productive environment where I was able to form strong partnerships with my users.  In that position, my management team understood the importance of these relationships.  I was able to respond quickly to my users' requests (since I understood what direction they were headed), and I was also able to make relevant suggestions to improve the process.

Just remember:
  • Watch
  • Listen
  • Analyze
  • Contribute
  • Continue
It's important to remember that we are all working toward the same goal: the success of the business.  The better that we understand it, the better we will be able to contribute.  And, ultimately, we will all succeed together.

Happy Coding!

Saturday, February 2, 2013

February Speaking Engagements

I'll be speaking at a couple of user groups in February.  If you're in the area, be sure to stop by.

Tuesday, February 5, 2013
San Diego .NET Developers Group
http://www.meetup.com/San-Diego-NET-Developers-Group/events/102264182/
Del Mar, CA

Topic: Dependency Injection: A Practical Introduction
The focus is getting a good handle on why we might want to use dependency injection in our code, what it can do for us, and the tools that we can use to make it easier to use.

Thursday, February 28, 2013
dotNet Group.org
http://www.dotnetgroup.org/
Las Vegas, NV

Topic: Get Func<>-y: Delegates in .NET
In Las Vegas, we will get a bit "Func<>-y" (and a little "Action<>-y" -- yeah, I know, it doesn't quite work the same, does it).  We'll be taking a look at delegates and how we can use them to add extensibility points to our code while adhering to the S.O.L.I.D. principles (specifically the "S" and "O" parts).  We'll also see how the built-in delegates Func<T> and Action<T> add flexibility and make things more readable.

Hope to see you there (or another event coming up this year).

Happy Coding!

Thursday, January 31, 2013

Book Review: Async in C# 5.0

I recently finished reading Async in C# 5.0 by Alex Davies (Amazon Link).  This is quite a short book (at 92 pages), but it contains quite a bit of useful information in that space.

The book covers that "async" and "await" keywords that were added in C# 5.0.  Because the new functionality is based around the Task Asynchronous Pattern (TAP), the book spends quite a bit of time talking about Tasks (which were added in C# 4.0).

With that said, if you are brand new to Tasks, then this book is probably not the best place to start.  It quickly covers the different asynchronous patterns that were available before async/await, but this is more of a reminder than an introduction.  There's not quite enough for someone who is not already familiar with these patterns.

The book has 15 chapters, which means that each chapter is fairly short (just 5 or 6 pages for many of them).  But there is enough information in each to be useful.  Some interesting examples include the following:
  • Writing Asynchronous Code Manually
  • Writing Async Methods
  • What await Actually Does
  • The Task-Based Asynchronous Pattern
  • Utilities for Async Code (including cancellation and progress reporting)
  • Exceptions in Async Code
  • Async in ASP.NET Applications
  • Async in WinRT Applications
An example of one of the shorter chapters is "Unit Testing Async Code".  This is just 2 pages long.  But in those two pages, it covers the problems with testing async code with a traditional unit test -- mainly, that since async methods return immediately, the test will actually complete before the asynchronous part is finished (and will always return "successful").  There are basically 2 approaches: (1) force the test code to run synchronously, or (2) let the unit test return a Task type (rather than void) -- MSTest (as well as some other testing frameworks) have been updated to support async tests that return Task.

In addition to showing how to use async and await in your own code, the book also covers some of the inner workings.  This includes a look at the methods that are generated by the compiler (with some simplified IL samples) and also how the stack is maintained and reported during debugging.

There are quite a few good tidbits -- such as how to create async wrapper methods to add functionality -- that make this book a good read.

It's short, it's cheap, and it contains quite a bit of useful information.  Well worth the read (especially useful is you've got a bit of Task-based programming under your belt).

Happy Coding!

Sunday, January 27, 2013

BackgroundWorker Component Compared to .NET Tasks

There are a lot of questions as to whether the BackgroundWorker component should be used for new applications.  If you've read my previous articles (such as this one), you know my opinion on the subject.  To summarize, the BackgroundWorker is an easy-to-use component that works very well for putting a single method into the background while keeping the UI responsive.

In .NET 4.0, we got Tasks.  Tasks are much more powerful than the BackgroundWorker (orders of magnitude more powerful) and have much more flexibility as well.  In .NET 4.5, we got a way to report progress in Tasks (through the IProgress interface).

A Comparison
Personally, I've been working with Tasks a bit more in my code.  And based on this, I thought I would revisit  my BackgroundWorker sample and rewrite it (with the same functionality) using Task instead.  The result is almost the same amount of code (just a few lines different).  I purposefully wrote comparable code so that we could compare the two approaches.

The Initial Code
Our baseline application will be the BackgroundWorker w/ MVVM project that was reviewed a while back.  This application was chosen as a baseline because all of the BackgroundWorker functionality is confined to the ViewModel (in ProcessViewModel.cs).  This makes it easier to swap out the functionality using Tasks.  I'm only going to cover the differences in the ViewModel here; if you want a better idea of the entire project, see the article mentioned above.  The source code for both projects can be downloaded in a single solution here: http://www.jeremybytes.com/Demos.aspx#KYUIR.

I did make a few updates to the UI.  This is primarily because my development machine is using Windows 8, and the previous color scheme didn't look quite right.  Here is the application in action:


As a reminder, we want to support cancellation and progress (progress includes both a percentage for the progress bar and a message for the output textbox.

So, let's start comparing the code!

Fields and Properties
The Fields of the ViewModel are almost the same.  First, the BackgroundWorker fields:

Now, the Task fields:


As we can see, the difference is that the BackgroundWorker field (_worker) has been removed and a CancellationTokenSource (_cancelTokenSource) has been added.  We'll talk a bit about the CancellationTokenSource in just a bit.  We need this in order to support cancellation of our Task.

The Properties are the same in both projects.  Our properties are used for data binding in the UI (the goo that makes the MVVM pattern work).  Since our UI hasn't changed, the public interface of the ViewModel does not need to change either.

The Constructor
Next, we'll take a look at the constructors.  In our BackgroundWorker project, the constructor sets up our component:


But in the Task project, we don't need this initialization.  We've actually moved some of this functionality down a bit.  But we're left with an empty constructor:


Starting the Process
Now that we've got our basic pieces in place, let's take a look at starting our long-running process.  The StartProcess method is fired when clicking the Start button in the UI.  This kicks things off.

Here is the StartProcess method from the BackgroundWorker project:


And here is the StartProcess from the Task project:


There are several similarities between these.  First, we see that the Output property is cleared out.  Then we see that the StartEnabled and CancelEnabled properties are set -- these properties are databound the the IsEnabled properties of the buttons.  We also instantiate the ProcessModel object (_model) if it has not already been created.  As a reminder, the ProcessModel object contains our long-running process and is the Model part of our Model-View-ViewModel.

Now the differences.  First, notice that we need to instantiate a CancellationTokenSource.  This is the private field that we noted above, and we'll be using this to cancel the process.  We'll talk about cancellation a bit more below.

Next, rather than starting the BackgroundWorker (with RunWorkerAsync), we call a method called DoWorkAsync.  This method returns a task.  We can see that it takes our _model as a parameter (just like the BackgroundWorker), but it also takes a CancellationToken as well as a Progress object.  The Progress object simply implements the IProgress<T> interface, and we'll talk a bit more about this later.

The last piece of our StartProcess method is to add a ContinueWith to our task.  This is the equivalent of hooking up an event to the BackgroundWorker's RunWorkerCompleted event.  When the task has completed, the TaskComplete method will run (and we'll see this below as well).

Cancellation
To actually cancel the process, we use the CancelProcess method.  This does not immediately stop any running processes, it simply sets a flag to indicate that things should be cancelled (if possible).

For the BackgroundWorker, we just call the CancelAsync method on the component itself:


For Tasks, we call Cancel on the CancellationTokenSource:


So, let's talk a bit about cancellation.  For the BackgroundWorker, cancellation is baked into the component, and we just need to call the CancelAsync method.  For Tasks, we need to create a CancellationToken and pass it to the task itself (we'll see how it's used in just a bit).  One of the interesting things about a CancellationToken is that you can't just create one directly.  This is why we have an internal field that is a CancellationTokenSource.  The CancellationTokenSource manages a CancellationToken.  We can use the Token property to get this token (which is exactly what we do in our call to DoWorkAsync above).  We can also set that token to a cancelled state by calling the Cancel method on the CancellationTokenSource.  Notice that we are not setting any properties on the token itself; in fact, we're not allowed to directly update the state of the token.

The Long-Running Process
Now we'll take a look at our long-running process.  This happens in the DoWork event of the BackgroundWorker:


As a quick reminder, this method loops through the Model (which implements IEnumerable).  First, it handles whether the process should be cancelled.  Then it calculates the progress percentage and returns a string for progress display.  Finally, if it gets to the end of the loop without cancellation, it returns the current iteration value.

We'll see that we have very similar code in the DoWorkAsync method in our Task project:


The first thing to note is that we have a custom ProgressObject that contains 2 properties.  This will allow us to report both a percentage complete as well as a message.  Notice that our DoWorkAsync takes a parameter of type IProgress<ProgressObject>.  This lets us know what type of object to expect when the progress is reported.  Note that IProgress<T> is available in .NET 4.5; if you are using .NET 4.0, then you need to report progress manually.

So, let's walk through the DoWorkAsync method.  First, notice that it returns Task<int>.  Because it returns a task, we can use ContinueWith to determine what to do after the task completes -- and this is exactly what we did in the StartProcess method above.

For parameters, DoWorkAsync takes a ProcessModel (our model for doing work), a CancellationToken, and an IProgress<T>.

Since we need to return a Task, we need to create one.  This is done through the Task.Factory.StartNew() method.  The version of the method that we're using takes 2 parameters: a Func<int> (since integer is our ultimate return type) and a CancellationToken.  For the first parameter, we're just using a lambda expression to in-line the code.  We could have made this a separate method, but I included it here so that it would look more similar to the BackgroundWorker version.

Inside the lambda expression, we are doing the same thing as in the BackgroundWorker.DoWork event: we loop through the model.  Inside the loop, we first check for cancellation.  Since we are using a CancellationToken, we just need to call ThrowIfCancellationRequested.  This will throw an OperationCanceledException if IsCancellationRequested is true on the token.  Since we passed the token as part of the StartNew method, the exception will be automatically handled and the IsCanceled property of the Task will be set to true.

Next, we calculate the progress.  The calculation is the same as the BackgroundWorker method.  The difference is that to report the progress, we need to create a new ProgressObject (our custom object to hold the percentage and the message), and then call the Report method on our progress object.

And finally (at the end of the lambda expression), we return the value of the last iteration (an integer).

So, we can see that this part is just a bit different.  It's not really more complicated, but we do need to understand Tasks well in order to get all of these pieces to fit together.

Reporting Progress
Updating the progress bar and message for the UI is similar between the projects.  Here is the UpdateProgress method from the BackgroundWorker project:


As a reminder, the ProgressChanged event fires on the BackgroundWorker whenever the ReportProgress method is called.  The event arguments for the event include the ProgressPercentage (which is an integer) as well as the UserState (an object).  Since the UserState is of type object, we can put whatever we like into it.  In this case, we put a string that is displayed in the output box, but we can put a more complex object in there if we like.

The UpdateProgress from the Task project is similar:


Here we can see that our custom ProgressObject is used as our parameter.  This method is called whenever the Report method is called on the Progress object.  This callback was hooked up when we originally created the Progress object in our StartProcess method:


Notice that when we "new" up the Progress object, we pass the UpdateProgress method as a parameter.  This acts as the event handler whenever progress is reported.

We do get some extra flexibility and type-safety with this methodology.  First, our Progress uses a generic type.  This type is ProcessObject in our case, but if we only wanted a percentage, we could have specified Progress<int> (and not worry about a custom object type).  Because we have a generic type, we don't have to worry about casting and will get compile-time errors if we try to use the types incorrectly.  (For more advantages to using Generics, you can look up T, Earl Grey, Hot: Generics in .NET.)

Completing the Process
Our final step is to determine what happens after our long-running process has completed.  Here is the code from the BackgroundWorker project:


Here, we check for an error condition, check the cancelled state, and have our "success" code which puts the result into our Output box and resets the progress bar.  Whatever the completion state, we reset the enabled properties of our buttons.

And from the Task project:


We can see that this code is almost identical.  Our parameter is a Task<int>.  Since it is a Task, we can check the IsFaulted state (to see if there were any exceptions), check the IsCanceled property, and then use the IsCompleted property for our "success" state.

Should I Use BackgroundWorker or Task?
So, we've seen some similarities and some differences between using the BackgroundWorker component and using a Task.  When we look at the total amount of code, the files are very similar (197 lines of code in the BackgroundWorker and 194 lines of code in the Task).

In writing the Task project, I purposely tried to line up the methods with the BackgroundWorker project.  This was to facilitate a side-by-side comparison.  But using either project, we can reduce the amount of code with lambda expressions and other in-line coding techniques.  But the goal wasn't to create the most compact code; it was to compare techniques.

Advantage BackgroundWorker
The BackgroundWorker component has several advantages for this scenario (and I want to emphasize "for this scenario").  First, the BackgroundWorker is easy for a developer to pick up.  As has been mentioned in previous articles, since the BackgroundWorker has a limited number of properties, methods, and events, it is very approachable.  Also, most developers have been working with events already, and so the programming style will seem familiar.

In contrast, there is a steeper learning curve regarding Task.  We need to understand how to construct a Task (the static Factory is just one way to do this) and how to pass in a CancellationToken and IProgress object.  These are both non-obvious (meaning, I never would have guessed that I needed a CancellationTokenSource -- my instinct was to try to use the CancellationToken directly).  We also need to understand what it means when we pass Tasks as parameters and return values.  So, a bit more effort is required.

One other thing to consider is the cancellation process.  With the BackgroundWorker, we raise a flag and then set the Cancel property on the event argument.  With the Task, the cancellation process throws an exception, and exceptions are relatively expensive.  Unfortunately, throwing an exception is the standard way of making sure the Task's IsCanceled property is set to true.  Since IsCanceled is read-only, it cannot be set directly.  So, we get a bit of a performance hit with the cancellation process for Task.  It's probably not enough for us to worry about in most cases (definitely not in this scenario), but it is something to be aware of.

Advantage Task
Tasks are extremely flexible and powerful.  We only touched on a very small part of what Task can be used for; there is much more (such as parallel operations).  And once we start using Task more frequently we can better understand how to take advantage of the async and await keywords that we have in .NET 4.5.

[Update Jan 2015: If you want to take a closer look at Task, be sure to check out the article series here: Exploring Task, Await, and Asynchronous Methods]

The Verdict
The BackgroundWorker is a specialized component.  It is very good at taking a single process and moving it off of the UI thread.  It is also very easy to take advantage of cancellation and progress reporting.

For this scenario, I would lean toward using the BackgroundWorker component -- this is because I like to use the most precise tool that I can.  And this scenario is just what the BackgroundWorker was designed for.

With that said, I am using Task more and more in my code in various scenarios.  It is extremely powerful and flexible.  I am a bit disappointed to see that Task is not 100% compatible with WinRT (although there are fairly easy ways to go back and forth between Task and IAsyncOperation).  But then again, the BackgroundWorker doesn't even exist in the WinRT world.

Wrap Up
I have a bit of a soft spot for the BackgroundWorker component.  I have found it incredibly useful in many projects that I've done.  And if you are dealing with a situation where the BackgroundWorker fits in, I would still encourage its use.  (If you want to review those uses, check here: BackgroundWorkerComponent: I'm Not Dead Yet.)

But, if you're dealing with a situation where the BackgroundWorker does not fit, then don't try to force it.  Instead, look at how Task can be used to make things work.  And as we use async and await more frequently, understanding Task becomes a critical part to writing understandable and reliable code.

Happy Coding!

Monday, January 21, 2013

Extension Methods - The Movie

So, I've created my first technical screencast: an overview of extension methods.  Now available on YouTube: http://www.youtube.com/watch?v=fKxy8Hl7Ht0.

If you'd like to see more videos, let me know the topics you're interested in (just leave a comment on this post).  If you don't want to see more, let me know that as well.  The goal is to make my blog, website, and other resources as useful as possible.

Happy Coding!

Saturday, January 12, 2013

Speaking in 2013

I've been busy setting up speaking engagements for 2013.  So far, I have 7 events booked (I'll post more details as the events approach).  If you'd like me to come speak at your event, user group, or corporate meeting, either drop me an email or send a request through INETA.

I specialize in intermediate .NET topics -- the topics that developers need to move up to the next level, and I've received good responses from new developers and seasoned developers alike (you can view feedback on SpeakerRate and my website).  Popular sessions include Design Patterns, Lambda Expressions, Delegates, Interfaces, Generics, and Dependency Injection.  For more details, check out the Demos section of JeremyBytes.com: http://www.jeremybytes.com/Demos.aspx.

Some new topics planned for this year include a practical introduction to the Model-View-ViewModel (MVVM) Design Pattern, how to perform common actions with MVVM, an overview of Unit Testing, as well as Mocking Strategies for Unit Tests.

I hope to see you at an event this year.

Happy Coding!