May 2006 - Posts

Coding Guidelines for CSS

As many of you know, I maintain and publish Coding Guidelines and Standards for a number of languages including C#, VB, JavaScript, PL/SQL, T-SQL, HTML etc. This document, however, is a little bit light on guidelines for Cascading Style Sheets (CSS).
 

CSS?
 
Rather than re-inventing the wheel I am going to refer people to Mike Rundle's CSS tips as well as PingMag's 5 steps to CSS Heaven from now on.
 

Disabling the annoying XP Automatic Update Nagging dialog box

It is that time of the month again where Microsoft has issued yet another 'Genuine Windows, kill all piracy' update via Windows Update. This is not a problem to me as I have a fully registered and official copy of XP Pro.
 
Unfortunately, whenever Windows Update downloads and installs patches it always insists on rebooting. Although I am pretty sure it is possible to patch things without requiring a reboot, I am kind of OK with it.
 
However, what I am definitely not OK with is the evil focus stealing dialog box that pops up every ten minutes asking me to reboot the system. I am a bloody busy man and I'll reboot whenever I want to, not when Microsoft wants me to.
 

 
I can get really worked up about the utterly moronic manner this has been implemented in, but rather than ranting and blowing my top I thought I'd sort it out once and for all.
 
There are multiple ways to disable this dialog. The 2 most common ones are:
  1. Temporary: Stop the service that controls the 'box of evil' by issuing the command listed below. This is only temporary as after the next reboot the service will be started again, which is a good thing.

        net stop wuauserv
     

  2. Permanent: Turn off the 'nag me' function using the group policy editor by performing the steps outlined below:

    Start -> Run -> gpedit.msc -> Local Computer Policy -> Computer Configuration -> Administrative Templates -> Windows Components -> Windows Update -> Re-prompt for restart with scheduled installations.
A more complete description, including screenshots and a lot of yelling and ranting, is available on the excellent Coding Horror blog.
 

Passionate Programmers

Yep, the news is out, I have resigned from my job as Technical Director and will go back into contracting. As always I have some mixed feelings about my decision, but I expect things to work out quite well.
 
Going back into contracting will most likely mean that I will become a full time developer / architect again, which should be fun. Ever since I started coding when I was 12 years old I have had a great passion for everything related to computer, programming and technology in general.
 
I was thinking about writing a lengthy article about this subject, but Justin James beat me to it in his Tech Republic column.
 

 
He and I seem very like minded. We share many of the same interests and we both blatantly divide all programmers in the world into two groups:
I cannot agree more. A programmer who treats their job as a way to make money and nothing more will rarely offer more than what they are asked to give. These are the programmers who churn out mediocre code on a mediocre project, do not involve themselves in making better software, just meeting the project specs, and so on. The programmers that are passionate about technology and code writing are the ones who will go home and research the best way of doing something, write code in their spare time, learn new things after hours, and more. These are the programmers you want to hire.
 I can't wait to get started again!
 

Workaround for bookmarking problem in Ajax based web sites

Anyone who has ever used rich web 2.0, Ajax, internet applications such as Google Maps or Windows Live Local has probably noticed that there is no way to easily bookmark the page with the current location while maintaining the page's state. Sure you can click on the 'permalink' or 'link to this page' links and then bookmark it, but how intuitive is that?
 
The reason for this is quite technical, the URL on the top of the page only displays the state of the page when it was loaded from the server, not the changes you have made (scrolling, zooming, navigating) since it was loaded. The result is that when you bookmark the original address you are usually presented with a map of North America, which is usually quite a few miles away from the address of the cinema you are really after.
 
What most developers don't realise is that there is a perfectly viable workaround for this bookmarking problem as you are allowed by the most common browsers' security model to dynamically rewrite the hash (#) part of a URL without performing a postback to the server. You simply serialize your state in this hash using JavaScript and deserialize it when the page is loaded again from a bookmark.
 

 
I would love to take credit for this simple, yet elegant, workaround, but I am afraid the credit goes to Mike Stenhouse, who has borrowed it from yet someone else.
 
I have placed a very simple example online that illustrates the concept. The Ajax / DHTML state is very simple, but anyone with an ounce of imagination can see that the same thing could apply to longitude, latitude and zoom factor on a mapping site.
 
The relevant part of the code is:
 

    4     <script language="javascript" type="text/javascript">

    5     function serialize()

    6     {

    7         var hashState = escape(currentState);

    8 

    9         location.hash = hashState;

   10     }

   11 

   12     function deSerialize()

   13     {

   14         var hashState = unescape(location.hash);

   15 

   16 

   17         if(hashState == "")

   18         {

   19             currentState = "1";

   20         }

   21         else

   22         {

   23             // ** Get rid of the leading #

   24             currentState = hashState.substr(1);

   25         }

   26     }

   27 

   28     function renderState()

   29     {

   30         switch(currentState)

   31         {

   32             case "1":

   33                 document.all.ajaxState.innerHTML = "State 1";

   34                 break;

   35             case "2":

   36                 document.all.ajaxState.innerHTML = "State 2";

   37                 break;

   38             default:

   39                 document.all.ajaxState.innerHTML = "Unknown";

   40         }

   41         document.title = "Ajax bookmarking fix / workaround. State: " + currentState;

   42     }

   43 

   44     function changeState()

   45     {

   46         if(currentState == "1")

   47             currentState = "2";

   48         else

   49             currentState = "1";

   50 

   51         serialize();

   52         renderState();

   53     }

   54 

   55     var currentState = "1";

   56     script>

   57   head>

   58   <body onload="deSerialize();renderState();">

 
Naturally in the real world the serialization function would be a bit more complex depending on the amount of data that needs to be serialised in order to maintain state.
 

A look into Amazon's kitchen

When you hit the Amazon.com home page, more than 100 (web) service calls are made to compile the ideal personalised page for you. ACM Queue has a candid interview Amazon CTO Werner Vogels where they discuss the distributed platform Amazon is built on, how their developers work and what drives innovation.
 

 
A very interesting read.
 

Recovering 'lost' .net source code


Please note that the names have been changed to protect the not so innocent. If you think this is about you then believe me, it is not. This blog post is completely fictional, I am not even a developer or have any knowledge about this topic.
 

 
Right, so I am working on a fun project, a blogging engine, for which someone has 'misplaced' the latest version of the source code. To make matters worse it is not clear which files are up to date and which files are old. Luckily the compiled DLLs containing the most up to date code are still available from a production server.
 
Let's not go into how or why this happened, especially considering the culprit is no longer around...surprise..surprise, but let's focus on how we can resolve the situation.
 
.NET assemblies (DLLs etc) can be disassembled into their original source code using external tools. Variable names and comments will be lost, but the syntax and structure of the code is the same. The best known tool to reverse engineer assemblies into C# classes is Lutz Roeder's .NET Reflector. Unfortunately this application only disassembles one method at a time, which makes it very time consuming to compare large parts of code.
 
Luckily a third party plug-in is available that enhances Reflector to disassemble entire Assemblies into the original code.
 
 
So, how are we going to merge the changes into the source code? Well, here is how:
  1. Compile the code that we DO have into assemblies
  2. Generate C# code from this compiled assembly using the above mentioned tools
  3. Generate C# code from the assemblies from the live server using the above mentioned tools
  4. Use your favourite DIFF tools (SourceSafe?) to compare the generated files
  5. Merge the changes into the latest source code
  6. Clean up the merged code and attempt to add some comments and sensible variable names.

 
Again, this post is not about you, it is about someone else who lost the source code, really.