Stuart & Meaghan

Life, love, and adventure in the Pacific Northwest

Mine is the pattern…

(to the tune of “Morning has Broken”)

Software is broken, from the first version
Fowler has spoken, his gospel word.
Praise for the patterns, praise for MVP
Praise for our passive view of our world.

Hear there’s a new thought, coming from Redmond
Wait it’s an old thought we already had.
Praise for MVC, in its new format
Sprung in completeness where their feet pass.

Mine is a castle, and it’s named Windsor
Born not from one light, silver or gray.
Praise it with caution, praise it with warning
MS’ recreation of the new way.

(C) MVVMVPVC

June 8th, 2009 Posted by stuartthompson | Blogging, Technology | no comments

Twitted my last tweet?

I had a realization this afternoon that I may have twitted my last tweet on twitter (say that six times fast!)  The social networking phenomenon that I’ve contributed to over the last year has become less and less interesting to me as time has passed.  I can see where the service delivers incredible value for some people, I’m just not certain I’m that audience.  In fact, I found myself having to actively become that audience in order for the service to make sense in my life.  That’s altogether the wrong way round.

I still like the idea of updating my status for friends to see, but FaceBook fills that niche much more succesfully than twitter for me, not only in terms of the people in my life who use facebook versus twitter, but also because the people I know who use facebook post updates that are of more interest to me.  I’ve found that my tweeps (friends on twitter) tend to post more from a marketing and “personal online brand” perspective.  On twitter my friends network, promote brands and initiatives, and support each other’s promotions in an effort to further the common good, and for that crowd those are wonderful things.  My friends on facebook tend to update more of what is happening in their personal lives and how they are feeling on a day to day basis.  For me, those are simply the posts that I find more interesting and that add more value to my life in terms of what I’m looking to achieve from social networking.

March 31st, 2009 Posted by stuartthompson | Social, Technology | no comments

GMail Themes

GMail now has support for themes to change the look and feel of their interface, something that has been speculated about for quite a while.  I usually feel like theming and skinning are really cheesy “value add” items for service-based software, but there is something about the particular implementation that Google have chosen that make me dig it.  You should try out the “Tree” theme at least once.  It asks for the name of the city you live in and then bases the graphic of the theme upon the current weather in your area.

The Inquisitr put together a collage of some of the available themes, although the preview mode in the GMail interface is more than sufficient.  I’ll bore with theming soon enough, but for now it’s cool and fun.  I guess I’d never really thought that the GMail interface was ugly before, just functional, but now I do appreciate the touches of color and a crisper feel.

November 20th, 2008 Posted by stuartthompson | Technology, Uncategorized | no comments

First post from Google phone

I downloaded the wpToGo app for my Google phone while waiting for my lunch to finish in the microwave and thought I’d test it out.

If this works then that means it took less than 4 mins to install, configure and post (according to the timer on the microwave).

Had a few minor issues that bumped the time up to 9 mins (had to enable xml-rpc access in my blog’s configuration)…but now I’ll try including a picture.
image

November 20th, 2008 Posted by stuartthompson | Technology | no comments

Alternate URLs

Although the true address of this blog is http://blog.stuartthompson.net, there are several alternate URLs that you can use to get here:

All of the above links will redirect to this blog.  It may seem like a strange setup, but there is actually some method to the madness.  stuartthompson.net is my domain, not just my blog.  I want to move to hosting a dashboard there that contains more than just blog entries, rather a aggregation of twitter, the blog, flickr, facebook, all of my social media presence in one location.  However, as more and more of those services are added there is more information that is specific to me and less that is specific to both Meaghan and I.  Meaghan has a separate twitter feed and flickr, for example, and might someday want a dashboard of her own.  I’m integrating stuartthompson.name with this site too, as an alternate portal access location.

However, for stuartandmeaghan.* I thought that the most pertenant location was our group blog.  I could actually host the blog at stuartandmeaghan.com, and I thought about that, but then realized that as Meaghs gets more into blogging she may well want a space of her own.  She already has a space of her own on wordpress for our wedding blog.  At some point if she wants a blog of her own, I would probably set it up at meaghanbrown.com or meaghanthompson.com once we are married.  Something personal to her.  For now, I host the blog at stuartthompson.net but have the group urls redirect there.  It made me realize in our age of permanence and location on the web that the tradition of having women who marry change their last name causes them quite a problem.  I’ll always be Stuart Thompson, so accounts on facebook, twitter, flickr, etc… where I can grab stuart.thompson, stuartthompson, sthompson or some other variation will always be in one place.  Anything registered to Meaghan Brown or any derivative might either a) have to change after we marry or b) stay with her maiden name (which introduces it’s own problems).  Either way, there is going to be difficulty for some people in finding her online profiles after we are married.  I wonder how many Web 2.0 companies have planned for this in their products.  It would be nice to see a “my name changed, now what?” use case in their UI.  I haven’t really researched it as the thought only occured to me recently, so it might already exist.  Either way, I think it has to be a use-case that is accounted for as web 2.0, social media, and online presence become increasingly important.

