Google Maps are borked

I don’t know what is up with Google Maps, but when I looked at them earlier today they seem to be borked. Roads are incorrectly designated and rail tracks seem to be misaligned.

For example, if you know the Glasgow area you should instantly spot what is wrong with this:

google-bork-1

For the uninitiated, there are two north-south running motorways (blue roads). There should only be one. The easterly (right) route is actually city streets for the most part and cannot be classed as a motorway. However, just drawing the route in blue wouldn’t be so bad, but they’ve actually re-numbered the route. The road should be the A77 is now the A77(M). Not much of a change I suppose, and it doesn’t seem to needlessly route people down that road. For people that don’t know the area, and are not relying on Google‘s routing capabilities, it could add significant time on to their journey, and they might be wondering why the road is not up to motorway standards.

In fact, if you look at the route’s northern terminus, it looks even crazier for a motorway – That’s one helluva bend just before the Clyde!

google-bork-2

Technorati Tags: ,,,,

Crazy Extension Methods Redux (with Oxygene)

Back in April I blogged about a crazy thing you can do with extension methods in C#3.0. At the time I was adamant that it was a bad idea. I still think it is a bad idea, however, my thoughts have evolved a little since then and I have, possibly a solution to my hesitance to use said crazy feature.

So, if you can’t be bothered to click the link, here is a quick recap. You can create an extension method and call it on a null reference and it will NOT throw a NullReferenceException like a real method call would. At the time I was saying it was not best practice because it breaks the semantics of the dot operator which is used for member access.

Last night, I attended an excellent talk by Barry Carr on Oxygene, an Object Pascal based language that targets the .NET Framework. Oxygene has a very interesting feature, it has a special operator for dealing with calls on a reference that might be null. If that language can do it, what’s so wrong with the functionality that Extension methods potentially give? Semantics. Notice that I said that Oxygene has “a special operator”. It doesn’t use the dot operator. The dot operator still breaks if the reference is null. It has a colon operator. In this case if the reference is null (or nil as it is called in Oxygene) then the call to the method doesn’t happen. No exception is thrown.

For example. Here is the code with the regular dot operator:

class method ConsoleApp.Main;
var
  myString: String := nil;
begin
  Console.WriteLine('The string length is {0}', myString.Length);
  Console.ReadLine();
end;

And the result is that the NullReferenceException is thrown:

NullReferenceException

Here is the code with the colon operator:

class method ConsoleApp.Main;
var
  myString: String := nil;
begin
  Console.WriteLine('The string length is {0}', myString:Length);
  Console.ReadLine();
end;

And the result is that the program works, it just didn’t call the property Length as there was nothing to call it on:

Result

At this point I really would like to show you what this looks like in Reflector to show you what is going on under the hood, however, I get a message that says “This item is obfuscated and can not be translated” and the code afterwards isn’t quite right. However, the crux of it is like this in C#:

int? length;

if (myString != null)

length = myString.Length;

Console.WriteLine(“The string length is {0}”, length);

Now, back to these extension methods. After seeing this I was thinking that perhaps my total unacceptablity of allowing a null reference to be used with an extension method was perhaps incorrect. In a normal situation with an accidental null reference exception being used the NullReferenceException wouldn’t be thrown at the point of the method call (after all, the null reference is actually being passed in as the first parameter in an extension method), but somewhere in the method itself. Normal good practice would place a guard block at the start of the method so that it would be caught immediately.

However, what if you wanted to create similar functionality to the colon operator in Oxygene and have it ignore the null reference and do nothing? Well, my advice would be to create a naming convention for your extension methods to show that null references will be ignored. That way you can get the functionality with a slight semantic fudge of the dot operator. Of course, you still have to do the work and set up guard blocks to handle the null situation yourself in the extension method.

Here’s an example:

class Program
{
    static void Main(string[] args)
    {
        string myString = null;
        Console.WriteLine("The string length is {0}", myString.NullableLength());
        Console.ReadLine();
    }
}

public static class MyExtensions
{
    public static int? NullableLength(this string target)
    {
        if (target == null)
            return null;
        return target.Length;
    }
}

Oxygene talk in Dundee

Last night’s talk in Dundee on Oxygene was excellent.

My journey up was a little fraught as an accident on the A80 meant that it took me 1h15m to travel the 20 miles from Glasgow to Stirling. By that point was running late and had 45 minutes to cover the last 55 miles. In the end I arrive 15 minutes late, but that was okay because they hadn’t actually started yet.

The journey back afterwards was a bit hellish too because the rain was coming down hard and the M80 between Cumbernauld and Stirling doesn’t seem to be able to drain water quickly enough. Even reducing my speed I was aquaplaning every few hundred metres.

Anyway, Oxygene, if you don’t know, is a Object Pascal based language that targets the .NET Framework. In terms of features it contains a lot of things that would be beneficial to C# such as interface delegates, parallel coding constructs and null handling.

I will be blogging in more detail about some of these features as I plan to look further in to this language.

Tip of the Day #8 (string performance)

Concatenating strings in .NET can be very easy. There is the overloaded + operator that makes stringA + stringB + stringC statements very easy to write. But, it isn’t very performant. The reason is that strings are immutable, and concatenating strings in this way causes lots of short-lived objects to be created and thrown away, which in turn causes the garbage collector to run frequently.

There are two better ways in .NET to concatenate strings. One is to use the string.Concat() method. The other is to use the StringBuilder class. They both perform better than adding strings together, but you still have to know when to use each.

According to this article on “Performance considerations for strings in C#string.Concat() is good up to 600 strings. But, only if you have 600 strings to concatenate in a single statement. StringBuilder is better if you have more than 600 strings to concatenate, but you can do so over multiple statements. In reality, I think the benefits of appending strings over multiple statements with StringBuilder will work out better even with much less than 600 strings because to get the performance out of string.Concat() you’ll have to perform some form of setup operation to line all those strings up – and that will take time.

