Sunday, 11 March 2012

I've been away, but now I'm back

Apologies for the lack of updates. Not that I expect anyone is actually reading such an ill-kempt blog. I have been spectacularly busy both at work and at home.

However over the following weeks I will be attempting to catch up with a few technical bits and pieces which I have been meaning to share with the world.

Lastly, but most importantly, I am sorry to announce the passing on of the original mono-rail cat. Whitby died peacefully in her sleep, in her favourite spot (the hot water pipe the runs under the bathroom) a few months ago now. My wife misses her more than I do. RIP.

Friday, 1 January 2010

Raw Sushi


Some raw sushi that I made today.

Parsnip Rice
Wasabi Aioli on the top

It was delicious!
Posted by Picasa

Sunday, 13 September 2009

Learning to code

OK,

Interesting conversation with another "developer" - in the loosest sense of the word, regarding a mutual friend who is just starting to learn C#

I mentioned that the world would be a nicer place to be if all developers were given a grounding in things such as:

- Source Control
- Design Patterns
- Unit Testing

Before being let loose on the world of code.

The question is, should your first "hello world" application be unit tested? Developed using TDD? Adhere to the MVC pattern? Be in source control and properly labelled?

I think the answer is a pretty resounding "yes, of course". The reams and reams of substandard C# I see every week (mainly from code posted on the interweb) demonstrates the fact that people don't "pick up those things later on". They simply don't. How many code/function snippets have you seen on various blogs (mine included) which have associated unit tests? Hardly any.

Don't get me wrong, I'm as guilty of this as the next man - but maybe...just maybe...if I'd started out writing code in the correct manner, maybe I would find it easier to do so now...

Tuesday, 23 June 2009

NDepend and Cruise Control .Net

Well, it had to happen, I finally got around to posting my fix/hack/dirty for this problem.

1. The problem: NDepend html results are very pretty, but don't work when you integrate NDepend into CC.Net, CC.Net's own xslt log parsers mean that the pretty images created by NDepend (dependency diagrams, Zone diagram, etc) are not shown.

2. The fix!

Create new file called image.ashx (ashx = cut-down webpage without the tedious events wired up)

Stick this in there:

<%@ webhandler language="C#" class="ImageHandler" %>
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Caching;


public class ImageHandler : IHttpHandler
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext ctx)
{
try
{
string name = ctx.Request.QueryString["name"];
string project = ctx.Request.QueryString["project"];
string label = ctx.Request.QueryString["label"];
string imagePath = ctx.Request.QueryString["imagePath"];
string cacheKey = string.Format("{0}|{1}|{2}|{3}", name, project, label, imagePath);

if ( name != null && name.Length > 0)
{
Byte[] imageBytes = null;

// Check if the cache contains the image.
object cachedImageBytes = ctx.Cache.Get(cacheKey);

// Use cache if possible...
if ( cachedImageBytes != null )
{
imageBytes = cachedImageBytes as byte [];
}
else // Get the image from the project/build directory.
{
if ( !Path.IsPathRooted(imagePath) )
{
imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, imagePath);
}


try
{ // Get the image stream from the provided path.
using ( FileStream fs = new FileStream(Path.Combine(imagePath, name), FileMode.Open, FileAccess.Read) )
{
using(Image inputImage = Image.FromStream(fs))
{
using(Image outputImage = new Bitmap(inputImage))
{
using(MemoryStream stream = new MemoryStream())
{
outputImage.Save(stream, ImageFormat.Jpeg);
imageBytes = stream.GetBuffer();
}

ctx.Cache.Add(cacheKey, imageBytes, null,
DateTime.MaxValue, new TimeSpan(2, 0, 0),
CacheItemPriority.Normal, null);
}
}
}
}
catch ( Exception ex)
{
ctx.Response.Write(ex.Message);
}
}

ctx.Response.Cache.SetCacheability(HttpCacheability.Public);
ctx.Response.ContentType = "image/jpg";
ctx.Response.BufferOutput = false;
ctx.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
}
}
finally
{
ctx.Response.End();
}
}
}