November 19th, 2008 Posted by stuartthompson | Blogging, Social Media, Technology | no comments

Craigs list underworld…who knew?

@codinghorror (a.k.a. Jeff Attwood) twittered a link this morning about drug deals and prostitution rings using craigslist for distribution.  It’s at times like this I feel so unaware of the criminal underworld:

http://www.nydailynews.com/news/ny_crime/2008/11/17/2008-11-17_drug_dealers_doing_roaring_trade_on_crai.html

I think the part that amazes me the most is that a felon with a record gets caught having used a halfway-house computer to deal drugs through the craigslist website, pleads guilty, and then ends up with 3-6 months in jail; a sentence I’m assured will probably end up being more like 6-8 weeks.  While I’m sure jail is extremely unpleasant, that doesn’t seem like much of a deterrant for someone who would so blatantly break the law even after having previously being prosecuted for armed robbery.  What are the odds than in about 8-10 weeks time he’ll be dealing drugs again, ruining other lives, starting another cycle for people to have to clean up?

November 19th, 2008 Posted by stuartthompson | Technology | no comments

Final phase of migration

Thank you for your continued patience as I finally finish migrating my blog to its permanent home.  I now have Wordpress installed on my own host and it’s up and running.  Yup, you’re in a different place and probably didn’t even know it.  The theme and layout will change slightly as the free Wordpress hosting has different themes than the version you host yourself.  However, now I have unlimited space and don’t have to worry about hitting limits on my account about the number of pictures I can upload etc…  It also means I can do much cooler things with the site.

I should have the remainder of the 2008 posts imported within the next couple of weeks.

November 6th, 2008 Posted by stuartthompson | Technology | no comments

Moved to Skype

Meaghan and I chat with my folks over the internet about once a month. For the last year we’ve put up with the bugged and flawed experience of MSN Messenger, working around audio delay, dropped calls, and frozen video problems. This morning we switched over to Skype and what a difference!  We’ve been aware of Skype for a while.  I’ve been using it on and off since I was in college.  However, we’d stuck by the principle that if it isn’t broken then don’t fix it.  MSN Messenger finally qualified as “broken enough” to switch.

The Skype call quality is superb, the video degrades and upgrades as the connection quality comes in and out but I’d much prefer a graceful degradation to a dropped call.  Even over the wireless network, the connection quality was amazing.  I was able to give my parents a tour of our house by connecting on our laptop and just wandering around.

Good to see my folks- great to see and hear them clearly!

October 19th, 2008 Posted by stuartthompson | Family, Technology | one comment

RSS Feed

Did you know this blog has a feed?  It’s located at: http://stuartthompson.wordpress.com/feed

October 3rd, 2008 Posted by stuartthompson | Technology | no comments

SubText import: 12 down, 128 to go

Continuing with my import from a failed SubText 2.0 upgrade, I’ve completed the import of my DasBlog entries.  Today I started importing the SubText entries.  With 12 down and 128 to go, it still feels like there is a ton of work ahead.  It’s nice to see it coming together here at WordPress though and I’m really enjoying the new blog interface and overall robustness of this blogging package.  I’m looking forward to having the import completed so that I can return to blogging normally.  I still have all of my photographs and stories from England to post.  If only there were 30 useful hours in every day!

September 29th, 2008 Posted by stuartthompson | Technology | no comments

Migration Part I - Complete

The first part of my blog migration has now been completed.  The posts from May 1st 2006 to April 18th 2007 have been successfully migrated to this blog.  Thanks to a lot of manual link cleaning and image relocation work, all of the links and images should now be functional and verified.  I’ll start work on the SubText entries tonight.

September 22nd, 2008 Posted by stuartthompson | Technology | no comments

The System is Down

She's Dead Jim!

You’ve all probably noticed by now that the old blog has been down for about a week.  Yes, unfortunately I must announce that the old blog has officially.  The upgrade to SubText 2.0 did not succeed, and neither did the restore from backup.  I’ve attempted to breathe life into the patient for the better part of a week with little to no success.  While I have some idea about why the upgrade might have failed, I cannot determine why the restore faired no better.  Everything is, ostensibly, as it was prior to the start of the process.  Alas it is sometimes just the way with these things.

As a result of this loss, I have undertaken a task that has been on my plate ever since I originally retired my old dasBlog archive and moved to SubText back in early 2007.  I am now consolidating the archives from both of those blogs into a new single home here at wordpress.

Fortunately, while the SubText software insists that the contents of the database are not compatible with the files on the web server, the content of the posts is still quite intact in the database itself.  With a bit of surgery and a good amount of manual labor I am fairly confident I can restore each of the posts without loss.  I still have all of the images as I stored those on a separate file system anyway and linked to them from my post text.  The post text itself is alive inside the SubText database.  All that remains is for me to extract the original text from the database and pair it up with the relevant images.  I’m undertaking that task a bit at a time as I get the chance.  It may take a while for full restoration.  Thank you for your patience.

Now I leave you with one from the master in this sort of situation: StrongBad.  Enjoy!

http://www.homestarrunner.com/sbemail45.html

