Archive for the 'Development' Category

Horses for Courses (and Jockeys)

I’ve stumbled across a few blog posts lately that talk about why everyone should use one technology over another, or why someone is leaving a particular language for another. Obviously there’s no shortage of evangelical blog posts pushing the merits of one technology and lamenting the poor state of whatever-you-plebs-use.  But this latest spate got me thinking.

Most (good) developers talk about using the most appropriate technology for the job.  At its most basic level that means choosing Objective-C for a resource-hungry iPhone app, or writing your latest facebook-killer application for the web rather than the desktop.  That stuff’s obvious.  The more idealist polyglot programmers will take it further and push Ruby on Rails for web apps with a small budget, or they’ll suggest using RavenDB and deploying to AWS because all you need to do is store and retrieve documents across the web. If you’re in a Windows environment with a team running Scrum, choose TFS, C#, and SQL Server.

So “Horses for Courses” right?

The aim is valid and noble, and it’s certainly one I strive for.  But one thing frequently gets overlooked, and that’s the people on the team (or to stretch the metaphor – the jockeys).

If you have a team of programmers who are very used to writing software using certain technologies, think very carefully about advising them to move to something else. I’m not saying don’t do it (in some cases you really should), but there comes a point where the benefits to be gained by using language X on platform Y with source control Z just aren’t worth the trouble.

Unfortunately, most programmers write code in one way. They use one language, they know one data storage mechanism, and they’ve only ever written applications for one environment.  Maybe in a past life they tried out some other language, and maybe they dabble in HTML occasionally, but they’re only experts at one thing.

You, on the other hand, might look at a set of requirements and decide a NoSQL data store running behind RoR is the “best” solution for this project. Similarly, you recommend using git as the “best” source control system to use. Great. Unless you’re the only one who knows this stuff – then you’re dreaming.  If you have a team of C# developers, you’d want to have a pretty good reason for suggesting they program in a different language. If every other project they’re working on uses TFS, learning git is going to introduce a lot of overhead (initially).  Sometimes, the current way of doing things is the “best” way, even if the idealist in you disagrees.

Now, that’s not to say it’s never a good idea to force a shift within a team.  Consider a team of VB 6 developers who, for the last 15 years, have been dutifully writing VB windows applications with an Access back-end. At what point do you tell them it’s time to move on? (Ideally it would have been at least 5 years ago, but that’s clearly not an option). Assuming you don’t outsource or “refresh” the team, you should strongly suggest they change, but acknowledge that the extra effort they’ll have to put in will increase the work. Also be aware that you’re unlikely to get a quality solution from them if they don’t yet know what they’re doing.

My point is, when choosing the right technology for the job, consider everything, and that includes the skillset of the developers.

With that in mind, blog posts encouraging everybody to stop using .Net because it sucks, or telling them they should never use pure HTML and JS for business apps are just ridiculous. Yes, you might have had an overnight change of heart and now realise language X is the worst thing in the world, but you’re thinking about the specific situations you’ve been in, and developers with specific skills (usually just the individual author). If your whole team can just up and move to Ruby, then fantastic! Say hi to the rainbow coloured unicorns for me!

It’s always good to encourage teams to learn new technologies. It’s occasionally good to force a team to move on, but sometimes the “best” way isn’t the “ideal” way.

How to be a Good TFS Master

How to be a Good TFS Master

How to be a Good TFS Master

I recently gave a talk at the Qld ALM User Group on the topic of “How to be a good TFS Master”.

Hopefully those who turned up got some great tips on how to use TFS more fully, but the main points I hoped people left with were:

Most people only use about 20-30% of the capabilities of TFS

In our experience, the majority of companies using TFS are using it for source control only.  SSW helps teams get closer to using 80 or 90% of TFS where the real advantages can be felt. There are some fairly significant infrastructure costs associated with getting to 100%, and we find most organisations don’t want to go that far.