Put image.ashx in the root directory of the CC.Net Web application (Program Files\CruiseControl.NET\webdashboard)

Then edit your ndepend.xsl and whenever there's a reference to an image, replace it with a reference to the .ashx file, passing in the project, imagePath, label and name as below:

\ccnet\image.ashx?project=&imagePath=&label=&name=AbstractnessVSInstability.png


et voila! Nice pictures in your combined report in CC.Net

Enjoy!

Thursday, 14 May 2009

Non-cursor cursors

So I've been doing SQL queries for a while now, but only just worked out that 99 times out of 100, whenever you think you need a cursor, you probably don't. You probably need some sort of loop and some sort of track of where you are...but the answer to that is NOT a cursor.

For example:

You have a list of portfolios that you wish to value, but you need to call a proc/function against each in turn, so doing a single query is out of the question. The answer would appear to be a cursor, iterating over a list. WRONG.

Here's how NOT to do it:

DECLARE pfo_cursor CURSOR FOR
SELECT pfo FROM #id
OPEN pfo_cursor
FETCH NEXT FROM pfo_cursor INTO @pfoLoop
WHILE @@FETCH_STATUS = 0

...do stuff with @pfoLoop

FETCH NEXT FROM pfo_cursor INTO @pfoLoop
CLOSE
DEALLOCATE


And here's a quicker, easier, cheaper, better way:

SET @pfoLoop = (SELECT TOP 1 pfo FROM #id ORDER BY pfo)
WHILE @pfoLoop is not null
BEGIN
...do stuff with @pfoLoop
SET @pfoLoop = (SELECT TOP 1 pfo FROM #id WHERE pfo > @pfoLoop ORDER BY pfo)
END


Easier, non? Quicker non? No cursors!

Labels:

Wednesday, 29 April 2009

Pfaffikon and Chicago


Apologies for the lack of recent updates. My work-life has taken a turn for the better recently, with several business trips abroad working on a new Scrum with the potential to deliver even more business benefit, quicker.

To that end, I've been working with some guys in Switzerland, and for the last two weeks have been working with some guys in Chicago.

I would have added a picture of Pfaffikon, but it's mainly populated by farms and coos, so not very picturesque.

So, that was the first time I'd been to the USA for about 20 years or so, and I have a number of things to report:

1. The adverts for prescription drugs

...are hilarious. 20 seconds of details about the drug, then a full minute on its side-effect; may cause Coma, Low blood pressure, High blood pressure, hair-loss, weight-gain, hair-gain, weight-gain, blurred vision, cataracts, nausea and death. If you die whilst taking this, please contact your GP. Best advert award "Extenze", which is a sort of Viagra/Cialis clone; "Performance AND Size? This could be fun!"

2. Eating sweets with a spoon

...seriously, if you want to be the size of a goodyear blimp, please carry on eating your sweets with a spoon

3. Cheerleaders

That's all. Normal service-ish resumed, as soon as I've had a good scrub in the bath; this is due to my good self being FORCED to use a Cursor in a T-Sql query.

Monday, 2 March 2009

Code Coverage

Today I have been mostly OBSESSED by upping my unit test code coverage.

When I inherited this current codebase all the way back in August 2008, the code coverage was 0%, nothing, nada. No unit tests and the code was in a right 2 and 8, and no mistake.

It's taken a LONG time and a lot of effort (and a lot of referring to my Design Patterns books, GOF and HeadFirst, both highly recommended, even if the examples in both are in Java rather than a proper language) - but I'm there, today I hit 93.7% with the remainder of the code being reasonably un-unit-testable (mainly in the datalayer, where no amount of fiddling will let me properly mock out OleDb connections...)

I have started work on a "Refactor to Abstract Factory" method - which I intend to demonstrate at the next "Show and Tell", just to bore the life out of the Product Owners when they ask "so what have you delivered this month?".

The upshot of this is that the codebase is now fit for purpose and can now be feature branched effectively between here, Pfaffeikon and Chicago - new development will be along the lines of extending the existing factories to include new concrete instantiations of the various factory interfaces (and some abstracts) that have been generated as part of this.

'tis all cool.

:)