Saturday, 26 September 2009

Subverting the source of justice

A post involving the use of subversion as my source control system? How could I not use that title? :)

Anyways, whilst I am the only developer working on my own personal projects, source control is still an essential tool. I may not need to sync my work with that done by other developers, but the version tracking that it gives is something that I don't want to be without. Additionally, as I am doing these projects to enhance my knowledge of various tools and best practices, getting a source control system in place is the first step towards being able to build a continuous integration server. The benefits here are far less obvious for a single developer project, but as a learning exercise it is very useful. At the start of a project seems like the best time to get this stuff set up too, so I can focus on getting decent build scripts that run unit tests, code coverage and other analysis all sorted out up-front, so I only have to make minor tweaks to my build as I go along the path of development, rather than try to set it all up further down the road and find things potentially more tricky.

As an aid to setting up my CruiseControl.net server I followed an article on dotnetslackers.com that is part of a series about building an entire website from scratch using all sorts of best practices, just the sort of thing that I want :) (linky). The author mentioned that he was using build 1.5.0.6184 of CC so i decided to use the same build rather than get the latest so I would encounter the same nuances that he did, unfortunately that idea didn't work out as well as I hoped, but I got it running in the end. I wonder how much smoother some of my past projects might have been had I discovered this stuff earlier.

Friday, 11 September 2009

TDD for free

Today was a good day. I was given a new project at work and in the spec that had been written up there was a comment that the functionality was fairly complex and so a test plan should be written up. This screamed to me as being the first chance that I might have to be able to introduce TDD practices in my workplace, and things went rather well on that front. My boss vaguely recognised the name of NUnit, but knew nothing about it. I explained the general principal of having a suite of consistent tests attached to the code that can be run quickly and easily to make any breaking changes instantly obvious and he was happy for me to give it a shot.

The only concern that he expressed to me was that of cost. Our department is what could be described as desperately underfunded. Developers are expected to provide a certain amount of IT support to the rest of the company, we are still using SQL Server 2000 for production databases, and we only have the free Express editions of the developer tools to work with. Anything that costs money is not going to get the go-ahead because we have none to spend. Fortunately there is some very good open source software in this area, so permission was granted.

In my investigations into TDD at home I have become accustomed to being able to run my tests in the IDE by way of tools like Resharper or TestDriven.Net. This was something that I was keen to continue with as having to switch back and forth between the IDE and the NUnit GUI is an added distraction, but the Express tools don't allow you to use any plugins. Fortunately I have recently been raiding the back catalogue of the Hanselminutes podcast and remembered Scott mentioning some open source tools that had sprung up around the Mono project, including an IDE called SharpDevelop. This has an integrated NUnit test runner, so I installed it on my development box and was off.

The next step in being able to do effective TDD was to get a mocking framework in place. I have had some experience with Moq at home on C# projects, but the reason that I chose it was because it is very specifically tuned for C# and the language of choice in my current job is VB.Net. Some googling indicated that it may be a little more tricksy to use effectively with this language restriction, in fact it seemed like all of the frameworks with the possible exception of TypeMock are C# focused to the detriment of VB.Net. I ended up deciding to use Rhino Mocks as there seems to be a lot of information on the web regarding it and because the .Net community has a lot of respect for Oren Eini so I feel happy using anything that he is involved in. With my IDE integrated test runner and my mocking framework in place I was ready to get started, and despite it being a bit of a slow start as I had a bit to learn about my new toolkit I am happy with the results of my day's work.