TFS is much more than source control

In fact, if you’re using TFS purely for source control, you’re doing it wrong! You might even be better off using Git or Mercurial.  The best option (of course) is to start using TFS more fully to help you get the best out of your team.

TFS supports your Scrum process beautifully

TFS has some awesome capabilities that allow you to manage and support your entire scrum process. The integration story is compelling; allowing you to tie code changes directly to user stories, produce genuinely useful reports for your managers, and enforce your policies and coding standards allowing you to tick items off your Definition of Done without any effort.

For more information or some reminders of what I spoke about, have a look at the slides on Slideshare - http://www.slideshare.net/damovisa/how-to-be-a-good-tfs-master

Using Generics for Lookup Tables in Entity Framework

I’m working with a client at the moment on a system using legacy database with (wait for it) about 50 lookup tables in their database.  We’re using Entity Framework to access the database, so it’s fairly easy to get the data out of these lookup tables.

However, I really don’t want to write 50 methods to return lists for each of these objects.  Thankfully, the solution is relatively simple – write a generic method to return the appropriate data.

Implementing this wasn’t quite as easy as I thought, but I got there after a bit of experimentation and, I’ll be honest, a fair bit of googling.

To demonstrate, let’s look at an Entity Model with a subset of the AdventureWorks database.

An Adventure Works Entity Model

An Adventure Works Entity Model

In particular, look at the red outlined objects.  These are likely to be referred to frequently throughout the application as simple lookup tables.

We want to write a single generic method that will return an IEnumerable of these objects so we can use them quite simply as lookup tables.

Here’s what I ended up with:

AdventureWorksEntities adventureWorksEntities = new AdventureWorksEntities();
 
public IEnumerable<T> GetLookup<T>() where T : System.Data.Objects.DataClasses.EntityObject
{
    try
    {
        var key = typeof(T).Name;
        // 1. we need the container for the conceptual model
        var container = adventureWorksEntities.MetadataWorkspace.GetEntityContainer(
            adventureWorksEntities.DefaultContainerNameSystem.Data.Metadata.Edm.DataSpace.CSpace);
        // 2. we need the name given to the element set in that conceptual model
        var name = container.BaseEntitySets.Where((s) => s.ElementType.Name.Equals(key)).FirstOrDefault().Name;
        // 3. finally, we can create a basic query for this set
        var query = adventureWorksEntities.CreateQuery<T>("[" + name + "]");
 
        return query.ToList();
    }
    catch (System.Data.EntityException ex)
    {
        throw new ArgumentException("Invalid Entity Type supplied for Lookup", ex);
    }
}

Now we can use the following code to return a set of all items of the appropriate type:

AdventureWorksRepository repository = new AdventureWorksRepository();
var states = repository.GetLookup<StateProvince>();
var addressTypes = repository.GetLookup<AddressType>();
var territories = repository.GetLookup<SalesTerritory>();

This has saved me countless hours of writing boring plumbing code.

Hopefully, some of you are already looking at this thinking, “couldn’t I take this a lot further and provide a set of generic data access methods for everything?”. The answer of course is yes.  I haven’t gone quite that far yet, but I wouldn’t be surprised if it was down the track.

The Rise of Participative Software

Recently I gave a presentation at Ignite Brisbane where I spoke about “The Rise of Participative Software”.

You may remember me briefly talking about this topic in an earlier post, as it’s something that I’m fairly excited about.  The general idea is that most software just does exactly what the user asks.  You give it some explicit input, it gives you some output.  Participative Software is my name for software that makes suggestions and “participates” without explicit input.

Watch the video for more details.

The five minute time limit meant that I didn’t quite get my point across as well as I’d hoped.  I’d like to present the topic over 20 or 30 minutes so I can really drill down on the subject and include a bit more of a call-to-action.

As always, feedback is more than welcome – especially if you’d like to hear a 30min version!

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.

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.

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…

Next Page »