How to debug web applications with Firefox

December 28, 2007 | Leave a Comment

Debugging is one of the most painful parts of developing web apps. You have to deal with browser inconsistencies with HTML, CSS and javascript, let alone the difficulty of debugging javascript itself.

Here’s a rundown of the Firefox extensions I use to manage this madness.

Taming CSS: Web Developer Toolbar

Install Web Developer Toolbar. Just do it.

Debugging CSS can be really frustrating. The Web Developer Toolbar lets you inspect and edit (in real-time) the HTML and CSS of your page, so you can see what’s happening when things don’t line up. It can do a heck of a lot more, but here’s what I use it for:

Ctrl + Shift + F: Display element information. This puts a red box under your mouse. Move the mouse over an element and its attributes appear in a pop-up: the name, class, pixel sizes, fonts, everything. Here’s what you can do:

  • Figure out what classes are creating the styles you see
  • Easily get the div’s id for use with Firebug (below)
  • Figure out how big an image is (pixel height and width)

web_developer_google.png



















Ctrl + Shift + E: Edit CSS. This pops open a sidebar tab with the current stylesheets. You can edit any attributes and see the effect in real-time (like giving Google a black background):

web_developer_google_black.png



















My favorite CSS style is border: 1px solid red;

I’ve done the following hundreds of times during the course of web development:

  • Find a div with your mouse (ctrl + shift + f)
  • Get its id
  • Edit CSS (ctrl + shift + e)
  • Put a border on the div: #mydiv{border: 1px solid red;}
  • Play with widths, heights, margins and paddings until it lines up nicely
  • Remove the border

But rather than deleting the border, put an “x” in front: “xborder: 1px solid red”. The CSS won’t be valid so the border is ignored, but keeps the style around in case you want to enable it later.

Select all the text in your edited CSS file and paste it into the real CSS file. Bam, your changes are now live. It’s almost the reverse of creating a file in DreamWeaver and viewing it in Firefox. You are viewing the live file in Firefox, making changes, and copying those back into your text editor. I’ve found this very effective for editing CSS, you avoid the constant back-and-forth switching because Firefox now has a CSS editor.

Bonus: ColorZilla Picks Colors

