Home > Ajax.Net, anti pattern, javascript > JavaScript “finally” block is anti pattern.

JavaScript “finally” block is anti pattern.

  I bet many of us hate debugging JavaScript code. The absence of strong-typing makes you pain when trying to figure out the error. So, you must employ the best patterns and practices of JavaScript and implement client code accurately. In some cases a debug tool becomes a useless tool.

  For instance, let’s review the following implementation:

function taskRunner(taskExecutor){
       finally{ //debugger would navigate you to this line

                 //do some clean up


onload = function(){

          var task = function(){

               //process something

               //and then eventually facing exception

               return i+j;// - this line of code throws exception


  The finally block of taskRunner function can be considered as anti-pattern. Whatever you do the debugger navigates you to finally block instead of real place of error at least for IE in this trivial sample. The one tool I know to figure out an error is a common sense. You have to search usages and analyze the code. Even if you manage not to use such constructions, it does not mean you to be safe. Nowadays, there are a lot JavaScript frameworks that probably use such constructions. I know one for sure. It’s the Ajax.net. The anti–pattern is exhibited in the Sys.net.XmlHttpExecutor.

  Once you have an error in one of your HttpRequest handlers, you will get into finally block immediately. How to figure out the error line? It’s good if you know what is going on, but what if you deal with legacy code and there is mix of Asp.Net update panels with ajax.net control and components(well-knowing name is extender). It is sad but your chances are low.

  1. October 1, 2009 at 9:00 am

    Hey, great blog…but I don’t understand how to add your site in my rss reader. Can you Help me, please 🙂

  2. October 4, 2009 at 11:14 pm

    Hi, just add this “https://andrewgolik.wordpress.com/feed/” to your reader

  3. Tom
    November 4, 2009 at 1:40 pm

    Hi I have the exact error you show in XmlHttpExecutor. (Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. Status code 500). Did you get anywhere on debugging it?
    All I’m trying to do is enter HTML tags into a textbox – without any open tag ‘<x' there is no error, a single '<' gives no error but any start tag '<x' gives the error that stops execution on the 'finally' as shown in your example.

  4. November 5, 2009 at 6:55 pm

    Try to use firefox with firebug extension. In many cases it works out the error.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: