Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

12 Steps to a Bigger, Better, Debugger

Rated 4.44 (Ratings: 19) (Add your rating)

Log in to add a comment
(23 comments so far)

Want more?

 
Picture of nagrom

Morgan Kelsey

Member info | Full bio

User since: February 09, 2002

Last login: November 28, 2008

Articles written: 2

Think back to the first time you wrote some code. It might have been:

<h1>King Crimson Rules!</h1>

Or maybe:

[BASIC]<br>
10&nbsp;PRINT &quot;I repeat myself when I am in stress&quot;<br>
20 GOTO 10<br>
[/BASIC]

No matter what it was, chances are the first thing you saw on your screen was:

SYNTAX ERROR Near line XX. <br>
Can't you even type? <br>
Why are you so stupid?

Maybe some of you have perfect queries dripping from your fingers, and well-formed functions hopping across your keyboards. Perhaps you dine on multi-dimensional arrays and wash them down with precisely nested ifs—but I know I sure don't. If you're like me, you've probably seen more error screens than 200s.

Whether writing server-side, client-side, or regular-old-markup, you need to develop some debug muscles. Errors are an incurable side effect of human programming, and when they pop on your screen, you can probably hear your bank account draining. Become an adept debugger and you'll write cleaner apps, in shorter time.

Bigger Debuggers in 12 Easy Steps

Considering how much time we all spend debugging, it is ironic how rare a topic it is in books and conversation. So how does one become a better debugger? Hopefully, the twelve guidelines I've compiled here will help you squash bugs faster, smarter, and with more authority.

1. Organize your code.

Let's face it, building websites isn't rocket science. If you've been at it for 6 months, you've probably developed some sort of methodology. All sites share common elements, pay attention to what they are and re-use code as much as possible. Organize your code and includes so you know where everything is.

The W3C is pushing standards that separate content from structure; you'll find adopting this philosophy helps when debugging as well. Some general guidelines:

  • Code once, include often.
    I'll say it again, organize your code so you know where everything is. Strive to have no duplicated code on your site.
  • Externalize client-side scripts.
    Put all CSS and JavaScript into external includes. Your pages will load faster, as the scripts will go into the browser's cache. Also, you'll be able to disable all your JavaScript or all your CSS more easily. Resist the temptation from the darkside, never code CSS inline or write javascripts willy-nilly through your pages. You'll be happy later when you want to either turn it all off, or export it to another project.
  • Refine it.
    Every website gains features through the development cycle. When a complex project reaches the 75% complete zone, it is usually begging, pleading, and bleeding for a rewrite. Try to take the 2, 3, maybe 4 days it will take to go back through your code and refine it when it feels out of hand. Don't be the type of a developer that has an FUNW folder for an inbox. Try to build in as much scalability as you can from the start.

2. Isolate and identify the problem.

This is the key to all debugging, and a close cousin of code organization. The problem code block must be identified, and identified quickly. If you're debugging-while-developing (DWD), you know the first place to look is the code you were just working on. If you're debugging-while-updating, or wading through someone else's work, isolation becomes more difficult.

  • Trust no-one.
    Your first suspect should be the machine you're testing on. Is the error totally bizarre? Does another, known to be working page serve OK? Computers crash all the time, and test boxes that kick more errors than successes all day will tend to crash more. If you test on a *nix box, you probably find this paragraph funny. If you're on Windows, reboot your test machine at least once a day.
  • Believe the error message.
    If you're reasonably sure your machine is stable, look for clues in the error message. It's funny how those messages make sense afterwards, isn't it? Line numbers in error messages are almost always wrong, but are invariably the best place to start looking. Head backwards from the line number reported, and check the nesting of that segment. When you can't find the error, comment out sections of your code until the error stops popping. Then cut and paste the problem block into a separate template for surgery.

3. Validate it!

Make the validator your home page. Writing valid HTML of some flavor is an excellent first step in error prevention. If you're new to validation, start with 4.01 Transitional. There are also many standalone tools available, the W3C maintains a list here.

4. View the source, Luke.

When you're working server-side, many times you can find problems faster by viewing what came out, rather than looking at what went in. Check the output, test variables using HTML comment blocks, or use a simple JavaScript alert to dump server-side variables. If you're dealing with a variable that changes throughout the page, output it several times.

<script language="JavaScript" type="text/JavaScript">
	alert('$some_variable');
