jQuery 1.4 released!

Yep, jQuery 1.4 has been released.

Here’s a great post from nettuts outlining some key differences and new features.

I don’t really have much to add on this, but just a few thoughts:

  1. Speed improvements are good and all, but when you can get ten times better javascript performance by changing browsers, nearly doubling the speed of your .css() method not the most important thing. Still, I can’t expect the jQuery team to be able to retire browsers for people I guess…
  2. A lot of changes seem to deal with allowing functions to be passed into methods.  Being a .Net 3.5 guy, I’m love this anonymous delegate stuff, so it’s good to see!

Yeah, that’s all the comments I have right now. Told you I didn’t have much to add!

Getting into ASP.NET MVC

It all started when I decided that I’d rewrite a partially-aborted PHP web app of mine in .Net. The PHP version was relatively functional, but PHP is not my strongest skill, so updating and improving it was hard. Combined with a lack of good quality free time, it meant that I didn’t have the inclination to update it much.

Much of the design work is already done. The database schema is solid and it isn’t trivial – it took me a number of iterations to get right. The UI flow has been decided, and at this point, I’m happy to reuse most of the layout, css and images.

At Tech.Ed this year, I heard a lot of great things about ASP.NET MVC. The Hands-On-Lab I did gave me just enough of it to get me interested, so when I decided I was going to rewrite this thing in .Net, MVC seemed like the way to go.

So I’m now attempting to learn how it all works, and it’s going really well. The secret? The NerdDinner tutorial courtesy of Rob Conery, Scott Guthrie, Scott Hanselman, and Phil Haack (actually I get the impression that Scott Guthrie wrote the tutorial, but the originating book is authored by all of them).

Seriously, if you’re looking at playing with ASP.NET MVC, run through this tutorial from start to finish. I guarantee by the end of it you’ll be all over the basics, and loving the way ASP.NET MVC is put together.

I’m looking forward to getting into some real development with this project.

Probably the best response on Stack Overflow ever

You’d probably be aware that I’m a fan of Stack Overflow. I mean, I have my SO profile over there on the right of the page.

This is just a quick post to provide a link to the best response I’ve seen on Stack Overflow. And just to clarify, I mean best in terms of amusement. It gets a point across I guess, but wow is it creative.

So here you go: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

Coding Tests in Interviews

In my travels, I stumbled across a blog post from Brandon Savage titled, “Why Coding Tests Are A Bad Interview Technique”.  While there are some points I think are fair enough, for the most part I disagree.  I’ll break down the major points so it’s easier to read.

Sending out Resumes to all available companies

Brandon mentions quite early on that his major gripe with these programming tests is that he doesn’t have time to do one for every company out there.  Every company. He’s applying everywhere.  A good developer shouldn’t need to flood the market with resumes.  If you’re any good, you should be able to be a bit more selective about where you apply.  Is the market really that bad that you need to go door-to-door begging for a job?

Writing a complete application as a coding test

In the comments, Brandon elaborates on this a bit further, saying that he’s actually had a company ask him to develop a complete database-driven, input-validating, emailing application.  Yes, that’s ridiculous and if it’s occurring more than once in a blue moon, then there’s a problem.  If a company is asking you to write an app like that just to get an interview, I view that as over the top and unrealistic.  Asking you to code up Fizzbuzz or a function to recursively search a directed graph? Not so much.

Look, as long as the code I’m writing doesn’t take me more than a few hours, and I really want to work at the company, then I’m willing to put the effort in.  If I’m flooding the market with resumes hoping for any kind of bite, then I’m probably not going to want to spend much time demonstrating my abilities to… who was it…? One of those places…

Asking for a code test is disingeneous and disrespectful

You know what? I totally disagree.  Brandon suggests that a few phone calls might be enough to determine whether someone is qualified.  Qualified? Yes, probably.  Does that mean anything? Not necessarily.

