End Loose Ends

Debugging JavaScript Unit Tests Apr 02

I am experimenting with jsUnit, a javascript unit testing framework.  I am really enjoying the ability to easily unit test my javascript code.  I particularly like the UI:

 

image

jsUnit uses a technique they refer to as cache-busting to ensure the System Under Test is reloaded between test runs.  Unfortunately, this makes it difficult for developers that want to debug a unit test (GASP! Yes, once in a while, the debugger is useful when unit testing).

The key is a simple javascript trick for setting a breakpoint in the actual code.  Wherever you want the code to break, just add a debugger statement and Voila! You will be able to debug why that pesky unit test is failing.  Keep in mind, you will need to have a javascript debugger running.  In this case, I opened FireBug, enabled the script debugger for the site and let the debugger statement do the rest.

image

TouchCursor is now open source! Mar 08

I am a self professed keyboard ninja.  One of the software tools I love most is TouchCursor.  By holding down the space key, it allows me to use keys in and around the home row to act as the arrow keys, home/end keys, pgup/pgdown keys, backspace/delete keys and almost any other key.  I cannot explain to you the pleasure I get from using TouchCursor!  I hardly ever feel the need to move my hands from the home row.  Using TouchCursor is a basic instinct.

TouchCursor is especially useful on laptops with non-standard keyboard configurations.  I simply NEVER use those awkwardly placed keys.  I have been using TouchCursor for years - even when it was a commercial product.  I am glad Martin Stone decided to open source this product because the more people that install it, the happier I will be to use their computers!  That said, TouchCursor is portable software that can run off a USB stick - never leave home without it.

Become a keyboard ninja. Use TouchCursor!

Disable Beep Sound for Git Diff Feb 16

This is really just an addendum to my post on disabling annoying beep sounds when using git for windows.  You will likely want to disable the aggravating beeps that happen when scrolling to the end of a diff. This can be done by tweaking your user level git configuration settings(~/.gitconfig).  Just modify the pager command line to include a –q for quite:

[core]
  pager = less -q

How to Push a New Local Branch to a Remote Git Repository Jan 11

You might find yourself in a situation where you have a branch that you created locally and want to push to a remote git repository. Suppose you created a local plugin branch that you want to expose to others through your GitHub repository but you are not yet confident enough to put it into the master branch. Here is the command you would execute to push all of the changes from your plugin branch to a plugin branch on the GitHub repository:
git push origin plugin
This tells git to push changes from your plugin branch to the plugin branch on the origin repository. If origin does not have a plugin branch, it is created on the fly. Now, say your friend Bob wants to access the hot new feature you just pushed to the new branch on the GitHub repository. All Bob needs to do is update his local repository with all of the changes on the GitHub repository and create a local branch where he can play with the new code.
git fetch origin
git checkout --track origin/plugin
The first command updates Bob's repository with the changes from the remote repository. The second command creates a local branch named plugin that matches the origin/plugin branch and tells git that Bob wants to be able to easily push and pull from the branch on GitHub. Bob can now play with the code, commit his own changes and git push them back up to the GitHub repository.

Configure Git to Display Colours May 12

It is no secret I have switched to using Git version control system for personal projects.  One feature that is not enabled by default but I find very helpful is the use of colour when displaying output to the terminal.  This is particularly useful when viewing file status and performing diffs.  The use of colour can easily be enabled by adding the following “color” section to your personal .gitconfig file:

[color]
    ui = auto

User wide configuration settings like this one can be saved in your home directory (~/.gitconfig). 

Disable Annoying Beep Sound for Git on Windows Apr 20

One of the first things I do immediately after installing msysgit (and/or using bash under cygwin) is to disable the annoying bell noise heard when using command line completion.  To disable the bell, append the following line to the .inputrc file in your home directory (~/.inputrc):

# Don't ring bell on completion
set bell-style none

How To Avoid Accessing Repositories from Domain Objects Apr 20

I was reading the comments on Udi Dahan’s blog post about Domain Driven Design when I ran into a frequently asked question regarding domain classes and repository access.  Usually, the questions are similar to the following:

  • While invoking behaviour on one aggregate root, how do I load another aggregate from a repository?
  • How do I inject a repository instance into a domain object? 

 

I generally prefer to tackle this problem using an approach that keeps the domain model free of storage concerns.

The Obligatory Customer/Order Example

