Using Fiddler as a “Dev” Debugging Tool

FiddlerΒ is a great tool that acts as an HTTP Proxy. You can use it to see what your app is doing, or what other apps are doing. You can change data as it travels and see the results right away. It’s neat, use it!

What other uses can it provide? I do think that it should be an integral part of what I call “Dev” QA, or pre-QA. Developers are well known for their QA talents… NOT! πŸ˜‰ Anyway, I’m digressing here, as a developer, you can use this tool to find issues that are quite invisible.

You sometimes have file not found hits you don’t know about, that small icon, background image, css or js file link that’s obsolete, or simply not there. Fiddler will tell you. You can also see the HTTP path of your application. If it’s using AJAX, that’s even better, you can see what call you are making, what they return and so on. You can see that you are calling that Web service twice (or more) when you init your page πŸ˜‰

And then fix it! Remove or fix those 404 links, remove those extra calls. When you will put this out there for 1K users to hit, you’ll be happy you removed 2-3K hits per <time-frame here> πŸ™‚

June 8, 2007 at 7:38 pm 1 comment

.NET wants to control my Visibility!

Ok, so I want to hide a control, label or block, I put it runat=”server”Β and on the server-side I simply do MyControl.Visible = false right? Well sure, that will simply not render the whole block on the client-side. What if you then want to make the visibility back up? You need to do a full postback, yikes!

Another option is to use MyControl.Style and change the visibility client-side, something like this:

<asp:Label ID="MyLabel" runat="server">Whatever. Bla Bla Bla...</asp:Label>

On the server-side code, we then do this instead:Β 

MyLabel.Style["display"] = "none";

And on the client-side, we can toggle it back to visible like this:Β 

document.getElementById('<%= MyLabel.ClientID %>').style.display='block';

May 31, 2007 at 4:16 pm Leave a comment

Server-side escapes in AJAX using JSON

It’s nothing new for most, but it’s something that often gets missed and issue arise later.

If you build an AJAX app (both client and server-side) without using the built-in .NET AJAXΒ or any other AJAX-ready components, you have to make your pick vs XML, JSON or some kind of text output.
If you pick JSON, mainly for performance or practicality, you have to make sure you escape some characters.

How does it work (short version)
JSON will build an array or object using a tight format (less verbose than XML) but mainly using a format that Javascript can handle right away, using eval.

So, your server-side app will build the JSON array of objects (or one single object), pass it back as text to the client-side script and the script will simply do eval(JSONString) and have an object to work with.

Quick example

