Wednesday, March 18, 2009

Software Craftsmanship

I'm a fan and subscriber of InfoQ. Lots of good stuff, a bit of dross, and the occasional jewel. The latest newsletter included news of the Software Craftsmanship Manifesto: A Call to Arms.



As aspiring Software Craftsmen we are raising the bar of professional software development by practicing it and helping others learn the craft. Through this work we have come to value:



  • Not only working software, but also well-crafted software

  • Not only responding to change, but also steadily adding value

  • Not only individuals and interactions, but also a community of professionals

  • Not only customer collaboration, but also productive partnerships


That is, in pursuit of the items on the left we have found the items on the right to be indispensable.



© 2009, the undersigned. this statement may be freely copied in any form, but only in its entirety through this notice.



History Lesson


Many years ago, somewhat fresh out of school, I figured I was doing ok as a coder. The work I was involved in was generally interesting and I felt I was doing a pretty good job. That all changed dramatically a short time later when I was given the responsibility for designing and constructing from scratch the debit card PIN encryption and verification infrastructure for a New Zealand household-name financial services provider. This involved writing C++ code to interface to the hardware encryption module locked into a rack in the computer room and communicating with PIN Pads and Encoders attached to computers in the branch network, plus a little GUI development to drive it all. These were the days before the emergence of Web Services and WCF; Windows NT was the target platform for both server and workstation — there was a lot of low-level boilerplate that just didn't exist. Ultimately it all worked well, I was reasonably happy with the resulting code, and the system provided many years of reliable, low-maintenance service.


However during the project I found myself in a deep hole with respect to a core aspect of the design. I was working for a vendor organisation at the time and when I asked for some assistance the silence was unnerving. I suspect there weren't any people in the organisation with relevant experience but what I needed was more peer review than technical assistance. Fortunately I'm not shy about asking for help when I need it. I finally got to sit down with another developer who pointed me towards the newly minted Component Object Model (COM).


Books


I started reading and haven't really ever stopped. One of the early books I read was Scott Meyers awesome Effective C++. Finding out how many things I had been doing incorrectly was painful but enlightening and really highlighted to me what a challenge it is to produce good quality C++.


Eric Meyer's Eric Meyer on CSS was another watershed moment for me. After an initial foray into writing code for the Web I finally began to understand how it could work. Lets face it, up until recently everything we've done on the Web is a hack. We pretend the browser is just a dumb terminal and not a sophisticated development platform in its own right. With Web 2.0, AJAX, and REST we are beginning to see the browser platform being taken seriously with good separation between client-side and server-side.


Jeffrey Friedl's excellent Mastering Regular Expressions is another great book, David Flanagan's JavaScript: The Definitive Guide, Fowler's Patterns of Enterprise Application Architecture, Andrew King's Speed Up Your Site, Uncle Bob's (& son) Agile Principles, Patterns, and Practices in C#, Joe Celko's SQL Programming Style. Just a few of many great and often extremely readable tomes informing and affording better software craftmanship.


Voodoo


Having personally witnessed the likes of copy-paste coding, superstitious code, and voodoo chicken coding, I've often found myself trying to raise the standard of the development efforts in which I've been involved. But I've also frequently found myself working without peer-review. It is that latter situation where the right book can be valuable, far more so than wading through pages of Google search results trying to identify the one nugget of information I'm looking for.


I've rambled on long enough. Read the article, sign the manifesto, join the LinkedIn group, and raise the bar.

Thursday, March 12, 2009

NHibernate.Linq

Hiatus


Right now I need to park my attempts at configuration of a new Windows Server 2008 r2 Core web server. I've effectively managed to hose my configuration, I hope temporarily, in the process of applying a security template using secedit.exe. Not one of my best moments, particularly when I've had something similar occur previously. Unfortunately there just isn't a whole lot of info I can find about using secedit but I'll keep all those details for another post.


NHibernate