Let me digress for a bit.  I’m not sure whether the same thing exists in the US, but in Australia, we have a small training organisation that advertises late at night and promise a career in IT within a few weeks of graduating their four month course.  Four months, and they’re considered an IT professional.  These offers boil my blood – particularly because for many, many people, their qualification means the same thing as mine and my bachelor’s degree is therefore diluted.  By the way, I’m not talking about IT recruiters here; one would presume they’d know the difference.  My point is that an IT qualification by itself means nothing, and I’m not just talking about a participation certificate from Bob’s Discount IT Training.  I graduated from my degree with a scarily large number of budding programmers who couldn’t write code to save their lives.

Even experience on paper means nothing.  I’ve worked with plenty of highly qualified and very experienced people whose code just makes you cringe.  Sure, they’ve been involved in several major projects and have been an integral part of so-and-so really-important-deployment, but when you get down to it, they wrote crap code and nobody really knew any better.  You can look great on paper and still not have a clue.  I’m sure we’ve all known people like that.

Brandon suggests that as an alternative, asking for an already-written sample of code would be acceptable.  If it’s available, then sure, but I don’t think requiring one is fair for two reasons:

Firstly, the code the person writes during the day at their current job is not likely to be public.  As an employer, how would you feel about an employee showing the inner workings of your proprietary software to a rival so they can up and leave?  There’s almost certainly something in the developer’s current contract that explicitly prevents them from doing that anyway.  So what about code they’ve written in their spare time I hear you ask?  Unfortunately, not everyone writes code in their spare time and I think it’s rude to assume they do.  I do, and I’m sure a lot of developers do, but they might not for whatever reason.  You shouldn’t hold this against them.  Even if the person does write code on their own time, why should it be any more public than their company’s code?

Secondly, I’m not sure I’d be happy showing much of my own (old) code.  I look at stuff I wrote a year ago and wonder what I was thinking.  Does this mean I’m a crap developer?  No, I think it’s healthy to be able to appreciate that you’re continuing to learn.  If you can’t compare your current work to your previous work and see some improvement, then are you still learning?  I’m not alone on this line of thought either.  From a hiring point of view, I want to know how good the person is now, not how good they were the last time they wrote code that they’re allowed to show you.

It’s not all bad – what I agree with

I agree that it is very important to be able to see whether a developer is capable of learning. However, I’m not looking for the coding test to evaluate someone’s learning ability. A piece of code is a snapshot, and you’re not going to get any idea of learning and adaptability from a test like that unless you ask them to do it every six months for several years. You can evaluate whether someone learns well by asking them questions in the interview, and looking at how they keep up to date in the industry. You could even ask the default predictable question about a particular challenge they faced and how they overcame it. But nobody’s going to look at a slab of code and marvel at how well this person adapts to change. That said, if someone proudly hands you a bit of code written in VB.Net for version 1.1 of the framework using untyped DataSets, it’s probably safe to assume they’re not the learning-and-adapting type.

Brandon also suggests that if someone has a body of code samples ready to give out, then they shouldn’t have to do the test.  If those samples are appropriate and relevant, then I agree to an extent.  Why ask someone to redo something they’ve already done?  Really, though, if you want to see how well someone can think through a problem and turn it into code, then the best way to test that is to actually ask them to do it.

Summary (and tl;dr version)

If someone is applying for a job as a developer, I don’t think it’s unreasonable to ask them to write some code.  If it’s a fully-fledged application, or something that takes more than a few hours, then we’re not talking about a code test, we’re talking about free software.  Sadly, qualifications and experience don’t necessarily equal capability so you can’t just rely on that.  It’s entirely possible to have an awesome resume and loads of experience, and just be crap at writing decent code.  Being asked to show off your skills shouldn’t be taken as an insult, but an opportunity.

Lessons Learned from Entity Framework

So I’m starting a new project, and we’ve made the decision to use the ADO.NET Entity Framework for talking to the database.

I actually found it quite difficult to get started. Examples on the web seemed to assume that I already had everything installed and ready to go. Step one was invariably “Add a new ADO.NET Entity Data Model”. That wasn’t available in my “Add New Item” dialog, so I set out to discover how to add the bits I was clearly missing.

After hunting for a while for a download, I found a CTP Preview 1 built on .Net 4.0 (I’m running 3.5), and some Entity Framework tools released as a CTP in 2007.

Lesson 1: Everything you need comes with the Microsoft .NET Framework 3.5 Service Pack 1.