So, today’s tip is don’t use the plus operator to combine strings except in quick / throw-away applications. Use string.Concat() or StringBuilder instead.

SQL Server / Visual Studio Install Order

Yesterday I paved my laptop in order to upgrade to Windows Vista. I’ve now started to reinstall everything from scratch again. However, one thing that didn’t work out was the installation of SQL Server 2005. No matter what I tried I could not seem to get it to install the SQL Server Management Studio – somehow it was convinced that it already existed. I eventually figured out why.

I’d installed Visual Studio 2008 first, and as part of that installation it installed SQL Server 2005 Express edition. The express edition does not come with SQL Server Management Studio. When I went to install SQL Server 2005 it refused to install the management studio saying that more up-to-date versions of the tools were already available on the machine. (Well, I suppose some of them were, at least the ones installed by Visual Studio 2008’s installer). Running the Service Pack 2 upgrade did not help either. It concluded that the client tools were not valid as part of the upgrade and refused to install them.

Eventually I came to the conclusion that it would be quicker, given my recent wiping of my laptop to just start afresh again and install things in the correct order. I suppose I was lucky to have that option. I am also lucky that I don’t activate Windows until I’m sure everything is installed correctly – after all I do have 30 days to activate Windows. I’d hate to have lost an activation of Windows because of a dodgy install.

So what is the installation order I’ve now used that works:

  • Windows Vista SP1
  • Windows Update (my install required 33 updates)
  • SQL Server 2005
  • SQL Server 2005 SP2
  • Visual Studio 2008

 

PLEASE NOTE: The above is what worked for me. I’ve also heard that it has worked for others too. It comes with no warranties of any kind.

If you are having difficulty installing your SQL Server you may like to ask a question on one of the many fine forums that are available for asking questions of that nature. I tend to hang out on Code Project and may be able to help there. If I’m not around then one of the many other great members can possibly help you on their database forum.

Installing SQL Server 2005 on Vista

Perhaps this is not so much an issue with installing SQL Server 2005 on Vista, but of the way I install SQL Server 2005 on Vista, or even more accurately install it on my laptop on Vista.

A SQL Server will expect to run constantly on the machine that it is installed on. However, not on my laptop. I generally have SQL Server turned off on my laptop because it uses resources that it does not need. I don’t frequently use SQL Server on my laptop but I do need it sometimes. Because of this, during the installation I customise it so that the SQL Server services do not start up automatically as they normally would.

There is a slight problem with this approach I’ve discovered when installing on Vista. SQL Server 2005 came out before Windows Vista and they don’t actually get along out of the box. You have to install SQL Server 2005 SP2 (or so several dialogs claim) before you can start working with SQL Server 2005 on Windows Vista.

At the end of the process for installing SP2 it will let you know that admin users on the Vista box will not be admin in SQL Server unless you are explicit about which users to add. It then launches a “SQL Server 2005 User Provisioning Tool for Vista” to allow you to set up the admin users. However, if the SQL Server services are not running it cannot do this – the User Provisioning Tool will run, but when you apply the changes it will popup an error message and quit. So, it would seem that what I should have done is let the installer get on with running SQL Server 2005 when it finished so that the admin users could be set up. However, I didn’t and it failed. So, without any users set up on the SQL Server I could not log in.

After hunting around on disk for this User Provisioning Tool I discovered that the SQL Server 2005 Surface Area Configuration tool will allow me to launch the tool by pressing the “Add New Administrator” link in its dialog. So, with the SQL Server services (all of them indicating in the User Provisioning Tool, in my case the Database Engine and the Analysis Services) running I add myself to the list of users, click OK and…. A moment later everything seem to work. There is no confirmation, the dialog just goes away without any error messages. To test it worked I opened up the SQL Server Management Studio and attempt to log in. It works. I’m happy.

Now, finally, I go and limit the amount of memory I’m prepared to allow SQL Server to use. See my post on managing SQL Server’s memory usage. It is a laptop after all…

Update:
Please note that this was using SQL Server 2005 Developer Edition.

Speaker Training full

As I mentioned in the Scottish Developers July Newsletter Microsoft are running speaker training sessions to help community speakers get started or improve their skills with regard to technical presentations. I just got an email earlier today to say that all the places are full already. Microsoft will be running another session at some point, so if you missed this opportunity there will be another sometime in the coming months.

In the meantime if you want to get your feet wet and have a go at speaking for a short 5-10 minutes session we always welcome new speakers at Scottish Developers. Just drop me an email to colin@scottishdevelopers.com. We also have an open mic night from time-to-time so drop me an email if you’d be interested in that.

 

Monthly stats

I stopped posting my monthly stats because some of it was getting predictable. People seem to stumble across the same issues time and time again, although not the same person obviously. My post on Internal Error 2755 caused by Folder Encryption is consistently near the top of the list as is my post on the SqlException.Number property when a timeout occurs. In fact 7 of the top ten posts viewed in the last month were not even recent. By that I mean they were from last year or before.

So what recent stuff (i.e. stuff I posted this year) did make it in to the top ten? The first part to my introduction to LINQ to XML series, a series I’m still writing; a tip-of-the-day on constraining SQL Server memory usage which is a surprise because it has only been on for 10 days; and the second part, on getting data out of XML in my LINQ to XML series.

Outside the top ten, other recent  posts of note high up in the listings are:

I am encouraged by the popularity of my LINQ to XML series, and I’ll try to get more posts in that series up on my blog soon.

Technorati Tags: ,,