server-side (C#):

Response.Write("{\"Name\": \"Luc\", \"City\": \"Davis\"}");

Client-side (when the calls come back):

var myObj = eval(httpRequest.responseText); 
alert("Welcome " + myObj.Name + " from " + myObj.City);

The potential Issues

Issues will arise when you don’t escape special characters. Note that those are special characters in Javascript, nowhere else. So, since everything is in a JS string, we need to escape the string delimiter (“) and we need to escape, well, the escape character (\) πŸ˜‰ In Javascript, “\”” would return a string with ” in it.

By adding two simple replaces to your server-side code, you avoid those issues, and bugs:

Response.Write("{\"Name\": \"" + MyDBStringField.Replace("\", "\\").Replace("\"", "\\"") + "\", \"City\": \"Davis\"}")

Note #1: Don’t make the easy mistake to swap the replaces, you need to replace the escape character before the ” πŸ™‚
Note #2: You might also need to replace control characters, those between ASCII 0-31

May 30, 2007 at 2:52 am Leave a comment

Do you know .NET foreach cousin?

Say you have a List of ints like this:

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);

For List’s, you can iterate through them using a foreach loop, like this:

foreach (int i in myList) 
{
Β  Console.WriteLine(i);
}

You could also use the ForEach method of the List like that:

private static void Print(int i)
{
Β  Console.WriteLine(i);
}
myList.ForEach(Print);

ForEach will perform the action (Print) for each item in the List, kinda neat. You can also wrap the action in a delegate like that:

myList.ForEach(delegate(int i)
{
  Console.WriteLine(i);
});

So why would we do that vs foreach? It seems about the same thing, or in fact more complicated… It’s faster, you avoid a loop in your code (encapsulation I guess) and it’s cool πŸ˜‰

I would ask all of you out there why you would or would not use ForEach vs foreach, but nobody reads this yet, so πŸ˜‰

May 25, 2007 at 8:53 pm 7 comments

Developers need uninterrupted time to be productive

They do! It’s not rocket science… I just spent 30 minutes working at homeΒ in a cramped workplace (you know, laptop keyboard, touch-pad…) and was able to cram several fixes/updates to a .NET project. My estimate is that I produced the equivalent of 1/2 day at work, or at least 2 good hours from a busy office.

Now, it doesn’t mean we can always do that, or that we even should, but since we all know (PMs, stakeholders, leads and the Dev dudes themselves) why not make an effort to make it work so we all win? It’s not always possible, some (like me) need to sit on meetings, do project planning, help others in their projects and so on. But do set time to do it, close your door, stay home, set a schedule where from X to Y each day, you are not available. 2-4 hours of productive time each day can go a long way πŸ™‚

May 24, 2007 at 3:32 pm 1 comment

Slow paced Multi-player game?

Eh! They exist? Well of course πŸ™‚ Turn-based games can be a blast, mainly because of the anticipation, also because they don’t take over your life. I’m playing a few pitboss games of Civilization IV: Warlord with friends and co-workers. The nice thing about it is that you stick a turn-timer (say 24 hours) and you all hook-up asynchronously (or at the same time, kind of) and you play your turn. Then you have to wait for everyone to finish their turn or for the timer to lapse for the next turn. Give it a shot and you’ll be hooked πŸ˜‰

May 22, 2007 at 7:13 pm Leave a comment

How to detect your runtime environment in .NET

To build a neat development framework, you might need to detect your environment at runtime and make changes depending on debug mode vs not, local Dev machine vs QA, vs production.

The obvious could be to use preprocessor directives like this:

#if DEBUG 
  // Do something 
#else 
  // Do something else 
#endif

That works OK if you publish your code in Debug mode locally and Release mode on QA/production. But you might push debug code to QA (I hope not to production though). In order to make the conditional fit more and be more extensible, I simply look at the host-name like this:

if (Request.Url.Host == "localhost") 
{ 
  // Do something 
} 
else 
{ 
  // Do something else 
}

You could also add an else if block for your qa host, so you would have 3 branches, local, qa, then production.What you do in these blocks is, well, up to you πŸ˜‰ You could load a Master Page on the fly, you could force a test user login, set some paths, flags and such. Putting that kind of code in a Base Page (inheriting from System.Web.UI.Page) is a perfect spot πŸ™‚

May 22, 2007 at 5:29 pm 3 comments

SQL Indexes and substring searches

In the DB world, we often need to search for sub-strings. Let’s assume we have a very large table (MyTable) with millions of phone numbers (Number).
If we often need to lookup numbers, we’ll add an index on the Number field to speed up searches, now it’s time to build the queries:

We want to match area codes, so we code this simple SQL statement:

SELECT Number FROM MyTable WHERE LEFT(Number, 3) = '530'

Queries are sluggish, why? Well, SQL can’t use the index on Number since it’s wrapped around a function (LEFT) and who knows what this could return…
We then look at the query plan and see a table scan to confirm it all.

This is easily solved, by using a statement like this:

SELECT Number FROM MyTable WHERE Number like '530%'

We get the same results but we use the index.

This is a simple example, just to show the issue.
More often than not, you won’t have that easy fix.
You’ll need to match a sub-string, right of and so on.

In that case, you could (and maybe should) resort to de-normalizing and adding a computed column with the sub-string already parsed.
However, you need that column to be indexed and for most app I don’t think it’s worth going through the SET option requirements that this entails.
You could build your own “computed” column though, using triggers (yuck!), and index the result.

Whatever you do, make sure that you look at your query plan, don’t assume too much before you see it, and then make corrections.

May 22, 2007 at 4:45 am 5 comments

Here we go…

I think it’s time I put myself out there and see how it goes πŸ˜‰

May 21, 2007 at 10:03 pm Leave a comment


Categories

  • Blogroll

  • Feeds