The biggest stumbling block that I have encountered with this set-up so far came up when I started my first mock. I was setting up a mock of my repository so that the business logic could receive a result for a data request. Using the Arrange, Act, Assert (or AAA) syntax, I needed to call Stub on my object. This is an extension method added by Rhino Mocks used to program the mock object so that it can respond as desired to certain requests made of it, and it takes an argument in the form of lambdas pointing to the method that will be called. For some reason this wouldn't compile. I was given a very short error message by the compiler stating that there was an issue with the Stub method, but it was not at all helpful. I spent a while trawling the internet for clues but to no avail. I finally decided to try compiling it in Visual Studio instead of SharpDevelop, I wasn't very hopeful about what the outcome would be as SharpDevelop will still be using the same MSBuild to compile it but it remained one of few options left open to me. Unsurprisingly I had the same error, but with one very important difference. The error message was MUCH longer. I can't remember the exact detail, but the essence of it was that I had some error in the construction of my lambda in the Stub method. SharpDevelop merely told me the outermost part was failing at compilation, but Visual Studio was giving me the trace of what was going on within it and so I was able to spot and fix the error pretty quickly. I switched back to using SharpDevelop at this point so that I could continue to use the test runner.

From what I have seen of SharpDevelop so far, it is not complete enough to entirely replace having a Microsoft IDE on my system, and this could become more of an issue with ASP.Net sites as the support for this is meant to be minimal and most of my work calls for ASP. However, it offers compelling reasons to use it alongside the MS toolset when restricted to just using free software. I'm happy to be able to stick to the full version of Visual Studio with the personal version of TestDriven.net at home, and having played with trial versions of Resharper and CodeRush & Refactor I can see how these could add a lot to my productivity which SharpDevelop will never be able to match, but it has certainly helped things along today and I hope to get more out of it in the future, especially as that would imply that the company is happy for me to keep on using a TDD approach.


Sunday, 6 September 2009

ToDos in code and my all time favourite thing in .Net ever of the week

In a recentish blog post, Jeremy Miller discussed his favourite way of marking up ToDos in code in such a way that it is very very obvious that there is still work to be done and where it is needed. Whilst I like the technique it is entirely dependent on working with unit testing frameworks and in my current role we sadly do not have any sort of testing in place. I'm hoping to try and rectify this, but for the time being I need a different option. This leads me to the first bit of technical stuff in my blog. Hooray.

Operating outside of TDD I also find that ToDo's close relative of slipping quick hacks into code to ensure that a particular path will be followed during my manual testing can be very handy. Both of these are things that need to be removed from code as soon as possible, they desperately shouldn't make it into production code, and should be as obnoxious as possible to make them easy to find and remove whilst being a very noticeable smell when they are present. Adding debug or trace output can easily be lost if the program is generally chatty and requires that code to execute to even show up. Just commenting it requires me to remember to search for issues that need removing, and a little typo could lead to problems being lost forever.

The answer that I have found to this focusses around the Obsolete attribute.

[Obsolete("HACK - This call is a hack that should be removed prior to release")]
public void Hack(string message)
{
Debug.Print(message);
}
[Obsolete("TODO - This marks a placeholder for work to do")]
public void ToDo(string message)
{
Debug.Print(message);
}

When compiled this will leave warning messages anywhere that the routines are called which is suitably obnoxious unless you have a project full of warning messages and that is a whole nuther problem. A double click of the warning message takes you straight to the code that called it meaning that locating the temporary hack or ToDo is quick and easy, and the mix of intellisense and compilation errors means that you're not going to lose something that you mistyped as TiDo or similar.

Marking the code as Obsolete feels a little kludgey, but it is the only attribute that has this behaviour and it can't be derived from to create something that fits more naturally. Of course, due to the temporary nature of how the routines are used there is an argument that any time they're added they are instantly obsolete, so I can cope with the slight dodgyness.

As such things are very useful, especially in an environment that doesn't allow for TDD or effective OO practices (most of the code for many of our company's apps is situated in the same file as the ASP markup, not even in an overly crowded code-behind, just with the markup) hence this is my all time favourite thing in .Net ever for this week (and in fact the last few weeks from when I started using it)


Coffee or Synergy