August 26th, 2008 Posted by stuartthompson | Technology | no comments

Reflector to go to RedGate

Lutz Roeder has decided to move on from Reflector.  RedGate will be taking over the project from now on.  Fortunately, they have agreed to maintain the community edition of the software.

http://blog.lutzroeder.com/2008/08/future-of-net-reflector.html

August 20th, 2008 Posted by stuartthompson | Technology | no comments

NDepend Part II - Beginning CQL

In my continuing use of NDepend on both my work and home projects, I have been investigating CQL more and more, especially from the perspective of use in continuous integration.  NDepend provides some incredibly powerful analysis features and I find the idea that constraints can be applied and warnings reported as part of day 1 builds to be extremely appealing.  I’m all about keeping code clean, constrained, and consistent.  NDepend provides another great tool in my kit to ensure that my own code is marshalled to my standards from the very first line that is written.  As David Muhondro put so eloquently in his blog post, NDepend is static analysis on steroids.  When most users encounter CQL for the first time, the response is “wow, this looks really powerful, now what does it mean?”.  I’ve started by taking the stock queries that are included in a basic NDepend project and understanding what they mean and the warnings that they are expected to generate.  By understanding the warnings and their purpose in the project, I began to understand how I could tweak those queries and even write my own to enforce the rules I’d like my code to live by.

Let’s start by looking at an example.  After creating a new NDepend project and pointing it at a list of assemblies to analyze, the default CQL queries that are included are grouped into the following categories:

  • Code Quality
  • Design
  • Unused Code / Dead Code
  • Encapsulation
  • Diff / Changes / Evolution
  • Test Coverage
  • Purity / Immutability / Side-Effects
  • Naming Conventions
  • .NET Framework Usage
  • Statistics
  • Samples of Custom Constraints
  • Constraints extracted from Source Code

08-19-2008-defaultcqlqueries

I started by looking at the Unused Code / Dead Code queries, specifically at the Potentially unused methods query.  By double-clicking on the query it is opened in the CQL query editor and displays the following:

// <Name>Potentially unused methods</Name>
WARN IF Count > 0 IN SELECT TOP 10 METHODS WHERE
MethodCa == 0 AND // Ca=0 -> No Afferent Coupling -> The method is not used in the context of this application.
!IsPublic AND // Public methods might be used by client applications of your assemblies.
!IsEntryPoint AND // Main() method is not used by-design.
!IsExplicitInterfaceImpl AND // The IL code never explicitely calls explicit interface methods implementation.
!IsClassConstructor AND // The IL code never explicitely calls class constructors.
!IsFinalizer // The IL code never explicitely calls finalizers.

Breaking it Down
Let’s break this down a little.  First of all it is important to realize that this is not SQL, despite any similarities in the syntax.  The first line selects the top 10 methods that satisfy the query and then raises a warning if the total number of methods is greater than 0.  The reason for the seemingly superfluous top 10 selection is that if the count is greater than zero then those top 10 will be displayed as the results of the query.  To avoid swamping with noise the query is restricting to only the first ten offenders.  The next line is the first part of the WHERE clause and looks for methods with an CruiseControl to bring these warning and reports into the daily life of your projects.  While static analysis en masse isn’t for everyone (not even for me), a small list of CQL queries can help you perform necessary housekeeping without having to do the hard hunting work yourself.  The inevitable question is whether or not this overlaps with FxCop.  There is some overlap, but they are both just tools from which you should pluck the best of the functionality you like and strike down the cruft that gets in the way.  There are some cases where writing an FxCop rule specific to your application is a good thing, however I’m finding that writing CQL queries for warnings lends itself a little better to how I think about static analysis in the first place.  The key is finding that sweet spot in the middle.

August 19th, 2008 Posted by stuartthompson | Technology | no comments

Project on hold

After a couple of initial snags, I’ve had to put my recent home electronics project on hold.  I started with the MadCatz wired controller, which turned out to be horribly manufactured internally and really not suitable for the project I had in mind.  I then started to work with an official Microsoft controller but found that the soldering iron I was using for the project wasn’t right for the job.  The little contacts on the PCB in the controller are about 1mm x 1mm in size.  They are extremely thin gold contacts and my 45 watt, medium tip iron was not right for the job.  I managed to get the first 10 or so soldered on well and was able to manipulate the A, B, X, Y buttons and the left, right, down directions working by tapping wires together.  However, when soldering the up direction I ran into a little trouble.  The solder joint didn’t take first time and as I tried to correct it the gold contact basically melted off in under a second.  I traced the route on the PCB and tried to scrape off a little more plastic to expose the pathway but after a few more tries found that the up direction contact and pathway were basically fried.  I’m pretty certain that with a 15 watt, fine-tip iron I would have been fine, but given that these controllers are $40 a piece and I’d just junked one, I’m going to wait a little while before trying again.  I’ll get in a little research and see if I can find some similar (cheap) PCBs to practice on first.

C’est la vie!

Stuart Thompson

August 4th, 2008 Posted by stuartthompson | Technology | no comments