Of course, when I try to find the misleading links now, I find the real one straight away. Hopefully your google-fu won’t fail you as mine obviously did.

I had a quick play with it and found that creating classes and relationships between them was mindblowingly easy. Build up your pretty diagrams or get the designer to build them from a database and suddenly your code can create objects, run LINQ queries against them, bind them to UI controls and keep track of changes as you go. Brilliant!

So I got to work building up a sample object tree for our new application. When I was done, I looked for the option to persist the changes to the database.

Lesson 2: You can update your models based on database changes, but not the other way around (it’s coming in 4.0 though).

Damn, database first. Back to the start.

No problem, I abandoned my design and started with the database. Once the tables were built, generating EF data models was a piece of cake.

Next step – dependency injection for testing. I don’t want to be bound to the database, particularly for testing. I want to be able to inject fake objects for my tests.

Lesson 3: EF creates concrete model classes and database contexts. Dependency Injection is not easy.

Honestly, we were prepared for this one based on some presentations we saw at Tech.Ed. We have come up with a solution for this, too. It looks good on paper and everything seems to compile and run ok, but we’re yet to see whether this holds true as we dig deeper.

Basically, we’re leaving the concrete models as they are, but we’re extracting an interface for the ObjectContext class that’s generated. Our proxy will provide a fake ObjectContext which doesn’t talk to the database, but it will mock out real model instances. If you have any better ideas for this problem though, let me know.

Using jQuery for evil

Sometimes you find a tool that’s not just good for run-of-the-mill, intended-purpose work, but also for fixing up some bad situations.  jQuery is one such tool.

Some work I’m currently doing involves presenting nicely styled pages to the user of a web site.  The html I have to work with is not always ideal and needs to be massaged.  Because I’m dealing with templates, it’s often simply impossible for me to change the html – I can only try to modify the framework around the bits of html I’m given.

Enter jQuery.

Provided the html I get is more or less xhtml compliant, I’ve got a Document Object Model (DOM) I can work with.  Using jQuery, I’ve found that I can manipulate this to my heart’s desire.

Let’s say I end up with an table which has an extra row between the header row and the first actual row of data.  No problem!

$(document).ready(function() {
    $('#mytable tr:nth-child(2)').remove();
});

And that’s it! Of course you may get a flicker of the incorrect display because we’re making the change only when the DOM is completely built, but we’re not really choosy in this situation.

Let’s try something a bit harder.  How about reformatting a table which contains a single column for debits and credits; the debits formatted with a red <FONT> tag.  And before you ask, yes, I’m serious.

We want this table to have a separate column for debits and credits.  Again, enter jQuery.

$(document).ready(function() {
	// first, deal with the headers
	var cell = $('#mytable tr:eq(0) td:eq(2)');  // look at the second cell (contains debits/credits)
	var w = cell.attr('width').substr(0, cell.attr('width').length-1);  // I know width is a percentage
	cell.attr('width',''+(w/2)+'%');	// half the width
	cell.text('Credits').before('<td align="right" width="'+w/2+'%">Debits</td>'); // split Debits and Credits header
 
	// now deal with each non-header row
	$('#mytable tr:not(:first)').each(function()
	{
		$(this).children('td:eq(2)').before('<td align="right">&nbsp;</td>'); // insert the new column
		var redVal = $(this).children('td:eq(3)').html(); // get current column value
		if (redVal && (redVal.toUpperCase().indexOf('COLOR=RED') > -1 || redVal.toUpperCase().indexOf('COLOR="RED"') > -1)) // if it's red
		{
			redVal = $(this).children('td:eq(3)').children('font').text(); // get the actual value to write
			$(this).children('td:eq(2)').html('<span style="color: red;"> '+redVal+'</span>'); // write to the debit column
			$(this).children('td:eq(3)').html('nbsp;'); // erase the credit column
		}
	});
});

Now this doesn’t give me beautiful xhtml, and I’m never going to feel happy about how this works, but it does the job.

Fun stuff.

Just a quick Kentico menu solution

Thought I’d post this solution to what I would have thought was a fairly common problem with this product.