I had a plan. At the time it seemed like a good plan, but in retrospect I have decided that maybe it was misguided. In an earlier post I mentioned that a lot of the interesting tools in the .Net space originated in Java. Things like NHibernate and NUnit were significant over there but were also very highly regarded in .Net and were high on my list of stuff to learn. This led me to think that I should concentrate on just using tools that exist on both platforms so I might have a better chance at performing well were I ever to have to work on a Java project.

The thing is, I'm not likely to end up in such a situation, and whilst Java and C# syntax is obviously similar I think that I would have more issues to worry about than not knowing a framework that I may or may not be allowed to use. As I spend more time learning bits and bobs about some of these tools and best practices it has become obvious that there are synergies between certain products. Researching NHibernate shows that there is a strong relationship to the Castle project, even to the point that some of their binaries are included in the NHibernate distribution. I had been planning to use Spring.Net for my IoC needs, but this makes me feel that Windsor would be a better fit, and that is the one that I hear about most when IoC in .Net comes up. Not that Spring seems to be a bad offering, it has some NHibernate functionality of its own, but the Castle Project seems that little bit more tightly bound.

Equally I had been looking into using NAnt for builds, but MSBuild seems to be a perfectly good product that will be installed on any machine that I am developing on and will also be on colleagues machines so I'm not going to cause problems if I use it. The fact that NAnt needs to call MSBuild to get anything done is another point in MSBuild's favour.

This has led me to decide that as I don't have any current requirements to work in Java, I would be better off trying to learn about what seem to be considered to be the best tools for .Net when there is a clear winner, the tools that seem to work best together when there are obvious synergies, and pay attention to what seems to best fit my requirements, or has a good learning curve and plenty of documentation. If I ever need to switch to Java I can learn to use whatever tools I need to then, and if I am already familiar with IoC, TDD, ORMs etc. then picking up a new one shouldn't be too hard as the underlying skills will be similar.

