The Next Home of Chris Chapman's Free Thoughts on Agile, .NET, SharePoint, what-have-you, whatnot. 
# Wednesday, October 31, 2007

Four years ago I wrote a bit of code to satisfy an "itch" to see what it would take to create a chess engine in C# / .NET 1.1.  Several thousands of lines of code later, it was done and spent a little time up on MSFT's WinForms Code Hero site.  I didn't set out to have it there, mind you - it sort of came up as I was in that fabled "85%" complete zone, and that caused a huge push on my part to get it done within a week to make deadline.

Jump forward to present day:  I've opened up the "crypt" and peered inside my code for the first time in ages.  Cobwebs and other detritus is strewn about - wow.  I actually wrote code like this??

So begins my journey to review my chess control code.  I have a couple of reasons to use it as a guinea pig:  I know that it has some bugs (there's an infamous one that deals with en passant captures), no unit tests and despite the best of intentions isn't that cohesive internally.  It's also .NET 1.1, so I'd like to buff it up to leverage some 2.0 features and eventually 3.5 features. 

The bulk of the code resides in a single class, RNChessRulesEngine.ChessRulesEngine, which is augmented by five helper classes for managing other features (click to enlarge):

ChessRulesEngine is a real monster:  3000+ lines of code, 100+ methods, 40+ member variables and types, excluding arrays of types.  Not all of this is a cause for concern, however:  Validating chess piece moves is a non-trivial exercise that can get complicated fast when we're dealing with bit manipulations, masks, state checks and the like.  Nonetheless, I know that this engine can be improved, especially with respect to improving its maintainability.  Which makes it an excellent candidate for a case study on refactoring existing code.  So, let's move along into the kitchen and see what we'll be using to whip this beast into shape.

The Power Tools

Scrolling up and down through the code, it quickly becomes apparent that despite having written this app, I've long since lost the conceptual mental map for understanding the mechanics.  Further, I'm not even sure where to begin.  There's a few things I'd like to do off the bat, like improving some algebraic coordinate lookup algorithms with arrays, but that's really nibbling 'round the edges.  To be a Code Viking and do Odin's Bidding, I need some power tools:

Some of these you probably already have in your kit - these are the core tools that I'll be using to tackle my ancient codebase, and I'll be referring to them in my examples as we take a stroll down my digital memory lane.  I mention them all for the benefit of others who may not be as familiar and curious.

The Motivation:  Maintainability and Cyclomatic Complexity

I've mentioned above that I want to improve the chess rules engine to "modernize" it, and part of this necessitates refactoring the unwieldy portions of the code that are... how to put this delicately...  a bloody nightmare because they're so damn complicated.  I know the routines are complex, because I wrote them.  But how can I be sure which methods to tackle first?  How do I know if my intuitions have any basis in fact?

To answer this question, I'll be leveraging a concept known as cyclomatic complexity (CC), which is a metric that measures how many decisions are taken within a method.  Put another way, it is a count of the following expressions in a method or type:

  • if
  • while
  • foreach
  • case
  • default
  • continue
  • goto
  • &&
  • ||
  • catch
  • ternary operator (ie. {expression} ? {if true} : {if false} )

In general, the higher the CC metric, the more complex your code, the more of a nightmare it will be to maintain.

Of the tools I've listed above, three provide the ability to measure CC:  NDepend, Refactor! Pro and Reflector (with the separately downloaded Metrics plugin).  I decided to use Refactor! Pro's metrics feature from within Visual Studio to get a handle on what I was in for.  This is easy to get at from the menu:

Clicking Metrics brings up a modal dialog with a list of methods and their related CC, lines of code and maintainability measures.  I filtered on CC and well, now we see where we have to begin looking to make improvements (all supported by unit tests, of course...)

As I've alluded to above, some complexity can't be avoided in this app - but when we look at the DoPlayerMove, IsCastleLegit, and GetInterceptSquares methods with CC scores of 24, 19 and 19 respectively, this tells us where we can begin our investigation and employ some strategies to get those scores down.

Next Steps:  Strategies for Reducing Cyclomatic Complexity

In my next post, I'll be reviewing my next steps for tackling the CC of my top three methods, as well as reviewing some of my other tools that I'll be employing to hack down the problem and get an even greater understanding of what's happening within the rules engine;  for this, I'll be demoing a really fantastic tool, NDepend.  Stay tuned - oh, and Happy Halloween!

Wednesday, October 31, 2007 9:37:33 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [2] -
.net | better practices | refactoring | rnchessboardcontrol

# Tuesday, October 30, 2007

Via Larkware I just noticed this excellent web utility that takes the URL-shortening concept pioneered by TinyURL (among many others) and applies a little Web 2.0 secret sauce to convert your baboon ass-ugly addresses into something more in-step with modern web navigation semantics:

After making it "decent" we get:

As the author notes on his site, the advantage DecentURL provides over TinyURL is twofold:  It provides an URL that is disambiguous, human readable and easily communicated to others, and a clear indication of the website where the link redirects.  Unless you turn on the interstitial page for TinyURL, your users get automatically redirected to the target - this can be unnerving to some users.

Pretty cool service - in the "D'oh! Why didn't I think of it first?" category.

Tuesday, October 30, 2007 2:32:22 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
web20 | webtools

Via Bink.nu, news straight outta Redmond that the hitherto model for distributing WSS (ie. a separate, free download vs. OOTB bundle) is being revived.  The reason?

Starting with RC1 and going forward, Windows SharePoint Services 3.0 will have to be downloaded separately. Basically, we made this decision to allow customers to most conveniently obtain the technology while allowing Microsoft to have flexibility in the Windows SharePoint Services development process.

Oookaaaaaay.  I'm not sure how it's going to be more convenient for me to have to download the 70 MB+ install for WSS (excluding SPs, etc.) than have the core version installed by default or at least something that could be configured to install during setup - but that's just me.  Further, WSS was never integrated into the core for Windows Server 2003 - it always was a bolt-on that sat atop the .NET/ASP.NET framework - so, how is this impacting development again?

Maybe recent news on the Windows Server vs. Linux market share front is prompting this decision to encourage even greater penetration - but WSS seems like a no-brainer for the environments that Windows Server will be installed against, especially if you're giving it away and using it as a loss-leader to get MOSS 2007 in the back-door.

Tuesday, October 30, 2007 12:46:04 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
windows server 2008 | wss30

In the aftermath of moving my site from webhost4life to Alentus, my original article on my 4-year old .NET chess board control has become lost, in addition to the SharePoint page that I had set up to re-promote it.  So, this post is just a temporary beacon to let any and all interested parties know that I've relocated the source code and demo app to the new space.  You can download them from the following links:

While originally written under .NET 1.1, I've tested migrating the control code into .NET 2.0 without any compliation errors whatsoever, so this avenue is available if you want to work with the control straightaway.

In the interim, I'm currently doing some analysis of the code for a future series of articles on improving legacy apps using commonly available third party tools like NDepend, NUnit, Refactor! Pro, etc.  It's rather fascinating and instructive to get right into code I wrote some time ago and view it through the lens of better practices that I've developed since.

As a point of interest (which I've mentioned in posts over the years about the code), rnChessBoardControl employs a time-honoured technique of using long value types (UInt64) to represent the chess board, pieces and legal moves that's known as bitboarding.  Whereas other engines use arrays to do all move validating, bitboards alllow for lightning-fast operations using bitwise arithmetic and logic.  It's an application that has many more potential uses than just chess, and harkens back to a time when programming was more concerned with lower-level truth-table style logic than the higher-level abstractions we commonly use today.

Have fun and let me know if there's any issues with the files.

Tuesday, October 30, 2007 11:36:49 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
.net | announcement | rnchessboardcontrol

# Wednesday, October 24, 2007

Update:  If you read my support ticket logs via the link below, it's also instructive to look at Call ID #311256 in the context of what led to my WSS site going to the great digital beyond.

Well, I'm back.  After an ordeal with my previous web provider (webhost4life), I was forced to move my assets to a new home (Alentus) and like moving in the real world, things got broken along the way.  *sigh*

Where it all began:  Webhost4Life

I moved my site to webhost4life about a year ago after reading Bil Simser's positive experiences with them and really reasonable pricing for WSS plans.  This is no slight to Bil, becuase after reading many, many similar disaster stories to my own, it's apparent that their service has been sliding for a long time.

Earlier this month, after seven months of on-again/off-again service my original WSS 3.0 blog that was hosted on my www.chapmanconsulting.ca domain began throwing a SharePoint error page for no apparent reason.

I contacted tech support, and within a week they succeeded in totally destroying it.  Worse:  They then deleted it entirely and were unable to find a backup more recent than July 31/07 (they're supposed to maintain them daily).

In the interim, I set up this blog engine on a sub-domain to fill the gap, and apparently it was good thing I did.  Still, it took me several days to regain about 65% of my original content from an RSS aggregation I've maintained for just such an occasion.

I wrestled with webhost4life supervisors and a manager (who I never actually spoke to because the tech support staff refused to give me their number!) for another week and a half, ultimately getting nothing for my trouble beyond a paltry offer to waive my $20 renewal fee for my WSS plan.  Ouch!

I took the liberty of creating a Google Notebook to document my ordeals with webhost4life's tech support and supervisors as a record and cautionary tale - see Call ID #372534 for the chronology of events around the destruction of my WSS site.  And be sure to read Call ID #314583 where staff tried to convince me that slow response times was due to a Microsoft "design flaw" in the WSS engine.  Another doozey is #319454 which, up until the day I left was ignored and never answered - it was filed in April.

It became obvious that I had to go - I just couldn't trust them anymore.

Back home:  Alentus

So, I've moved everything back to my old, original provider.  And that hasn't been a bowl of cherries, either.  After having a full-featured admin console where I had complete control over my entire site, I now have nothing.  Zero.  Zilch.

Alentus had pretty poor admin tools when I left them almost a year and a half ago;  not much has changed.  You need to run everything through support tickets.  From the mundane like permission changes for network accounts to creating user access accounts.

Can you say "abysmal"? 

But, at least they're stable, in Canada and have generally competent staff with reasonable turnaround.  Nonetheless, I'm keeping my eyes open for a .NET provider who has competent staff and provides me with the tools I need to manage my own site.

Let me know if you see any.

 

Wednesday, October 24, 2007 6:17:37 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [3] -
announcement

About Me
I am a Toronto-based software consultant specializing in SharePoint, .NET technologies and agile/iterative/lean software project management practices. Currently, I am employed by Microsoft Consulting Services (MCS) Canada as an Application Development and Information Worker Consultant, focusing on delivering guidance and subject matter expertise to enterprise customers who have or are in the process of deploying Microsoft technologies.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Chris R. Chapman
Sign In
Archive
<October 2007>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Statistics
Total Posts: 194
This Year: 2
This Month: 0
This Week: 0
Comments: 109
All Content © 2010, Chris R. Chapman
DasBlog theme 'Business' created by Christoph De Baene (delarou)