</script>

5. Be the layout master, not the master layouter.

When dealing with a layout problem, first determine if it is an HTML or CSS problem.

  • View it naked.
    Try disabling CSS, that is, if you have it in external files.
  • Preview in many browsers.
    Test in whatever browsers and resolutions are appropriate throughout the entire project. Don't wait until the very end to discover your client's V.P. uses AOL 2.0 on an Amiga with a 13" monitor. HTML with syntax errors usually renders inconsistently across browsers, so if you see this behavior, inspect your HTML. Repeat step 3, and make sure your HTML validates.

6. Man-handle forms.

Everyone hates forms. If you don't, you haven't done enough multi-page checkouts yet. Bulletin boards everywhere are crawling with people having form trouble, and it's almost always related to bad-HTML or a misnamed field.

  • Dissect the form elements.
    Break down the form into small chunks until you find the problem. If you're using the POST method, does the page even submit? How about with one variable? Same for the GET method, can you pass at least one variable and have it show in the URL of the target page?
  • Name fields sensibly.
    If the form fields correspond to table columns in a database, use the same names for both, or adhere to some naming convention. Always build and debug server-side validation before introducing any JavaScript validation.

7. Write clean queries.

An extremely typical data-driven website scenario: You write a query in whatever, and then try to output the results. Sounds fine, but how often do you actually test the query output in its entirety? A good cabinetmaker measures twice, and cuts once. Most programmers measure a little, and hack it with an axe until it fits.

  • Isolate the SQL.
    Cut and paste your SQL into its own template and run it there. Can you output anything? Write a function in your favorite server-side scripting language that auto-dumps complete result sets for you, so you can test them before you try to output them on your pages.
  • Declare all columns.
    Never write SELECT * FROM table, name your fields explicitly. Even if you need every column in the table, naming them will help your templates become self-documenting.
  • Run it at the source.
    Websites usually connect to databases through ODBC or some other method. At the first sign of trouble, run the query in its native program if possible.

8. Organize your variables.

Keep your local variables in a structure or array. If you store all your local variables in one object, it is much easier to dump their values onto the screen as a whole, which is an invaluable tool when debugging.

  • Keep your ducks in a row.
    Every server-side language has a method for outputting complex datatypes. Store your local variables in an array or structure so you can dump them onto the screen. You might create a special footer, triggered by a URL variable, which dumps your local variables. Never put this sort of code on your live server.
  • Avoid reserved words.
    RTFM and find out what the reserved words for your favorite languages are. Errors caused by reserved word misuse are difficult to diagnose, prevention is the best medicine.

9. Proceed slowly, preview often.

When you first started coding, you probably previewed every &lt;H1&gt;, and scrutinized every &lt;img .../&gt;. Typically, the more experienced programmers become the more code they'll write before previewing.

  • Go slow.
    Try to find a comfortable amount of coding before previewing, so that you have a manageable chunk to work through when there's a problem. In my checkered past, I have painted houses for money. I once worked with a guy that seemed to move in slow motion, but at day's end he had always done the most work.
  • Work methodically.
    Whether you like to bring all elements of a site to the same point simultaneously, or like to finish each section as you go, decide what you're going to do and stick to it. Do the hard parts first, and save the easy stuff for when you're sick of the project.

10. Mirror your test and production setups.

If you've ever uploaded files to the server with a local path unchanged, take off your left shoe and eat it. The feeling is right up there with sitting in the Principal's office, or locking your keys in your car.

  • Mirror the server setup.
    Though not always possible, the advantages of having the same physical drive and folder setup as your production box are huge, and can save you hours of heartache and sweat drenched ftp sessions.
  • Use absolute paths.
    Consider using absolute paths as much as possible. Avoid using the &lt;base ...&gt; tag, as it will inevitably lead to disaster. A long long long time ago, I uploaded a site with a &lt;base ...&gt; tag set to 127.0.0.1. Can you guess what happened? Everything seemed fine when I tested the uploads, because the live site was referencing my local machine. Whatever you do, don't mix absolute and relative paths—choose one and avoid the other.

11. Work like a debugger.