I’m using an ASP.Net CMS called Kentico CMS.  It is quite good – much more friendly than some others I’ve used recently.  I’ve been trying to get the CSS Menu control set up so that it will always show root pages, and also the pages beneath the current page.  It wasn’t as easy as I thought.

For example:

Home
About
 - Our Products
 - Our Services
    - Programming
    - Design
Links
 - Friends
Contact

In the example above, no matter where I was on the site, I wanted to see Home, About, Links, and Contact on the menu.  If I was on the About page, then the Our Products and Our Services links should be shown as well.  So I’m looking for the root pages and the children of the current page.  I thought this was pretty standard menu behaviour, but it took me a couple of hours to figure out.

Here’s the solution:

In the design view, edit the properties of the menu by clicking on its gears icon, go to the Content Filter section and put the following query in the WHERE condition field:

NodeAliasPath LIKE ‘{&/{0}/%&}’ OR NOT NodeAliasPath LIKE ‘/%/%’

This gets all items under the current one {0}, or anything that’s not a root element.  You’ll have to make sure the Maximum nesting level is set appropriately, but that’s it.

Hopefully someone else with the same problem will stumble across this and save themselves an hour or two.

Don’t say random when you mean unique!

I made a mistake the other day.  It seems that I used a random number when I really wanted a unique number.

Let me explain.

A service I wrote needed to send a message to an older process every 30 seconds or so.  To do this, it put a file in a particular folder and gave it an 18-digit filename.  Something like 541238740514861189.input.  The older process picked up this file, did what it needed to do, and put a corresponding response file back in the folder for my process.  Something like 541238740514861189.output.  My process picked up the response and continued doing its thing.  Fairly simple in theory.

I figured that seeing as this is a single-threaded process and it only runs every 30 seconds, seeding a random number with a time value would work.  I was even using the Ticks property of the DateTime which measures time in 10-millionths of a second.  It didn’t occur to me at the time that even though there was only one thread, there might be multiple copies of this service running.  Being a server with a multicore processor, that means that it’s possible for the same line of code to be executed at exactly the same time.

Still, it’d have to be phenomenal bad luck for the same line of code to execute in two processes within the same 10-millionth of a second, right? The odds would have to be miniscule!  Well, it turns out that while the Ticks property of the .Net DateTime might measure the number of 100 nanosecond blocks since the first of January 0001, that doesn’t say anthing about its resolution.  The resolution of DateTime is not 100 nanoseconds, it’s about 15ms.  Suddenly the chances aren’t so slim.  There are now only 2,000 blocks of time in that 30 seconds.

So yes, it was very, very unlucky for two of these service to generate the same “random” filename, but it was possible.  It was stupid of me to use a random number when I really needed a number that was unique.  They’re different things.  Solving the problem was as simple as making sure the first x characters of the filename represent the process Id.  That can’t be shared between processes so there’ll be no collisions.

Lesson learned.  Also, I should probably stop publishing my mistakes…

Offtopic – Triple J Hottest 100 of all time

I don’t often write about anything non-tech related, but I thought this was worth the detour.

Triple J is taking votes for their Hottest 100 of all time. It’s incredibly hard to choose. If you’re anything like me, you’ll find yourself with a shortlist of about 30 before you realise you’ve forgotten one of your favourite bands.

Anyway, in no particular order, here’s the top 10 I ended up with before I stopped. No doubt it would have changed many more times if I’d kept at it. Even as I type this I’m questioning my decisions…

  1. Foo Fighters – Everlong
  2. Radiohead – Paranoid Andriod
  3. The Beatles – A Day in the Life
  4. The Dandy Warhols – Bohemian Like You
  5. Jeff Buckley – Hallelujah
  6. Crowded House – Four Seasons In One Day
  7. Regurgitator – ! (the Song Formerly Known As)
  8. The Rolling Stones – Sympathy For The Devil
  9. Queen – Bohemian Rhapsody
  10. Coldplay – A Rush Of Blood To The Head

Ten is totally not enough…

Pride in bad solutions

I solved a problem the other day.  It was a terrible solution. It works, but it’s difficult to be proud of how I solved it.