Designing for Testability

Automotive manufactures have long been on the vaunted TDD bandwagon.  For years they have harnessed the amazing potential of unit testing, verifying components individually prior to the expensive and time-consuming exercise of assembling them to make a completed vehicle.  Integration testing in the automotive world is very expensive and to find that a single component failure has wasted a fleet of test vehicles as well as many months of construction and fabrication is close to unacceptable.  However, they have also long faced the question of when and where to design for testability; a line still not clearly defined in any industry.

It makes perfect sense that if a component is to be tested in isolation from the system it will eventually be integrated with that a certain amount of planning is necessary in order to facilitate that testing.  This means that certain designs, while technically and functionally brilliant, may need to be modified if they are ever to be tested.  The question is where to draw the line.  All too often in software engineering the line is drawn firmly on one side or the other, rarely in a position of balance.

I can understand on a modern motor vehicle with multiple on-board computers performing a variety of related tasks that there is a need to interface with and test those computers thoroughly.  I’m sure that to the component manufacturers the need for extra diagnostic access ports and programmable interfaces is a hindrance to their development; after all they are working in a very closed system.  However, to the test engineers the ability to simulate all manner of failures and receive highly detailed information about everything the computer is doing is absolutely indispensible.  They cannot guarantee the component functions correctly without it.

Unfortunately this desire for components to be testable can easily get out of hand.  Consider that a test engineer is tasked with ensuring that the onboard computer functions at various speeds.  Unforuntately his equipment is bulky and doesn’t fit inside the vehicle.  Even at low speeds he finds that keeping up with a car in motion requires him to run alongside pretty quickly.  Should he require that the car not go faster than 8mph in order to ensure that he can completely guarantee the functionality of the computer for the legal range of speeds he is able to test?  Something tells me that the manufacturer wouldn’t be able to sell too many of that particular model.

Instead the test engineer must find other ways to test the car at high speeds.  Custom models of the car are constructed that allow a connected rig to be tethered to the car, carrying all of the the test equipment alongside even at high speeds.  At even higher speeds, motion simulations and car treadmills are employed to allow for testing that the tethered rig is unsafe for.  The design of the car wasn’t limited to a maximum speed of 8mph, rather a compromise was found to allow the right balance of functionality and testing.

In software engineering, we have a variety of tools and frameworks at our disposal to accomplish similar testing of software.  Unit testing frameworks, mock frameworks, dependency injection containers; they are tools.  Each plays a specific role and solves a particular flavor of problem, however none of them are all-encompassing and all of them require thought before use.

There is a growing epidemic of code grown out of the desire for “high coverage” unit testing.  However, some of the motivations behind this testing appear to have taken an 8mph turn somewhere; for the worse.  In an effort to improve code quality, the initiatives towards testing have started to build 8mph cars with 6 wheels and no doors.  In terms of test coverage the cars are truly superb, but from the outside many consumers are left scratching their heads and wondering where it all went wrong.

I usually find that if a particular design decision is inhibiting the testability of a component, one of three things is happening:

  1. The design is so tightly coupled and stable that no testing could hope to penetrate the darkness of the black box surrounding it.  (i.e. a dll with one 5,000 line method inside that does “something”)
  2. The design is so abstract that is doesn’t actually do anything.  (i.e. a dll containing only interfaces - test that suckas!)
  3. The testing tool being applied is the wrong one for the job.

As a software engineer, I can fix 1 and 2.  Fortunately with good design and architecture we can actually prevent 1 and 2 from ever reaching QA in the first place.

There isn’t much I can do about number 3.  This is where I ask for the line to be moved a little with regards to the tool that is being employed.  Here are a few examples of hammering screws and screwing nails.

Static methods, unless invoked through reflection, attract coupling from consumers.  That is to say that if an assembly contains a static method, there is a good chance that something calling that static method has a strong reference to the assembly that exposes it.  This is not a bad thing and there are many places where such a design makes perfect sense.  However, in certain situations, this can cause mocking frameworks to perform more work than necessary to set up the premise for a unit test because they can’t mock away the reference the exposed assembly that performs work.  This is because mocking frameworks are ideal for mocking abstract implementations and poor for mocking tightly coupled components.  There is nothing to mock because everything is explicitly stated.  See screw, swing hammer.  Static methods are perfect for unit testing thanks to the magic of reflection.  Inspect the method contract (the logical contract not the physical source code - i.e. what, in English, does it do) and based upon that contract construct a suite of tests to affirm that the method is indeed doing what it promised it would.