Make your work environment work for you. I like to hog as many machines at once as possible, coding on one and previewing on the others.

  • Keep your reference books handy.
  • Make sure you have video games and sugar water within reach.
  • Tell noisy people to shut their pie-holes.
    Nothing clears a room like great music. If Sun Ra doesn't drive them out, I highly recommend Charles Ives' Fourth Symphony.
  • Become an alt-tab, or alt-arrow ninja.
    Windows does a nice job of keeping track of your recent selections, so tabbing between your editor and browser(s) is a quickly learned skill. Soon you'll be tapping: alt-tab, ctrl-r, alt-tab, rinse and repeat. And if you're GUI-ridden, use your keyboard to navigate drop-downs in your programs, it's faster than playing with your mouse.

12. Don't waste time, take a break, ask for help!

Don't spend more than 5 minutes trying to fix something (unless you've really done it this time).

  • Take a break.
    Work on something else for a while, work on nothing for a while, beat your head in with a hammer, and come back to the problem later.
  • Don't go it alone.
    Sometimes a fresh set of eyes can pick up a missing semi-colon faster, so don't be afraid to ask for help when you get jammed. If you work alone, find a mailing list or bulletin board you like.

Flex Those Debuggers

Hopefully, this article has gotten you to think about how you debug, which may in turn get you thinking about how you construct your web applications. As programmers, we feed our bottomless curiosities with small victories, and fuel our fires with the burning cinders of lift-off catastrophes. So get out there, write a whole heap of errors, and be proud when you bomb the whole server.

When Morgan was a boy, his Grandfather gave him a Sinclaire 1000, and the trouble hasn't stopped since. Morgan also enjoys playing the piano, spending time with his 3 children and 1 wife, and late night sessions with his Wii.

Developing websites since 1995, Morgan is Director of Online Operations for CMPMedica US.

NIce!

Submitted by Fingland on September 27, 2002 - 22:11.

As this is often overlooked but really f*n annoying problem in the day to day life of a programmer I think its excellent that you have taken the time to write this article and all programmers (not just web ones) should pay attention... I know if I had some better habits I probably wouldn't be tearing my hair out... :)

login or register to post comments

Opera & debugging HTML

Submitted by sasha22 on September 28, 2002 - 11:59.

For those unfamiliar with Opera, it has some rather handy tools for debugging HTML. One of the right click menu options within Opera is to upload the cached source of the document you're viewing directly to the w3c's HTML validator. Very useful, especially if your code hasn't been uploaded to a "live" server yet. It also has the ability to turn off CSS, Java, JavaScript, and images with just a few simple clicks.

login or register to post comments

Navigator JavaScript errors

Submitted by Martin Tsachev on September 29, 2002 - 03:18.

Navigator has always had the best JavaScript messages. Too bad most people still use version 4.

login or register to post comments

Re: Navigator JavaScript errors

Submitted by Jeff Howden on September 29, 2002 - 18:07.

Shaggy,

NN4.x and up having the best error messages is a matter of opinion. I've found that I can now debug scripts much quicker in IE because the error messages, while somewhat cryptic to inexperienced JavaScript coders, accurately identify the cause of the problem and where to find it. Combine this more precise wording with the script debugger from Microsoft and NN4.x and up can't touch it.

Beginner coders will find NN4.x and up easier at first because it gives them the line and character numbers along with the snippet of code with the problem and a carat pointing to the exact spot where the error is. However, the debugging won't really be a learning process beyond matching up line and character numbers from the debug window. They won't be learning things like "Object expected" means that they're trying to reference an object or function that doesn't exist, "Expected ')'" and "Expected ';'" means they didn't properly escape a string qualifier embedded in a variable value or argument being passed to a function, etc.

In short, easier doesn't always equate to better.

First, download and install the debugger.

Second, read this tutorial from DevGuru.com.

.jeff

login or register to post comments

good stuff

Submitted by bustamelon on September 30, 2002 - 10:02.

9 times out of 10 when I have problems writing code, it's the debugging part that holds me up. I tend to throw reason to the wind and get all flustered, using a random haphazard approach rather than a pragmatic and systematic one. And I think many programmers are the same way, especially if they learned on their own, or in a less than formal environment. This is advice that all programmers can take to the bank. I recommend everyone to keep this article on hand at all times, in between the coffee cup and the No-Doz bottle.

login or register to post comments

Re[2]: Navigator JavaScript errors

Submitted by Martin Tsachev on September 30, 2002 - 12:42.

Think MS have a version for Linux?

I've never used their debugger so I can't really say if it's good or bad - all I know is that it is easier with Navigator.