Having used the Java Hibernate ORM tool previously, plus having an interest in LINQ and wanting to cut some actual code, I figured I would kill three birds using NHibernate.Linq. Of course once I started googling it became apparent that there are many interesting things occuring in the Alt.Net space, enough to get happily side-tracked for some time.


I eventually found my way back and followed these instructions to build NHibernate from the latest subversion source. The mystery of the missing assemblyinfo.cs files was quickly solved although I did have to install NAnt and modify the default.build NAnt file to correctly generate the missing file for the NHibernate.ByteCode.Spring project. I think the entry had been removed to decouple NHibernate from Spring but the work may not have been complete at the point that I retrieved the source. The source has a comprehensive test suite which, apart from highlighting issues using NHibernate with my PostgreSQL database, is also a great starting point for information about using NHibernate.


PostgreSQL


As mentioned above, I'm using Postgres which happens to be installed on my MacBook Pro. I wanted to run the test suite against it from my Vista VM. Connecting to Postgres was actually pretty straight forward. I created a database user nhibernate with a long random password (using uuidgen) and modified the provided Postgres template, renaming it to hibernate.cfg.xml (see below). Apart from setting the appropriate values for the user credentials and server, the connection string in the template incorrectly specified "Initial Catalog" — this needed to be changed to "Database". The Postgres driver is provided by the mono project which also contains good information about the connection string parameters. Two assemblies are required, mono.security.dll, and the driver itself Npgsql.dll. Once these things are in place, and assuming you already have the nunit framework installed, the test suite runs quite happily against the Postgres database. A small percentage of tests fail, typically due to some feature difference between the target SQLServer database and Postgres, a good way to highlight those things that might bite later.



<?xml version="1.0" encoding="utf-8"?>
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="connection.connection_string">
Server=mbp;Database=nhibernate;User ID=nhibernate;Password=D4F52CC8-5603-4C84-9147-3CC602EF359A;
</property>
<property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
</session-factory>
</hibernate-configuration>

NHibernate.Linq


NHibernate.Linq is part of the NH Contrib project and the source is available from subversion. I replaced the NHibernate.dll with the one generated above. The test suite uses the NorthWind database so no joy testing against Postgres. I have SQLExpress installed in my Vista VM and with some jiggery pokery the tests will run successfully. First allow the user Full control on folder C:\SQL Server 2000 Sample Databases (or second in the event that attempting to attach the NorthWind database results in an error 5 exception). After starting SQLExpress if it isn't already running, it's necessary to attach the NorthWind database using the sp_attach_db stored procedure. And might as well create the Test database that is also required by the test suite.


By default accessing SQLExpress is limited to the Shared Memory protocol (some info and much joy can be found at SQL Server 2005 SQLExpress error: ...provider:Named Pipes Provider, error 40 - Could not open connection to SQL Server — fortunately I was already familiar with the issue, others obviously weren't so lucky). The NHibernate.Linq test suite uses Named Pipes for connecting to the database and will choke if the protocol isn't enabled. Enabling Named Pipes is straight forward using SQLServer Configuration Manager and requires the service to be restarted.


The other trap to be aware of is the surface area configuration which needed to be modified to add my user as an administrator and ensure remote connections via named pipes are enabled. This is managed by the SQL Server 2005 Surface Area Configuration tool.


app.config file for the test suite also needs to be modified to connect to SQLExpress:



<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test"
connectionString="Data Source=.\SQLExpress;initial catalog=Test;Integrated Security=SSPI"/>
<add name="Northwind"
connectionString="Data Source=.\SQLExpress;Initial Catalog=NorthWind;Integrated Security=SSPI"/>
</connectionStrings>
</configuration>

Running


To actually run the tests, the NHibernate.Linq.Test project needs to be the Startup Project and the project properties modified to use NUnit for testing.


Finally the tests run successfully. 46 of the tests are ignored which indicates there is some work yet to be completed but at least it is obvious where the work is required.