Some Simple Thoughts

Monday, August 03, 2009

An interesting task

Over on the OzDotNet mailing list, one of the posts was asking if it’s possible to detect if there is an active exception. I believe the purpose is to change the behaviour of a method that is being called from a catch block.

While I suspect the what I consider to be the “best” solution is to pass a parameter into the function, the idea really hit a “geek spot” somewhere deep inside me. So, without further ado:

   1:          public static bool InCatchBlock()


   2:          {


   3:              StackTrace stackTrace = new StackTrace();


   4:   


   5:              bool inCatchBlock = false;


   6:   


   7:              foreach (StackFrame stackFrame in stackTrace.GetFrames())


   8:              {


   9:                  MethodBody body = stackFrame.GetMethod().GetMethodBody();


  10:   


  11:                  if (body != null)


  12:                  {


  13:                      foreach (ExceptionHandlingClause clause in body.ExceptionHandlingClauses)


  14:                      {


  15:                          bool isFinally = clause.Flags == ExceptionHandlingClauseOptions.Finally;


  16:   


  17:                          if (!isFinally && stackFrame.GetILOffset() >= clause.HandlerOffset && 


  18:                              stackFrame.GetILOffset() < clause.HandlerOffset + clause.HandlerLength)


  19:                          {


  20:                              inCatchBlock = true;


  21:   


  22:                              break;


  23:                          }


  24:                      }


  25:   


  26:                      if (inCatchBlock)


  27:                      {


  28:                          break;


  29:                      }


  30:                  }


  31:              }


  32:   


  33:              return inCatchBlock;


  34:          }




This function above, simply walks the current call stack and checks at each frame to see if we are inside a declared catch block. It seems to work fine for the limited testing I’ve done.



On a related note, I personally believe that code should not care about where it’s called from, as it creates intimate coupling with upstream code, which is more likely than not to create issues with your code.

Tuesday, April 07, 2009

National Broadband Network

Wow, it’s been months.. no, years since this process started. Finally today we got the announcement we were all waiting for. Who is going to build the NBN?? Who won, who can provide the best service??

Well, it turns out that nobody was a winner. The government has cancelled the request for tender process and has decided to go it alone.

So, the plan?? A new Government owned business, who will implement a new network, implemented over the next 8 years. $4.7 billion of initial capital, but the plan is for a total of $43 billion for the full 8 years..

Personally, I think this is a very interesting result. The NBN along with Voice over IP, the Social Internet and Mobile communications will effectively make Telstra’s existing infrastructure obsolete… I guess we will see soon what Telstra plans to do. Will they build a competing network?? Lower prices so they can actually compete?? I do hope this move brings competition to the market, and that the Governments moves will produce a workable/usable network.

Monday, April 06, 2009

A growing shrinking problem

A growing trend around the net lately, has been shrinking URLs. This isn’t a new thing, it’s been around for several years thanks to tinyurl and a few other sites.

The purpose of shrinking URLs is to make re-typing addresses easier, to make the links neater and to cut down on space.

Twitter has benefitted massively from shrinking urls. With such a small limit on message length, it’s means users can have a URL AND a little bit of info in their message. It’s a win-win.

Unfortunately, it seems that more places are also adopting the process of URL shrinking, in some cases, with very little benefit and some big downsides for me.

So, what’s the problem?? I regularly use a little feature that exists in nearly every browser, I like to look at where a link points to before deciding if I’ll click on it. See, it’s very easy to have a URL A Nice Site with Puppy Dogs that really points to www.somebadurl.com. Personally, I’d not click on the link despite the promise of puppy dogs.

Shrinking urls, unfortunately hides the true destination of a hyperlink, and as such, means that I am running blind. This means, I have to use my best judgement based on trust. Do I trust the person/site that posted the link. In general, this isn’t to bad.

But this is where it’s getting more difficult. Several social media sites are now actively shrinking all URLs posted on their site. These links can be posted by anybody, people I don’t know, people I don’t trust. The result, the sites no longer have my patronage. Sure, I’m only one person, but I’d rather be safe than run the risk of something far nastier.

Tuesday, March 24, 2009

News Flash: Wally has been found

It’s been years, many books, and finally the day has come. Here is Wally

Friday, March 20, 2009

A Quick history of the internet

Anybody who thinks Microsoft haven’t gotten their mojo back only have to stop and grab IE8 and take a look at this video (IE8 not required)..

Thursday, March 19, 2009

IE8 News

Straight from the horses mouth via twitter. “@NickHodge IE8 Final: you'll be able to download it from 3:00am AEST tomorrow. http://www.microsoft.com/ie8

So, I guess tomorrow morning I’ll be updating my IE8 RC installs.. w00t!!

Wednesday, March 18, 2009

The Joy of Development

One of the most rewarding parts of my job is delivering new software that makes a difference for my clients. It’s part of the reason I’m still working in the industry, it makes me feel good.

The other thing I really enjoy, and something that has happened today, is the ability to make a program (or small part) run faster. It was just a simple query, with a few tables, a view and sub query or two. In one of our environments the query run blazingly fast, taking only 2-3 seconds. In all of our other environments (one of which is an exact replica from 2 days ago) took a long time too complete. By a long time, I mean it took more than 15 minutes before I lost patience and stopped the process.

So, what changes did I make? Simple, it was just a restructure of the query. As I mentioned before, there was a sub query. This sub query was used within the where clause. An example is:

WHERE Table1.Field1 In (SELECT Field2 FROM Table2.Field2 WHERE Enabled = ‘Y’)

became:

FROM Table1 INNER JOIN (SELECT Field2 FROM Table2 WHERE Enabled = ‘Y’) as Table3 On Table1.Field1 = Table3.Field2

By moving this into the From section and making it an Inner Join, I was able to help the optimiser make the decision to apply the filter earlier in the execution.

The result, ever environment now runs the query sub second.

You may ask yourself, how did I know where to look? The answer is all in the tools you use. Today, I was using Toad, and a simple “Explain” on the query quickly shows you where the execution cost is. SQL Management Studio and many other tools can all provide an execution plan that you can use. There are a few things that you should focus on when looking over an execution plan. The two I focus on the most are Cost and Full Table Scans.

Cost provides you with a figure relative to the whole query about how expensive that operation is. If an operation is excessively expensive then you should try and simplify it.

Full Table Scans generally occur when there are no suitable indexes in place. This means that a filter cannot occur on an index and instead “scans the whole table”. As you can imagine, on a large table this can be a very time consuming process.

There is plenty more information available over the web on this topic. This is just one of my favourite (and easy) fixes for a very common database performance issue.

* This is a very simplified example of the actual query