PS. I've come to the point when I don't get errors so often that I need a JavaScript debugger although one for PHP would be good.

login or register to post comments

php debugger

Submitted by nagrom on September 30, 2002 - 13:03.

shaggy, there is this
haven't tried it myself.

login or register to post comments

Re: PHP debugger

Submitted by Martin Tsachev on September 30, 2002 - 13:11.

Thanks for that, I'm downloading it.

login or register to post comments

Re[3]: Navigator JavaScript errors

Submitted by Jeff Howden on September 30, 2002 - 13:20.

Shaggy,

Considering Microsoft no longer offers IE for *nix and they don't make it a habit of developing much of anything for *nix, I think it's safe to say there isn't a version available for *nix.

The burning question though is "So?". When I'm developing a website, web-based application, etc., I want to build it in the environment that the majority of my users are using. That almost always means Win/IE. In situations where I have to test it on other platforms, I make it a point to use the browsers that are in use the most. In those situations I make do with whatever error reporting capabilities those browsers provide. In the end though, I much prefer the Win/IE/Script Debugger route.

.jeff

login or register to post comments

Re[4]: Navigator JavaScript errors

Submitted by Martin Tsachev on September 30, 2002 - 13:35.

Jeff, the "do MS have it for Linux" was a joke.

I don't like doing anything browser specific in JavaScript so what works for me in Opera, Konqueror, and Galeon on Linux will most likely work in IE on Windows too.

login or register to post comments

Re[5]: Navigator JavaScript errors

Submitted by Jeff Howden on September 30, 2002 - 14:18.

Shaggy,

I know it was a joke, but there was some truth that needed mentioning -- use the tools your target audience is using.

As far as *nix specific browsers go -- I wouldn't waste my time unless they represented a significant portion of my audience. I'm not saying that in a publicly accessible site I'd use browser specific code (at least not without appropriate object/method support detection), but I'm certainly not going to waste my time hunting down and squashing bugs on perfectly good code that insists on breaking on esoteric, immature browsers -- especially at the expense of possibly increasing page weight or decreasing usability for the majority of my audience for the sake of the few, the proud, the broken when those browsers can simply degrade just fine to the non-JavaScript experience already developed.

So, I applaud you for your efforts with Opera, Konqueror, and Galeon on *nix, but fail to see the point of it really.

.jeff

login or register to post comments

Re[6]: Navigator JavaScript errors

Submitted by Martin Tsachev on September 30, 2002 - 14:43.

Jeff, I suppose you've misunderstood what I meant. I don't test everything on these browsers - I just use Opera and check in the others from time to time. Nothing wrong has happened to date so I suppose it's OK. I don't need to debug code that has no bugs and is working fine.

login or register to post comments

Netscape JS debugger much better

Submitted by ghurtado on October 2, 2002 - 17:36.

The reason is simple: if you are using return values and object references, IE makes it much harder to spot the error, because it only gives the highest level element in the script that returned the error, where NS will tell you exactly where the bug is, deep down in your code.

To give a simple example, if you have a form and the submit button has something like
ūcode˙ &lt;input type="submit">
and validateMe fails, IE will return the error line as the one with the onclick event, rather than whatever part of the validateMe function that may be at fault.

After many years of complex JS debugging in both browsers, this is something that always drove me crazy about IE, its kinda like saying "Javascript Error in file index.html", not exactly specific and helpful.

login or register to post comments

Great article

Submitted by trfc791 on October 4, 2002 - 06:57.

I must say this is a great article. However, I believe the debugging part comes with experience. I remember how, when I first laid out my website, it had five pages with the same layout, and everytime I needed to change the navigation bar I needed to go through all five pages and if you made a mistake, you had to go back to a page laid out correctly and copy the code all over again. God help you if you missed out one character in the layout. As time went on, more and more pages were added; more and more mistakes were made; more and more debugging was needed.

Since then, I've learnt PHP and ASP, I've learned to use includes. Life has been much easier. But that's not the only stuff you learn. Server side languages give you a great foundation for debugging, especially if the only server you can use is a remote server that you must FTP to, like me. Code, upload, view, error, remove section, upload, view, error, remove more sections until problem is isolated, debug, upload, view... etc.

I would recommend that beginners don't sweat over debugging so much as most of these tips mentioned will develop into habits as you accumulate experience. However, if you're looking to develop your debugging habits early, I say this is a really good article to read. And don't fret if you don't seem to be debugging a lot. Less debugging needed, cleaner code.

