Saturday, September 22, 2012

Building a Team

If you want to build a ship, don't drum up the men to gather wood, divide the work and give orders. Instead, teach them to yearn for the vast and endless sea.
- Antoine de Saint-Exuperé

You often hear how teams perform much better than a group of individuals.  But how do you bring out the best in people and the team they're a part of, when not all of us have the magnetic charisma of Martin Luther?

Let's do a thought experiment. Assume you've landed a new job and are paid a salary (just to take monetary incentives out of the equation - see Dan Pink's talk on motivation).

You turn up to your new job and are asked to move bags of dirt to a place up the road. You don't like the job or the people you work with, but you'll do it until something better comes up. You're not particularly helpful towards your crew and don't work that hard.

Now your boss comes up to you and says, "the reason we're moving bags up the road is because a foreign country will be invading us and we need a wall there for cover".  Suddenly, you're helping your mates out and working like a Trojan.  Why?  Because your job has meaning and purpose.  You now care.

While moving these bags, you start to wonder if dirt's the best defence. So you ask your boss to lend you a gun, a couple of workers and a key to storage.  With the crew's help and some company resources, you experiment a little and find that sand stops bullets better. You report this to you boss and soon you're moving the heavier bags up the road with no less determination.

So just by being aware of the mission and given the support you need, you can contribute in ways that make a real difference.

What would kill this? If you had daily meetings where you reported how many bags you moved the previous day and how many you planned to move today. This would not only be a waste of time, it would be demeaning to the workers and threaten the goal.

So by giving people more control and a purpose that's meaningful, they'll come together and rise to the challenge.  Why?  Because, as Nick Fury says in the Avengers, "we'll need them to".

Thursday, September 20, 2012

Implementing Lazy Evaluation - Part 1

In this blog series, you will learn how to implement lazy evaluation with the yield keyword and be introduced to Microsoft's Reactive Extensions (Rx) for complex event processing (CEP) and other goodness.

First off, what are the main differences between IEnumerable, ICollection & IList?

  • IEnumerable is a potentially infinite sequence, 1, 2, 3, ...
  • ICollection is a finite sequence, 1, 2, 3, ..., n
  • IList preserves order so you can index into it, list[5]

IList extends ICollection which extends IEnumerable. We're interested in IEnumerable.

A well known but not well understood gem of C# is the yield keyword.  Let's look at an example.


At first glance, the AllWholeNumbers() method might look like it will hang or throw an OutOfMemoryException, and in fact does if you put a .ToList() after the call.

But what yield does, and this is where the goodness is, is it wont execute the AllWholeNumbers() method until you access the next element of list. It's best to see this with a debugger - it wont step into the method until the first iteration of the foreach loop.

Once yield is hit the first time, AllWholeNumbers() runs until 0 is returned (given to the variable result) and the method is put into hibernation. In other words, the method's internal state is saved for later.  The second iteration of the foreach loop will restore this state and start from where it left off - incrementing idx. It will then return 1, hibernate, return 2, hibernate, and so on.

So you can do things like,

And it will only execute what it needs to to get the results.

It's interesting to note that while AllWholeNumbers() appears to be an infinite loop, it's a mathematically valid representation of the whole numbers (all infinitely many of them) and, thanks to the yield keyword, it can be used without waiting forever.

Next post: Implementing Lazy Evaluation - Part 2