The tools that I am currently looking at are:

  • MSBuild - For automated builds that can fire off unit tests and other fancy things on a regular basis.
  • NHibernate - Deals with the CRUD in my database.
  • NUnit / xUnit / MbUnit - For unit testing to make sure that I've not just gone and broken something with a minor change that I was sure had no chance of causing a problem. NUnit is the daddy, but the others seem to have their followings too, so I am open to checking them out. MSTest seems to be less well thought of right now but this may start to change come VS2010 so I'll re-evaluate things when that surfaces.
  • Moq / Rhino Mocks - A good mock framework is essential for any serious unit testing. These 2 seems to be the best, at least in the open source arena, with Moq edging it due to reports of it being simpler to get started.
  • NCover / Partcover - for giving code coverage analysis of my unit tests. The free version of NCover is getting old now and is reportedly having some problems with some .Net 3.5 code so Partcover may end up coming to its rescue. Again this is something that may also be addressed with newer versions of studio.
  • Castle Windsor - For its Inversion of Control tools (a concept that is currently blowing my head up when I try to read about it. Methinks I'm just going to have to dive in and do some to grasp it.) There may also be other parts of the Castle Project that get a look in later on.
  • CruiseControl.Net - If I decide to set up continuous integration route then this is the tool for me, but CI may not really be applicable for a 1-man development team so it is a low priority to get sorted out.

There may well be more to add to this as I get deeper down the rabbit hole.


Monday, 3 August 2009

ClosedID

A few months back I discovered stackoverflow.com and with it a new way of logging in, OpenID. Like most people who've been online a long time, I have signed up to countless sites, creating a huge list of usernames and passwords and it keeps on growing. OpenID is an attempt to counter this trend by letting users create a single ID with a central provider and use that log on to access many different sites. So when starting work on my own website and in need of a logging in mechanism this was the thing that sprang to mind.

A little time on the aforementioned stackoverflow pointed me in the direction of a well regarded framework to easily add OpenID support to my site which i promptly downloaded. It includes a variety of samples for using it in different types of project including the ASP.Net MVC framework which is the core of my new site. After a brief scan of the code I ran the project to see it all in action. That's when things started to go wrong.

Whilst trying to log in with my OpenID (provided by myOpenID.com) I got an error indicating problems with the myOpenID.com server. Obviously with this being launched from example code running from the personal web server that is part of Visual Studio on my home network my first step was to go to a major OpenID site on the internet to eliminate all those issues. So back to stackoverflow I went, and I had problems logging on there too. I did find that by switching to use google as a provider I was able to access both stackoverflow and the example code website on my dev machine. It was not a good sign though, problems with the ID provider and all of a sudden people can't log in to my web site because of someone else's server. Still sometimes things go wrong so I decided to leave it for the night.

The following day at work I was able to log into my stackoverflow account perfectly so assumed that there had just been a temporary problem which had been fixed. However, returning to my home development that evening I still couldn't access stackoverflow or use the example code to log on. Investigating the problem further I found a directory of OpenID sites and flicking through a bunch of them I was able to log into some but others gave me the same errors. This is happening with multiple computers running both Mac OSX and Windows.

Is it a problem with the specific OpenID framework I am using, combined with something about my home internet connection that is different to my work connection? Maybe, but I don't know if all of the failing sites on the web are using this same framework or if a variety of methods of using OpenIDs are all displaying this problem. I believe I have a version of the same library as used by stackoverflow, but the others are a mystery to me. I'm considering trying out a different framework to see if that has better results, but currently the whole thing has left a bit of a bad taste in my mouth so I may just go back to the age old pattern of offering my users yet another username and password to remember, safe in the knowledge that any problems logging on to my site will be my own problems and nothing to do with a server completely outside of my authority and influence.

Friday, 24 July 2009

All this and a hot cup of coffee?

When I look at the 3rd party tools out there for .Net development it seems like everything started over in the Java community. Unit testing, ORM platforms, IoC, automated builds, mocking, basically all the good stuff that I'm trying to master. As they came over to the .Net space other projects have sprung up offering alternatives, in some cases better suited to the specifics of .Net, but many of the tools ported from Java are still considered to be great pieces of code.

To try and build a skill set that is as flexible as possible, my intention is to try and find tools that have Java counterparts wherever possible so that I could slip into Java more easily if I ever find myself in that situation. Obviously the general skills are going to be more important than the specific tools used for the job, so if I find myself having to use a different testing framework or IoC container there won't be much of a learning curve.


Sunday, 19 July 2009

So what's the big idea?

So, I'm trying to hone my developer skills by learning new technologies, best practices and so on, but there is only so much that you can learn from simply reading about things, especially in a field such as development. Therefore I shall have to put what I am learning about into practice and that requires a project (or projects) where I can use the tools and techniques. Obviously there are things that I could do at work, but I am locked in to using the company standards for development there which means I can't suddenly start working on an MVC based web site, use NHiberate for my data access etc. So I need a project that I can work on in my spare time at home, and that requires having a good idea.

Without a clear goal anything that I work on will be disjointed examples focussed on nothing but the topic I am learning. To gain a true understanding of all this stuff, I want to be able to use a variety of new techniques together, working towards a proper, real-life application. As this work is going to happen during my free time I also feel that having an interesting goal will aid with motivation as this is the time when I could be playing games or watching tv or spending time with my family or playing my guitars, or recording some music or writing a song... Ohhhh, maybe I've just hit upon an idea.

I've been playing guitar for many many years now. I enjoy coming up with my own tunes and I have a whole bunch of high quality recording gear. Unfortunately I find that whenever I sit down to write a song it ends up sounding like the sort of overly-sentimental, rubbish ballads that tend to make up most of your average Bon Jovi album once you remove the tracks that are released as singles. However, from time to time I get the odd flash of inspiration, just a couple of lines that work really well. My plan is to build an app that will allow me to store these little snippets and add a few tags to them consisting of the genre of music that I think they fit, the concept behind them, and general keywords. As the collection of lyrics builds up I could then search them based on these tags, such as find all my ballady lines when I want to write something for my girlfriend, or everything about how angry I am with the world if I tap in to the angst of my inner teenager. This may not write a song for me automatically, but it should help to provide a very good starting point.

If I create this as a web-based application it means that I can access it from wherever as inspiration strikes. As I tend to use macs at home it means that I don't need to boot into Windows, I can add things from my iPhone whilst out and about, or if something annoys me at work releasing the aforementioned inner-angsty-teenager I can save it there and then. Opening the app up to the web also adds the ability to let others use it, both random strangers and friends of mine. With musically inclined friends using the same service it makes sense that adding some collaboration features might be handy too, allowing members of a band to share ideas easily. It's hardly going to be facebook or anything, but this will add a few social networking like features to the site.

Further down the line I could extend the functionality to include saving short recordings to catalogue in addition to the lyrics. This might work better in a more traditional windows based application that could communicate with the website as a service and if I get really enthusiastic about it, I may even try to create a simple multitrack recording environment capable of hosting VST plugins to record these with, but that is a long way off and certainly out of scope for anything I am doing now.

So, that's the big idea. A multi-user database-centric website with scope for future rich client enhancements. Off the top of my head I can see how I can use the MVC framework for the site, NHibernate for my data access and OpenID for my user authentication. One of the reasons for MVC's popularity is that it allows for automated testing more easily than webforms, so NUnit could slot in to that role. Unit testing and dependancy injection go hand in hand, so this may also open up the way for using the Castle Windsor IoC container. If I do decide to build a rich client interface in the future with recording capabilities I could easily add WPF and WCF to the list of technologies in play too.

That seems like an almost overwhelming bunch of new tools and techs to get my head around so I'm going to have to take it all one step at a time. The first step involves yet another new tool that I haven't yet mentioned. In my different jobs all my source control has been via MS SourceSafe, but for my home development, due to the combination of open source licensing and its good reputation compares to SourceSafe I shall be setting up Subversion.


Tuesday, 14 July 2009

Yet another blog hits the internet

Once upon a time I thought I was a pretty hot developer and a bit of a software architect. Then the credit crunch and recession hit leaving me on the job market. A quick glance around the job boards quickly showed me that I had fallen into a rut, comfy with the skills and technologies that I knew, and that I had been stagnating. I had been using .Net 3.5 for nearly a year and had not heard of WCF, WPF, WF and that other card thingy wotsit one. Or even generics for that matter (which, for the record, are now my favourite thing in .Net). I'm a pretty smart guy, I pride myself on being able to learn new technologies quickly and easily but I had been taken by complacency and failed to keep up with the world around me, which can be easy to do in a company that isn't very forward looking.

I decided that I need to work on my skills to get myself back to the level that I once thought I was. To that end I started listening to .Net focused podcasts such as DotNetRocks, and researching the new features that I had been missing out on. Exposing myself to all of this also opened me up to the open source community and some of the great tools that are out there like NHibernate, NUnit, and various other things beginning with N. I downloaded the ASP.Net MVC framework, signed up for the Windows 7 beta and decided to kick off a little project of my own from home.

Fortunately I was not out of work for too long, and I feel that some of the knowledge that I had picked up from my newly sparked interest played a key role in me landing my job. Obviously I am keen not to fall back into that same trap and so I am continuing to try and keep learning and improving my skills, both to excel at my current job and to improve my prospects for finding my next job when the time comes. This blog is one way that I am trying to keep my momentum going. I'm not expecting to build an army of loyal fans, but rather I am using it to try and document my progress and if anyone ever reads it, that'll be a bonus. As I am going to be doing this in my own time* I may be slow to make progress as I need to fit it in around my life as a family man, but I shall be happy if I can see slow but steady progress.

*Obviously I'd like to be applying these things at work too, but when there are real-world pressures to get work done in a timely fashion and no buy-in to implementing these sort of best practices from management I can't afford a learning curve.