login or register to post comments

&lt;base&gt; and javascript debugging

Submitted by lpb1 on October 9, 2002 - 16:21.

  1. re: <base>: i think you give it a bad rap unfairly. if you choose to use <base>, it is worth it to make the include file it's in read-only by your development webserver, such that you won't automatically copy the wrong tag when updating the site. doing so will require you to make some changes by hand, but i guarantee you that it it's worth it.
  2. if you're looking for a good javascript debugger, across the board, i'd recommend zenkman, a javascript debugger (a la gdb for other languages) available in mozilla and netscape 7. for more a good tutorial, check out ian oeschger and robert ginda's article at netscape devedge, venkman, the new javascript debugger for netscape 7.

login or register to post comments

To base or not to base

Submitted by nagrom on October 10, 2002 - 19:23.

re: <base>:

I tried to keep the language mild on the base tag section, I did say "consider". ;-)

I maintain sites (that I wrote originally) across the board on this issue: some have no base and are all relative, some use the base tag, and some are absolute. Hands down, the easiest to maintain are the absolute-path sites. I actually have a site that does what you suggest, it has the base in an include, which is read-only....but it started to bother me. Do I really need the overhead of an include for something like setting a base tag? Do I need the added debug overhead? It is afterall, one more thing that can go wrong.

In some situations, of course, it is unavoidable. If you're working on an intranet, you probably can't live without it. But for most websites, I still think absolute paths have the least pitfalls, and are easiest to maintain.

Thanks for the JavaScript debugger link, too. I was hoping people would add their favorite tools.

I've been using this alot lately: WebTools
Lets you view all the images, form elements, parsed CSS, tables, etc on a page. IE only, though.

[nagrom hits view source]
heh, evolt has a base tag...i expect a full explanation, followed by a promise of reformation, from the guilty party(s)
[/nagrom closes source]

login or register to post comments

performance base

Submitted by mwarden on November 2, 2002 - 23:28.

nagrom wrote:
"Keep your local variables in a structure or array."

Would that not impose a hit on performance? If, for every access of a variable, i have to access it through an index of an array (e.g. locals[8] ), or (even worse) a content-/hash- based array (e.g. locals['siteID'] ), would that not add some stress in the name of debugging? Normally, with things like this, I'd be all for it, noting that you could simply "turn off" the debugging helper once the script is throroughly tested. However, that's not too easy to do here (other than for the dump itself), without (maybe) a rather complex regexp.

For certain languages, where variables in a certain scope are aggregated into a structure of some sort automagically by the [whatever name you prefer for the execution software], this seems like a nice debugging help.

It doesn't seem like the benefit outweighs the performance hit (which will persist much beyond the debugging phase - indefinitely, in fact) for languages that do not already do this automatically.

Thoughts?

login or register to post comments

RE: performance base

Submitted by nagrom on November 4, 2002 - 11:26.

Hmmm, hadn't thought about that, Matt.

The languages I'm most experienced with are CF and PHP on the server-side, and javascript on the client side. I haven't seen a performance hit when using complex datatypes for those languages. I know you have a lot of ASP experience, that may be the case there.

Still though, I'd be surprised if there's a big performance decrease. Maybe if the local array is *huge*, but I usually need less than 10 variables for a page. If performance does degrade with array size, I would break them up into smaller, more function-specific arrays.

login or register to post comments

Large scale?

Submitted by trfc791 on November 10, 2002 - 08:06.

Suppose you were building a large scale web app (say 75 pages) and every page's output is based on the variables from the other pages, how would you "proceed slowly"?

login or register to post comments

Well, article is too client side-oriented

Submitted by Zlatev on May 14, 2003 - 23:39.

Nice Article, but for server-side development I preffer "Unit testing" develoment:
Kind Regards,
Zlatin Zlatev

login or register to post comments

Great article!

Submitted by Putte_5 on May 23, 2003 - 03:48.

Very nice article, learned alot from this.

login or register to post comments

Thanks

Submitted by matte99 on May 23, 2003 - 04:40.

Thanks for the info, i found it really useful.

login or register to post comments

Other debugger article

Submitted by Akela on August 21, 2006 - 18:44.

Nice Javascript debuggers review can be find here: http://www.ajaxpath.com/ajax-debuggers/

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.