Let me put forth a simple scenario to demonstrate what you might do when a domain class seems to require access to information that would typically be in the repository:

A Customer class has a CreateOrder method that enforces the invariant "a customer can only create orders if they have no more than two unpaid orders."

In this scenario, the CreateOrder method needs to access the presumably short list of unpaid orders belonging to the customer.  Rather than polluting the Customer class with calls to a repository, the method should simply access an UnpaidOrders field of the Customer class.  This brings up the big question: how do the unpaid orders get into the Customer instance?

Repositories Return Ready to Use Aggregates

As suspected, the unpaid orders are loaded from a repository - the big difference is which repository and how.  In this example, the Customer repository would handle the loading of UnpaidOrders collection.  Whether this collection is fetched immediately when the Customer is loaded (eager loading) or loaded when you access the UnpaidOrders member (lazy loading) is a separate concern.  In both cases, the Customer class is oblivious of this and simply accesses the UnpaidOrders collection as if it were pre-populated.

Fancy object relation mapping (ORM) tools, like NHibernate, make it easy to build domain models in this way because they provide separation of concerns.  How the UnpaidOrders collection is loaded is part of the ORM configuration.  If you are concerned with database performance issues, Udi has a great post on using intention revealing interfaces to solve the conflict between eager and lazy loading.

Closing

In short, the ideal solution is to make domain classes oblivious of repositories by letting the repository for the aggregate root handle the fetching of related entities and aggregates.

Enabling and Disabling Triggers in SQL Server 2005 Feb 17

One of the goals of data migration is to preserve the integrity of existing data while altering it to be represented in another form.  Enabling and disabling triggers can be very handy when migrating data – particularly if you encounter triggers that alter or create rows in other tables.  Fortunately, it is extremely easy to programmatically enable/disable triggers in SQL Server 2005:

ALTER TABLE [dbo].[YOUR_TABLE] DISABLE TRIGGER [YOUR_TRIGGER]
ALTER TABLE [dbo].[YOUR_TABLE] ENABLE TRIGGER [YOUR_TRIGGER]

Easily Amused Feb 11

During the subway leg of my commute into the great city of Toronto, I noticed some marketing that made me laugh out loud – literally, not in a LOL instant messenger sort of way.  Normally, this may not have been a big deal or even noticeable, but when you’re riding the rocket in rush hour traffic with three or four people rubbing up against you, its hard not to make eye contact with the awkward stares.  You may not find the following all that amusing, but when it comes to long commutes using public transit, every laugh counts.

ttc_is_hiring 

I think the statement in the big red banner make the TTC Employment Opportunities advertised above it a little less appealing.

There is one other thing keeping me amused for the duration of my commutes.  I have steadily been using Git distributed version control for a few months.  In fact, looking at my check-in log, I can see I have been using it since November 13.  Let me tell you, distributed version control is doing wonders for me – especially because I am currently commuting like crazy.

Tommy Boy Pulls His Hair Out

Thankfully, Git keeps on working while I’m on the subway, taking the bus and waiting for pizza after missing the GO train due to bus delays.  Committing new functionality and corresponding unit tests to my cloned Git repository while disconnected is the only thing keeping me sane.

Configuring Git to Ignore Visual Studio and ReSharper Files Feb 11

In light of my last post where I mentioned how pleased I am with distributed version control, I thought I should share a few tips that have made my life using Git a bit easier.  This post will be the first of several posts on configuring Git for daily use by a .NET developer running Windows.

One of the first things to do after creating a new Git repository is to exclude certain files from ever entering the repository.  Git has several built-in methods for excluding files.  I use a single .gitignore file at the root of my source code directory.  It’s simple, but it works.  Surely, you, Reader, will eventually need to add some project specific lines to the following basic template, but this has been working for me:

*resharper.user
# Visual Studio
[Dd]ebug/
[Rr]elease/
*.sln.cache
*.suo
*.csproj.user

# Resharper
*[Rr]e[Ss]harper.user
_ReSharper.*/

The *resharper.user files store user specific settings for a solution, so they should not be checked into the repository.  Similarly, the other lines prevent build output, VS user specific solution configuration and ReSharper temporary data from ever being considered for check-ins.  This is especially useful because excluded files are not listed when checking for modifications.

April 6th, 2010: added a few extra lines to ignore file.

April 16th, 2010: Resharper 5.0 uses different casing for the resharper user file.