Service assemblies, such as configuration loaders or proxies, tend to operate more upon interfaces than implementations.  That is because the value they add is an operation upon some implementation of an interface rather than a concrete process defined up front.  This is the provider model.  We write a set of interfaces describing the kinds of things a provider can do.  We also write a set of services than help those providers achieve some work.  We don’t write the actual providers themselves because this is a plugin model.  Well, how do we test a provider that hasn’t been written yet?  Wait, this could be hard in unit testing.  We call a method on an interface but have no way to know what the “as yet unwritten” provider will do in that method call and have no way of knowing whether the result is correct.  NTemporalDisplacementUnit 1.0 hasn’t been released yet so we can’t do it in a state of temporal flux either…panic!  But wait, in thinking about the problem further we realise that we don’t care what the provider does.  The contract we are providing is that we will interact with a provider in a predictable and documented fashion.  As long as we interact with the interface in the correct way and at the correct time, our part of the contract is fulfilled.  Woohoo!  That’s what a mock framework is for.  We mock up a provider and check that our service correctly interacts with the provider.  Mock frameworks are great for this.  Finally, a nail that we can hit with our hammer.

If you find that such thinking manifests itself in dogmatic emails that state “static methods shouldn’t be used because they inhibit testability” or “interfaces are bad because we can’t get coverage with them.” then it might be time to inspect both the situation and the tools and see that the two are being correctly paired.

The goal of testing is to improve the quality of the end product, not to turn lots of lights a pretty shade of green.  8mph cars with lots of green LEDs and no doors for great dogmatic case study’s and really awful consumer responses.

July 30th, 2008 Posted by stuartthompson | Technology | no comments

Cuil is NOT cool

The big tech news of the morning is the new search engine Cuil.  Formed by ex-google employees in an attempt to upstage their former employer, Cuil are claiming to have the largest search index on the web and also superior algorithms for search matching and results presentations.  One of the big selling points they claim is that they return larger portions of text for each search result as well as a relevant image.

I quickly headed over there and typed in my favorite search term: “stuart thompson meaghan brown”.  Yes, I narcicistically type in my own name and that of my fiance.  I have two reasons for doing this:

  1. I like to see where we rank in terms of self-relevancy
  2. I want to get a preview of what others will see if they are searching for us.

Five search results were returned, all of them from websites I own or write to.  However, I was shocked to find that of the five search results returned, four of them had images that were completely irrelevant.  Right there next to my name, my blog address, and content from articles I wrote is a picture of someone I don’t know and have never met.  WTF??

7_2d28_2d2008_cuiliscrap

Three of the articles that linked directly to my blog had pictures of other random people.  The fourth article displayed an image of the word “Proof” that I have never seen before and certainly does not appear anywhere on my site.

I use my blog to keep in touch with friends and family.  On more than one occassion I have ended up getting back in touch with someone I had completely lost contact with purely because they searched for my name and found my blog at the top of the search results.  Here they would take one look at the three images returned and decide that this certainly wasn’t me.  That is not cool!  I’m actually pretty ticked off about it.  This blog is the second review of Cuil that I have done today on this issue.  The first went directly to their administrative staff.  I’ve worked hard to author the posts on this blog and share my life and experiences with friends and I’ll be damned if those posts are going to be attributed to some other random person on the internet by some trumped-up search engine.

Yeah, perhaps Google don’t have the best results, perhaps there is room for improvement.  When I see my posts returned in results with someone else’s picture sitting right there with the text, I know for sure that this is the WRONG answer.  Give me the plain old vanilla text search results any day thank you very much, at least until you figure out how to associate the right image with the right post.

7_2d28_2d2008_googleisbetter

At least with the Google search results people would have enough suspiscion to click through and read the actual blog articles.  Maybe a bigger summary isn’t the answer?  Perhaps people should actually be given reason to read instead of just summarizing?

Clearly I’m ticked off and not fighting the case for both sides.  I completely agree that search engines need improvement.  They are the key to finding the information you want and need quickly and efficiently.  However, not at the expense of information quality.  What use is a search engine that actively throws you off-track by presenting false information right there in the results.  Worse than that, an image that is incorrect.  In my experience, most people who are surfing the web go by images first and text later.  If they are searching for a person and see an image of someone else next to the search results they are hardly going to click through to the article to “double check”.  There are too many results to double check everything.  We barely have time to scan the first page of a Google results page, let alone check results that visually look bogus to begin with.

So I ask: “Come on Cuil, or cool as you prefer to be known, please do not provide reference to online material until you can provide is accurately.”

I’m sure that I’ll incite all kind of response from this, which is partially the point.  Let’s consider the following:

  • How do you feel about the idea of content you produced being visually attributed to other people?
  • Is it important that search results are inaccurate as long as there are more of them?
  • Are there legal ramifications to this?  (i.e. what if www.pepsi.com showed an image of a Coke can in the search results)
  • Does cool factor outweigh the need for quality?

Stuart Thompson

July 28th, 2008 Posted by stuartthompson | Technology | one comment

Planning for the worst

“The status page we use to report service disruptions in progress is suffering a temporary misconfiguration.”

Brilliance!

July 25th, 2008 Posted by stuartthompson | Technology | no comments

LINQ to Coffee

Nice!  A perfect addition to the reference material you keep within reach.

http://www.cafepress.com/linq.225122905

July 15th, 2008 Posted by stuartthompson | Technology | no comments

NetFlix Streaming to XBox 360

I think I’m definitely going to have to look into this!

http://lifehacker.com/398529/netflix-meets-your-xbox-360

July 15th, 2008 Posted by stuartthompson | Technology | no comments