The problem was with MSMQ, but let me describe the problem with a needlessly overwrought metaphor.

Every week for the last 5 years, you’ve been sending a bill to one of your clients.  You have an infinite supply of envelopes and when Friday comes around, you print out an invoice, seal it in an envelope, send it by courier to your client, and a couple of days later, you get paid.  With me so far?

One day, you decide to start using a different invoice management software package – it’s much prettier and more stable than the old one.  You know, however, that the client doesn’t want anything in that invoice to change.  You’re not sure how they process it, but one time your printer smudged an invoice slightly and you didn’t get paid.  So everything in that invoice has to be exact.  Luckly, the new software is capable of printing a pixel-perfect version of the old invoice layout.  So far so good.

You also think you should start using a different courier.  Your existing courier company (COM+) is fine, but frankly they’re a bit behind the times.  The delivery drivers are about 80 and they’re driving some vans that are generations old.  So you set up a deal with a new company called C-Sharp 3.5 to do the delivery for you.

Still with me?  I told you it was overwrought.

So anyway, you give your new system a try.  You print out the invoice, compare it to the old one (spot-on, 100% the same), put it in an envelope, and send it off using the new courier company.

You don’t get paid.

You contact the customer but they don’t say much.  Just that they didn’t receive a valid invoice.  The new courier company swears they delivered it to the correct address and they provide proof.

You decide to try again next week and do some investigating.

The next week, you print out the invoice, compare it to the old ones (still the same), put it in an envelope, and organise for the new courier to pick it up.  This time though, you follow him.

He takes your letter, gets in his van, and drives to the client’s address.  He gets out, puts your letter in the mailbox and drives off.  Nothing wrong so far, as far as you can tell, so you wait to see what happens.

Soon, a guy comes out of the house (which by the way looks exactly like a big black box), opens the mailbox, picks the letter up, and takes it inside.

You go home, satisfied that the letter made its journey this time.  It must have been a once off.  But you still don’t get paid.

Again, you contact the client and all they’ll tell you is that they didn’t get a valid invoice.  You protest, telling them that you saw them pick it up but to no avail.

Over the next few weeks, you try everything you can think of to get this new system to work.  You try looking at the message again after it’s put in the letterbox and you try sending an invoice created by your old program.  Every time, the seemingly perfect letter gets picked up my the man in the black box, and nothing happens.

No matter how hard you try to work out what’s going on, nothing helps.  So, like any sensible person,  you give up and call the old courier company.  They turn up, pick up your invoice, and two days later, you get paid.  Despite the fact that they’re apparently doing exactly the same as the new guys, their deliveries get you paid, and the other deliveries don’t.  You resign yourself to using the old couriers until they or their vans all die.  It’s just a matter of time…

Fun story, huh?

So in case you’re a bit slow and didn’t work out what it was all about, I replaced an app that put a message in a Windows message queue for another (black box) program to pick up.  The old app was VB6 using a COM MSMQ library, and the new one was C# using the native .Net MSMQ library in the 3.5 version of the .Net Framework.

No matter how hard I looked, I couldn’t find ANYTHING different between the messages and where they got put.  I even dug up a copy of the black-box code and stepped it through a debugger.  When it got to the line saying Queue.Receive(), the message disappeared from the queue, and nothing came back.  There was no exception thrown!  I watched the message get picked up!  I did a binary comparison of the message contents from the old program and the new program!  No difference at all!  Inexplicable!

The fact that I was putting the message into the Windows Message Queue using a .Net library and picking it up using COM+ seemed to be all it took to break the thing.

This is the first time I can remember being absolutely, 100% stumped by what was going on.  I’d analysed what was going on as deeply as was practical and had come up with nothing.

So I did the logical thing and referenced the COM+ dll from my new app.  When I used this library to send the message, it all worked perfectly.

I really don’t like this fix. Despite the fact that everything works perfectly now, it’s still a failure in my eyes.  It’s like a brand new Merc with duct tape holding the wheels on.

However, given that I’d spent way too long already on something that really shouldn’t have been a problem, it was probably the right thing to do.  Cut my losses and take the easy way out.  I’m not proud.

Next Page »