Archive for July, 2009

Peeking Under the Hood: Unit Tests

I don’t know about you guys, but I really like to learn by example.  Much better to see someone else’s working code – provided you have an understanding of the underlying principles of course ;-)

Unit tests are a topic that’s often a hot debate.  In my opinion, it’s because a lot of people don’t really know how to “do it right”.  To that end, in case you hadn’t seen this, Microsoft generously provides all of the source code to the ASP.NET MVC project on CodePlex.  And codeplex has this cool feature where you can browse the repositories right from your browser.  So you can dig right into the MVC/test/SystemWebMvcTest folder and see how they do it first hand :-)

http://aspnet.codeplex.com/SourceControl/BrowseLatest

Comments

Cross-Platform Javascript WebWorker

If you’ve been keeping up with the new developments in the web world, no doubt you’ve heard about Web Workers.  A new technology for browsers that will let you take advantage of the client’s true multi-threading capabilities.  Currently, it’s only supported in Firefox 3.5, Safari 4, and some of the latest nightly Chrome builds.

I of course am very excited that the platform is moving ahead.  However, I am disheartened by the fact that it will take *ages* for the majority of the population to move to a browser that supports this feature.  So naturally started thinking about how I might be able to take advantage of this new feature, while still supporting older browsers.

Below you will find a relatively small script that will conditionally declare the Worker class if it’s not currently declared.  I tested it and it worked in IE8 in addition to Chrome (not the nightly with worker support), and of course FireFox 3.5, where it executes using the new feature rather than the wrapper.

please note: This code has a small dependency on jQuery to do the ajax call.  It should be pretty easy to substitute that with another framework’s analogous feature if you so prefer.

The Worker Class

if (!Worker) {
function Worker(path) {
function InternalWorker(p) {
this.onmessage = function(m) {
if (trace)
trace(‘received ‘ + m);
};

$.get(p, function(data) {
setTimeout(function() {
eval(data);
}, 1);
});
}

var iworker = new InternalWorker(path);

function postMessage(m) {
setTimeout(function() { iworker.onmessage({ data: m }) }, 1);
}

return iworker;
}
}

The Usage

Once you’ve included the above script, you can use it as such:

<script>
var worker = new Worker(‘Scripts/worker.js’);
worker.onmessage = function(event) {
$(“#result”).text(event.data);
};
</script>

<div id=”result”></div>

The Worker Script

The file mentioned above (Scripts/worker.js) is defined in my sample as follows:

postMessage(“this is from the web worker”);

I know, simple … all it does is post a message back to the main gui thread.

How it Works

The premise is very simple. If we need to fall back to manually supporting the feature on a downlevel browser, then we take the path to the script file which is passed into the constructor and download the contents of it using the $.get method.  We take that and use the setTimeout method to execute said script using the eval method.

This is of course not production-ready code. More of a proof of concept as I’ve only implemented the basic execution of the remote script, and passing messages back to the client.  If you actually try to do some heavy computation, the main thread will obviously get bogged down.  This will of course prompt IE to ask the user if they want to cancel the script.

However, I still see some value in being able to code using this API on all browsers.  The concept of message passing forces you to have clearly defined boundaries between functionality.  If your DOM modifying code can only talk to the validation functionality using messages, there’s a much higher chance that your code will be easy to maintain.

Please let me know if anyone is interested in further development of this little wrapper.  I’d like to collaborate on it and perhaps release a more fully featured and minified version on codeplex.

Thanks!

Comments

On Personal Productivity

There are many different types of productivity, today I’m going to talk about the things that you do on a day to day basis.  A few years ago, I found myself unhappy with my level of productivity and decided to figure out a way to manage my time better.  As one would do when trying to improve the performance of code, I profiled myself.  For a week, I wrote down in painful detail each task I’d do … even if it was just goofing off and browsing the web.  Then at the end of the week I tallied up my results and found that I spent an inordinate amount of time doing the following 3 things.

  • checking email
  • reading blogs/news
  • trying to remember things and manage my daily tasks

I was actually kind of surprised that I was spending so much time doing these non-essential tasks so I started thinking of ways to improve.  I’ve tried a number of techniques over the years, and this is my current setup.

RSS Reader

The first thing I wanted to improve was how much time I spent surfing the web.  This was in my mind the thing that contributed the least to my work output (at least I need to read email and manage my tasks to do my job).  Then one day, I discovered the venerable RSS Reader.

My first RSS reader was RSS Bandit.  It’s a desktop client and works pretty well … however, I had issues synchronizing the subscription list between my home PC and work PC (newer versions have solutions for this).  Then google decided to launch Google Reader and I haven’t looked back since :-)

I go through every once in a while and clean out feeds that are too chatty, and add new ones that are relevant to my daily workload.  For example, when I started working on an ASP.NET MVC Project, I added several new feeds like Phil Haack’s blog.

Nowadays, I’ve got it tuned so that I spend no more than about 5 minutes catching up and reading blogs in the morning.

Email

I use GMail as my email provider and have since the service was in Beta (funny, I know).  Since I use this email address in public forum comments, posts, and to register for websites – I get a considerable amount of extraneous email.  It got to the point where I was checking my account every 5-10 minutes because I got so much mail that I didn’t want to miss the important ones in the noise.

These days, I use gmail’s label and filter system quite extensively.  I identified a few groups of emails that came with great frequency and had some marginal value … enough that I didn’t want to unsubscribe or remove them entirely.  These are things like newsletters from companies that I’ve legitimately done business with, social networking site notifications, and receipts.  So what I’ve done is to create 3 labels:

  • Receipts
  • Social Networking
  • Newsletters

And then created filters that immediately apply the appropriate label to the incoming message and archives it out of my inbox.  That way, I can choose when I want to look over my social network activity instead of having it blast me in the face everytime one of the emails comes in.

The result is that I’m able to focus on core conversations that matter to me.  And when a conversation is done, I can archive it and still feel confident that I can find it using gmail’s search if I ever need to refer back to it. Coupled with GMail’s excellent spam filtering, and I couldn’t really ask for a better email solution.

TODO List

Completing the trilogy of productivity is my beloved TODO list.  For years I simply used notepad (whether virtual or physical).  I’d start the day by collecting all of the tasks that I need to accomplish for the day.  As much  detail as I could think of for each task would be logged, and then I just start checking them off one at a time.

That actually works fairly well for me, the only problem is that there is more going on in my life than just my work tasks.  I’d find that multiple things would bump into one another (“take kids to the park when I get home”, “call the insurance company tomorrow at work”, etc.) and purge my brain’s L2 cache, much to my detriment. I was forgetting important things all the time.

Now, I’d heard about the Getting Things Done system before. As with all self-help advice, I discounted it almost immediately and would never read about it.  However, when Remember The Milk launched I started seriously evaluating how I process the incoming stream of things I need to do.

I came across a few articles that talked about how to apply the GTD system in RTM (which I had recently started using at the time).

I must say that it works extremely well for me. I’m able to put things on the list and put my mind at ease that I won’t forget it.  But more importantly, by using the special saved searches to create “Contexts”, stuff that I don’t have to worry about right now is conveniently out of site so it doesn’t clutter up my brain.

Conclusion

I’m always looking for ways to improve my workflow and to use technology to help me manage the information overload that this modern age throws at me.  Although this setup is currently working pretty well, I’ve noticed that I constantly have to be adjusting little things about the process (email filters, RTM context queries, RSS subscriptions).

I feel like there’s an impending breakthrough just waiting in the wings that will greatly improve everyone’s workflow … I’ll keep my eye out for it :-)

Comments (2)