Debugging into .NET source code

Many have “heard” that you can now step into .NET source code when debugging in Visual Studio 2008.  For those who have not been doing this since the start of the year, you can find excellent instructions for configuring Visual Studio to get the necessary symbols here.

I highly recommend understanding this process and making it a part of your debugging routine.  It often becomes necessary to understand why the .NET framework is behaving in a certain fashion while developing.  By stepping into the source code and using the watch window, I’ve saved myself a lot of hours of head-scratching.  More often than not it’s as simple as understanding why an exception was thrown and the hidden meaning behind the message.  Within minutes I can usually pinpoint how I was incorrectly using one of the libraries, fix the bug, and be on my way.

July 14th, 2008 Posted by stuartthompson | Technology | no comments

LINQ’d In #1 - Behavior Injection

As part of a continuing set of articles on LINQ, I want to first take a moment to look at how the “natural” syntax of LINQ is turned into executable code.  Once we understand how those expressions and compiled we can investigate ways to extend the code that is being executed.

First of all let’s consider a very simple LINQ expression:

from num in Enumerable.Range(1,9)
where num % 2 == 0
select num

This expression selects the even numbers in the series 1–9.  Let’s first break this down into the actual code that is being executed.  Behind the scenes LINQ is viewing this expression as a series of lambda expressions and extension methods.  Our example is actually being run as:

Enumerable.Range(1,9).Where(n => n % 2 == 0)

Enumerable.Range(1,9) yields an IEnumerable<int>.  By using Reflector we can discover that the signature of the Where method looks like the following:

public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)

This is clearly an extension method on IEnumerable<TSource> that takes a Func<TSource, bool> as its only parameter.  While the actual internals of the Where method are actually a little more complex due to the way in which iterators have been abstracted, we could theorize that the behavior is roughly similar to the following:

public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
foreach (TSource t in source)
{
if (predicate.Invoke(t) == true)
yield return t;
}
}

“For each item in the supplied IEnumerable, invoke the supplied predicate and add the item to the returned collection if that predicate returned true.”

Note: If you haven’t encountered keyword yield before, check out this post about how it works.

Now that we understand how the statement is working internally, lets look at ways in which we can extend it.  To understand the following trick we need to think for a second about how extension methods are implemented, or more importantly about how they are resolved.

When the compiler encounters a statement of the form:

<type>.<method>() (e.g. IEnumerable<int>.Where(…))

for which there is no explicitly defined method, it searches the current list of scoped namespaces for an extension method that matches the implied signature.  The System.Linq namespace defines the extension method we’ve been examining above.  However, what if we wanted to use our own method instead of the System.Linq version of Where()?

We might want to add in some logging statements when the predicate finds a match.  Let’s do this right now by adding our own extension method for the IList<T> generic.  To do this we need to define a static class that contains a static Where method that has this IList<T> as its first parameter:

public static class ListExtensions
{
public static IEnumerable<T> Where<T>(
this IList<T> source, Func<T, bool> predicate)
{
foreach (T t in source)
{
if (predicate.Invoke(t) == true)
{
Console.WriteLine(
String.Format(”Predicate indicated success for value: {0}”, t));
yield return t;
}
}
}
}

This method is almost identical to the theoretical version of Where we built above.  The only differences are that it extends IList<T> instead of IEnumerable<T> and that it logs when the predicate finds a match prior to yielding the matched item.

Now let’s put together a little snippet that will exercise this extension method.  We can actually use the first sample we used in this post and by simply adding ToList() to the end of Enumerable.Range(1,9) we’ll be calling our extension method for Where() instead of the built-in System.Linq version.  Let’s look at that sample:

from num in Enumerable.Range(1,9).ToList()
where num % 2 == 0
select num

By simply adding the ToList() to the sample above we’ve caused the compiler to call our new Where() method.  This gives a pretty clear idea of how LINQ is working internally and what’s actually going on when we use the new C# 3.0 language keywords.  There is a lot less voodoo involved in the process that one might first believe!

Example Listing
The following is a complete example suitable for pasting into snippet compiler:

using System;
using System.Collections.Generic;
using System.Linq;

public class MyClass
{
public static void RunSnippet()
{
Console.WriteLine(“Results using the IEnumerable<T>.Where() method from System.Linq.”);
var enumResults =
from n in Enumerable.Range(1,9)
where n % 2 == 0
select n;
foreach (int n in enumResults)
Console.WriteLine(n);

Console.WriteLine(“Results using the custom IList<T>.Where() method from this sample.”);
var listResults =
from n in Enumerable.Range(1,9).ToList()
where n % 2 == 0
select n;
foreach (int n in listResults)
Console.WriteLine(n);
}

#region Helper methods

public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format(“—\nThe following error occurred while executing the snippet:\n{0}\n—”, e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write(“Press any key to continue…”);
Console.ReadKey();
}
}

private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}

private static void RL()
{
Console.ReadLine();
}

private static void Break()
{
System.Diagnostics.Debugger.Break();
}

#endregion
}

