Writing Bug-Free Code

I l @ ve RuBoard

A bug is an error or other problem with the program that you have written. Sometimes, bugs are obvious things that you are warned about while writing the program. Flash warns you about syntax errors and things that it doesn't understand.


The term bug came about because the first computer bug was actually a real bug. A moth got caught in an early computer, attracted by the light of the vacuum tubes inside. The term bug has been used to describe computer problems ever since.

Other bugs don't show up until you run the program. These are problems that look okay to Flash but make the program perform in a way other than what you wanted.

Simple Solution

The easiest way to write a bug-free program is not to put the bugs in there in the first place. This sounds a little ridiculous, but it makes sense.

Take time planning and writing your code. Check and double-check variable and function names . Think about how each line of the program will work as you write it.

One way to make it easier to do this is to write your program one piece at a time. After you have completed each piece, test it out before going any further.

For instance, if you are making a space invaders game, try just making the player's ship react to the left arrow key first. Set up everything you need in your movie to make this one thing happen. Then, test it. If you keep working with just small steps like this, you'll avoid having to make a lot of bug fixes when you are finished.

The Hard Truth About Bugs

The hard truth is that it is nearly impossible to write a program that doesn't have any bugs at first. Even the best programmers who have been writing programs all their lives can't do it.

So don't try to be a perfectionist as you write your code. You will have to spend some time testing and fixing bugs anyway at the end.

This is a tough point for some people who are first learning to program. It seems that programming is something that has a certain amount of precision and should be easy to get right the first time. But it is more like an art, you work at a program, making adjustments and changes, until it runs the way you want it to.


Dealing with bugs this way seems contradictory. On the one hand, take the time to write bug-free code, but, on the other hand, don't waste time trying to be a perfectionist. Find a balance between these that will optimize your time and effort.

Preparing for Bugs

You can do many things to prepare for fixing bugs later. The primary one is to comment your code. Most programming languages allow you to add comments to your code, and ActionScript is no exception. You can designate one or more lines, or even part of a line, to a comment. A comment can be anything you want; it is ignored by Flash.

The idea behind commenting your code is to leave hints for yourself so that you can return to the code later and remember what it does. It also helps if someone else will be using your code later.

At times, it may seem like overkill to comment code. After all, when you write a section of code it is obvious to you what the code is doing. But consider that some bugs may not pop up for weeks or months. You may be several projects down the road when you find that you need to open up an old Flash file and make changes. Comments will be a huge help at that point.

In addition to comments, you can also make your code easier to read by using descriptive variable names and function names. Three months after writing the program, you may not remember what s was supposed to stand for, but gameScore will still make sense.


So, how do you get rid of bugs? This is a process called debugging.

Sometimes debugging is straightforward. You simply see what the program did wrong, think about what could have caused it, go into the program, and make a change.

However, it is more often the case that you will need to investigate a little to determine what is wrong. Sometimes you need to make a change, test the program, and make some more changes. A single bug can sometimes take hours to track down.

Flash includes some debugging tools that will help you fix your programs. You can track variable values and even set breakpoints in your code. We'll look at some of these tools in Hour 4.

I l @ ve RuBoard

Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net