ColorZilla gives you a dropper that can find the hex RGB value (#123456) of anything on the page. This is great when designing, and you want to match a font color to a color in your page. This is way faster than taking a screenshot and opening it up in Photoshop.

Keeping Javascript In Line: Firebug

Firebug, how I love thee. If you love yourself you will install it immediately and save countless hours of frustration.

Firebug can debug javascript, examine the DOM, and do much more (you can and should read all about it). Here’s how I use it:

F12: Open Firebug. You may have to enable it for the page.

Console Tab: Write quick javascript commands — it even has autocomplete on variable names and properties. Play around with your functions, change CSS attributes, add elements to the page — whatever it takes to test.

Script Tab (Debugging): Best. Feature. Ever. Click on a line number to set a breakpoint (red dot) in your javascript. Reload the page and it will break (pause) when it encounters the line.

At this point, you can switch over to the console to examine and change variables, and figure out what the heck is going on when your code won’t work. You can then hit the blue “play” button and continue running your app, until the next breakpoint.

Net Tab: Find the download performance of your page.

Profile Button (on Console Tab): Find the run-time performance of your page. Click “profile” to begin capturing information, do some commands, and then click stop. You’ll get a report of where your code spends its time. If you must optimize, optimize the common-case first.

If you are a more visual person, try this awesome collage:

firebug_awesome_collage.png



















Not satisfied? Check out the examples on the home page.

Dive into the details: Live HTTP Headers

Sometimes you need to dive into the nitty-gritty. What cache headers is my site sending back? Are my pages really gzip-encoded?

I know these questions keep you up at night, so here’s what you can do:

1. Install Live HTTP Headers
2. Open it (Tools > Live HTTP Headers)
3. Visit a page / press refresh
4. Rejoice

live_headers.png



















As you visit a page, you’ll see HTTP headers fly by as your browser requests elements. If items are cached, the browser may not request them at all (awesome!) or may request the element and get a 304 “Not Modified” response (slightly less awesome, you still had to check with the server). I’ve written more on cache behavior, and Live HTTP Headers is a great way to learn about HTTP caches (something every webdev should be interested in for performance reasons).

Even better, you can “replay” any header, editing the data that is sent. This is useful when testing or debugging cache or gzip encoding behavior.

Debugging IE: The lost chapter

Argh, unfortunately IE lacks these wonderful tools. There is a script debugger, but it doesn’t hold a candle to Firebug. In fact, I often just resort to alert statements, which make you shudder after being spoiled by Firebug.

One less painful method I use is this:


In your HTML: <div id="log"></div>    

In your Javascript:    

function log(str){
  var log = document.getElementById("log")
  if (log){ // let's be safe...
	 log.innerHTML += str + "<br/>";
  }
}    

Usage: log("Hi there!");

It’s nothing fancy, just a simple logging function that appends text to a div. Yes, it’s brutal, but it’s better than alert() statements, especially if you have a loop (unless you like repetitive stress injuries or want to condition yourself to fear dialog boxes). If anyone knows a good way to debug javascript in IE I’d love to know. The tools I’ve tried have been very clumsy and disjoint, taking you out of the browser.

I try to do 95% of my development in Firefox, and debug IE-specific issues (like erratic substr behavior) using this method.

Keep Getting Better

Web Developer Toolbar and Firebug can do way more than I’ve described here. Like the 80/20 rule, these are commands I use most frequently that give me the best bang for my buck. Take a few minutes to learn these tools and you’ll save hours down the line. And here’s a few more tools for web development.

These tools might not save you from getting a nervous twitch in one eye from building web apps, and that’s ok. They’ll save you from getting that twitch in both.

Mental Math shortcuts

December 28, 2007 | Leave a Comment

Here’s a collection of time-saving math shortcuts, great for back-of-the-envelope estimates.

Time and Distance

60 mph = 1 mile per minute

  • Going 60 mph and the exit is in 10 miles? That’s 10 minutes.
  • Been driving a half hour? That’s about 30 miles at highway speeds.

Feet Per Second = MPH * 1.5
MPH = Feet Per Second * 2/3 (derivation)

  • 60 mph is about 90 feet per second (88 exactly), so just multiply by 1.5. Or, just add half to itself (60 + 30 = 90).
  • Going 100 mph? That’s 150 fps.
  • Going 10 fps? That’s about 7 mph (10 * 2/3 is 6.666). Or, just take away 1/3 (10 - 3 = 7).

speed of light = 1 foot per nanosecond (derivation)

  • The US is about 3000 miles long. There’s about 5000 feet/mile, so that’s about 3000 × 5000 or 15 million feet. 15 million feet takes 15 million nanoseconds, or 15/1000, or 15 milliseconds. That’s the minimum time for a signal to go across the country.
  • Inside a microchip, if you have a clock cycle every nanosecond (1 GHz), your signal can only travel 1 foot at most (or less, depending on the material). Even light takes 30ns to cross a 30 foot room.

1 year = 250 work days = 2000 work hours (derivation)

  • Project takes 1000 man hours? That’s 6 months for 1 person.
  • Daily commute of 1/2 hour? That’s .5 * 250 = 125 hours in the car each year.

Money and Finance

$1/hour = $2000/year (derivation)

  • Earn $25/hour? That’s about 50k/year.
  • Make 200k/year? That’s about $100/hour. This assumes a 40-hour work week.

$20/week = $1000/year (derivation)

  • Spend $20/week at Starbucks? That’s a cool grand a year.

Rule of 72: Years To Double = 72/Interest Rate (derivation)

  • Have an investment growing at 10% interest? It will double in 7.2 years.
  • Want your investment to double in 5 years? You need 72/5 or about 15% interest.
  • Growing at 2% a week? You’ll double in 72/2 or 36 weeks. You can use this rule for any duration of time, not just years.
  • Inflation at 4%? It will halve your money in 72/4 or 18 years.

Mental Arithmetic

10,000 = hundred hundred
million = thousand thousand
billion = thousand million
trillion = million million

  • 1% of 10k is 100. The Dow is roughly 10k (it’s about 12k now). So if the dow drops 100, it’s about a 1% loss.
  • What’s 5k x 50k? That’s 250 * thousand * thousand or 250 million.

2^6 = 64 (the sixes match: six and sixty-four)
2^10 ~ thousand (1 kb)
2^20 ~ million (1 mb)
2^30 ~ billion (1 gb)

  • Sure, 2 to the tenth = 1024, but 1000 is good enough for government work. (Read on about KB vs KiB).
  • Have 32-bit color? That’s 2 + 30 bits, aka 2^2 billion, or 4 billion (4gb exactly).
  • Have a 16-bit number? That’s 6 + 10 bits, or 2^6 thousand, or 64 thousand (64 kb).

a% of b = b% of a

  • It’s not immediately clear, but it’s true: a% of b = .01 * a * b, which is the same as b% of a (.01 * b * a).
  • What’s 16% of 25? The same as 25% of 16: 4
  • What’s 43% of 200? Same as 200% of 43: 86.

E-Mail Password Discovery Trick

December 18, 2007 | Leave a Comment

If you are having a hard time convincing your manager that you need to start using encrypted e-mail or to go away from POP3 e-mail access, here is a nifty little trick!

  1. Install Wireshark on a computer that is off the network.
  2. Install a managed switch that supports port mirroring or port spanning between your Internet connection and your firewall.
  3. Mirror the port that your firewall is plugged into to another port andplug your computer with Wireshark into that port.
  4. Capture the information with Wireshark for about an hour (or however long you want) using the following filter:
    pop.request.command == "USER" || pop.request.command == "PASS"
  5. Show your manager everyone’s username and password that you captured from outside the network on the Internet!

You can also find all of the FTP passwords using the same method and this filter:

ftp.request.command == "USER" || ftp.request.command == "PASS"