public static class ListExtensions
{
public static IEnumerable<T> Where<T>(this IList<T> source, Func<T, bool> predicate)
{
foreach (T t in source)
{
if (predicate.Invoke(t) == true)
{
Console.WriteLine(String.Format(“Predicate indicated success for value: {0}”, t));
yield return t;
}
}
}
}

July 14th, 2008 Posted by stuartthompson | Technology | no comments

BlueTooth Retro Handset

Here’s one for all the folks who had a hard time letting go of “the way things used to be”:

ThinkGeek Bluetooth Retro Handset

July 10th, 2008 Posted by stuartthompson | Technology | no comments

Deeper Project Visibility - with NDepend

Software projects, by nature, have a propensity for getting out of hand.  No matter how tightly we adhere to our carefully crafted development processes, despite diligent and methodical software designing and refactoring, the elusive software project always seems to manage to get away from us and mask its true nature from our prying eyes.  Something close to a bajillion dollars has probably already been spent in attempts to tame the software project and convince it to open up and reveal its inner secrets and with increasing success.  It has certainly been my experience that the software projects I’ve worked on in the last five years are much more transparent than they were fifteen years ago.  This is in no small part due to the fact that I am no longer a teenager banging out Win32 applications in Borland C++, stumbling my way through APIs until an acceptable solution to the problem in hand somehow occurred.  However, the view that software projects are becoming increasingly transparent is also shared by my peers.  The general consensus is that the tools and development environments have played a significant role in this, a significantly larger role than the maturity or structure of the code being developed.

This is the key observation that I have made in thinking about the problem of software project wrangling.  Despite advancement in the languages, environments, and tool-set, the code itself doesn’t seem to have become significantly more transparent.  Bear in mind also that I am not describing the “API of complete awesomeness” that represents years of careful labor and also accounts for about 0.1% of the code developed each year.  I’m talking about the other 99.9% of “copy/paste/tweak/pray” code that represents the bulk of what you and I will be maintaining, supporting, and extending for a notable portion of your software development career.  Given this (rather lofty, sweeping, and completely personal-opinion-based) assumption, thought moves again to the area in which our wrangling efforts have advanced: the tools.  If we can’t get people to architect more elegant solutions, perhaps we can find less painful ways to pry open Pandora’s box and examine the gifts our coding predecessors have left for us.

7_2d10_2d2008_ndepend_logo_small

One tool in my arsenal for this task is NDepend.  Tools and development styles will always be very personal.  There are tools that I use for which others find no benefit and similarly there are those tools that have been recommended to me that just don’t seem to do what I need.  However, I can highly recommend that any developer at least evaluate NDepend for their own use as it brings a rather unique perspective to the problem of analyzing even the most elusive of software projects.

What does NDepend do?
Arguably one of the most important questions to ask when evaluating any new piece of software is what it does.  Put simply, NDepend analyzes your project and tells you stuff about it that might otherwise be difficult to discover.  It breaks down a software project into its most basic components and then lets you query and visualize the sum of those components in extremely powerful ways.  NDepend provides a wide variety of features for software analysis but undoubtedly the majority of its value lies in the power of CQL.  The Code Query Language is not unlike traditional T-SQL in both syntax and the type of problem it is trying to solve.  However, where SQL is designed to query data stores, the CQL language is designed to query software project information.  This has several applications from code navigation and searching to coding quality evaluation and a whole host of standards metrics calculations.

In short, NDepend provides an incredible amount of information about how your project is glued together and how every field, method, type, and assembly is structured as well as how each of those elements relates to one another.  When you first run NDepend upon a solution or set of assemblies, the amount of information that is presented can feel a little overwhelming.  However, as you start to present small problems for NDepend to solve it quickly becomes clear why all of this information is very necessary.

Where do I start?
The best way to start working with NDepend is to point it at an existing set of assemblies and run an analysis.  Then we can start to pick through the individual parts of the analysis and start to benefit from the information that is presented.  For this purpose I have down-loaded the source code for an open source project called NArrange.  This provides a nice publicly available solution on which to test NDepend such that the samples in this article can easily be reproduced.

NOTE: I also recommend viewing the video tutorials on the NDepend web-site.  The getting started tutorial covers a lot of the information I present here in much more detail (and very probably with higher accuracy).  The getting started tutorial video is located here.

Step 1 – Download and Install NDepend Trial
You can download a trial version of NDepend here.

Step 2 – Analyzing a Solution
First make sure that you build your solution, either using Visual Studio or via MSBuild or another tool.  NDepend analyses a set of assemblies rather than a project or solution.

From the NDepend start page, select “Analyze: a set of .NET assemblies”.  This will display a file selection dialog which we will use to browse to the NArrange libraries.  I navigated to the NArrange.Tests.ConsoleApplication bin/Debug directory as this contains many of the solution assemblies that we wish to analyze.  Once we have selected a set of assemblies, NDepend will being to run its initial analysis.  This should take about 30 to 60 seconds depending upon system load and performance.

7_2d10_2d2008_ndependstartpage 7_2d10_2d2008_ndepend_assemblyselection

Once the analysis has completed, NDepend will display the current project in its main window as well as opening a browser that contains a full analysis report.

There is a tremendous amount of information in this report and it will take a while to become familiar with all of the data that is being presented.  To that end we will look at each of the sections in turn and adopt a step by step approach to understanding why this information is useful.

Analyzing the Analysis
Let’s first look at the project map (or VisualNDepend View).  If you close the browser that contains the NDepend Report you’ll see that the NDepend gui has been updated.  At the top is a section containing this view.

7_2d10_2d2008_projectmap

This looks a little like a topographical analysis of the moon with yellow writing on it.  In truth this is a lot like a lunar map except instead of showing the peaks and troughs of the moon’s surface it is showing the topography of our project.  The NArrange solution is split into several different projects, each of which compiles into an individual assembly.  The thin yellow lines separating sections of the map represent the boundaries between these assemblies.  Each of the little gray circles that fill in those yellow squares represents a group of code within that assembly.  In essence you are looking at the project as though it were arranged into states and counties.  Each county represents a type whereas each state represents an assembly that contains several of those types.  You can run your mouse over the project map and see that each gray area will turn red as you mouse over it.  Additionally, the name that gray area represents is displayed.  The purpose of this project map is to give you a visualization of the layout of the whole solution.  It also plays into a lot of the deeper analysis functions that NDepend has to offer and becomes a center-point for visualizing the location of code in the project.

7_2d10_2d2008_navigatingthevisualview

Start by clicking on one of the gray areas.  Note how the class browser window to the left is updated when an area is clicked.  This allows you to navigate your code visually while seeing the familiar class browser tree-view for the selected code element.  Alternatively you can click on a method or type in the class browser and see the visual view update by highlighting the appropriate area of the map.  Selecting an assembly in the class browser will highlight that entire assembly in the visual view pane.  The class browser gives a very zoomed in perspective of your project, whereas the visual view gives a fully zoomed out view of how a particular element relates to the other elements in the full solution.  I find a combination of the two to be a very powerful and expedient way to navigate a solution.

Abstractness vs Instability
Lets open the report that was generated after the initial analysis again.  To do this we can click the 7-10-2008_ViewReportButton button at the top of the Home ribbon.

7_2d10_2d2008_viewreport

The set of links at the top of the report allow you to navigate the report contents.  Click the Assemblies Abstractness vs. Instability link to be taken to a graph that pinpoints this information for each assembly that was analyzed.  This graph contains two major zones of pain and uselessness, leaving a sweet spot in the middle.  Assemblies that are both abstract and unstable live at the top-right of the graph, in the zone of uselessness.  This is to indicate that pairing abstractness with instability serves little purpose.  Assemblies in the bottom-left of the graph are very stable and tightly concrete.  This can lead to significant pain when they are maintained or extended as they are so tightly coupled and interwoven that making even small changes can require significant effort.  The sweet spot in the middle represents the correct balance between abstraction and stability.  This can be a good visual aid when learning new code or revisiting code you haven’t touched for a long time.  The pain points and potential pitfalls are identified quickly.

7_2d10_2d2008_abstractnessvsinstability

In this graph we can see that the NArrange assemblies are sitting firmly in the green zone.  The NArrange.Core assembly is the most abstract of the bunch but is also just stable enough to balance that level of abstraction.  It is expected that most test assemblies will cluster in the bottom right area of the graph, which is not a bad place to be.  Most test assemblies are fairly unstable and employ no abstraction as they are intended only for the purpose of testing a single type or set of known methods.

Additional Resources
This very brief overview was intended to scratch the surface of NDepend and give a feel for what this tool is trying to accomplish.  I’m working on more articles around the excellent CQL language and deeper features of NDepend.  These articles will be the basis of a training for some brown bags as well as the source material for more blog posts on this topic.  Meanwhile, here are some resources that are other great starting points for learning more about this great tool.

Stuart Celarier put together an excellent cheat sheet for NDepend that can be found here.  This is one of those sheets you just want to print out, laminate and either hang on the wall or find a place on your desk, perhaps even as a mouse mat.

The NDepend website contains a great series of videos and articles to help get started.

Other than that, one of the best ways to learn the tool is to simply install it and then start poking around with the analysis and learning by using.  Analyze your current project assemblies and then start navigating using the NDepend interface.  Play with some of the CQL queries (it will make reading future articles on CQL simpler because you’ll be more familiar).  Make sure to install the VisualStudio and Reflector addons as these make full circle integration a part of your life.  Other than that, I look forward to putting together my next NDepend article on using CQL to really understand and learn about how your code is put together and where it can be improved.

July 10th, 2008 Posted by stuartthompson | Blogging, Technology | no comments

HttpRequest

Have you ever wondered how an HttpRequest ends up returning a particular header code such as 500 Internal Server Error or 401 Unauthorized?  This handy chart walks you through the entire request flow:

HttpRequest Flow Diagram - (Courtesy Thoughtpad)

I think I’m going to print this out and hang it on the wall.

July 9th, 2008 Posted by stuartthompson | Technology | no comments