Subsections


2.6 Running code

We have now discussed how to write code and how to check that the syntax of the code is correct. The final step is to run the code and have the computer produce the result that we want.

As with syntax checking, we need to use software to run the code that we have written.

In the case of HTML, there are many software programs that will run the code, but the most common type is a web browser, such as Internet Explorer or Firefox.

2.6.1 Running HTML code

All that we need to do to run our HTML code is to open the file containing our code with a web browser. The file does not have to be on another computer on the internet. In most browsers, there is a File menu with an option to open an HTML file on the local computer. We can then see whether the code has produced the result that we want.

Web browsers tend to be very lenient with HTML syntax. If there is a syntax error in HTML code, most web browsers try to figure out what the code should do (rather than reporting an error). Unfortunately, this can lead to problems where two different web browsers will produce different results from exactly the same code.

Another problem arises because most web browsers do not completely implement the HTML standards. This means that some HTML code will not run correctly on some browsers.

The solution to these problems, for this book, has two parts: we will not use a browser to check our HTML syntax (we will use HTML Tidy instead; see Section 2.5), and we will use a single browser (Firefox2.5) to define what a piece of HTML code should do. Furthermore, because we will only be using a simple subset of the HTML language, the chance of encountering ambiguous behavior is small.

If we run HTML code and the result is what we want, we are finished. However, more often than not, the result is not what we want. The next section looks at how to resolve problems in our code.


2.6.2 Debugging code

When we have code that has correct syntax and runs, but does not behave correctly, we say that there is a bug in our code. The process of fixing our code so that it does what we want it to is called debugging the code.

It is often the case that debugging code takes much longer than writing the code in the first place, so it is an important skill to acquire.

The source of common bugs varies enormously with different computer languages, but there are some common steps we should take when fixing any sort of code:

Do not blame the computer:
 
There are two possible sources of problems: our code is wrong or the computer (or software used to run our code) is wrong. It will almost always be the case that our code is wrong. If we are completely convinced that our code is correct and the computer is wrong, we should go home, have a sleep, and come back the next day. The problem in our code will usually then become apparent.
Recheck the syntax:
 
Whenever a change is made to the code, check the syntax again before trying to run the code again. If the syntax is wrong, there is no hope that the code will run correctly.
Develop code incrementally:
 
Do not try to write an entire web page at once. Write a small piece of code and get that to work, then add more code and get that to work. When things stop working, it will be obvious which bit of code is broken.
Do one thing at a time:
 
Do not make more than one change to the code at a time. If several changes are made, even if the problem is cured, it will be difficult to determine which change made the difference. A common problem is introducing new problems as part of a fix for an old problem. Make one change to the code and see if that corrects the behavior. If it does not, then revert that change before trying something else.
Read the documentation:
 
For all of the computer languages that we will deal with in this book, there are official documents plus many tutorials and online forums that contain information and examples for writing code. Find them and read them.
Ask for help:
 
In addition to the copious manuals and tutorials on the web, there are many forums for asking questions about computer languages. The friendliness of theses forums varies and it is important to read the documentation before taking this step.

Chapter 3 provides some basic information about common HTML elements and Section 3.3 provides some good starting points for detailed documentation about HTML.

We will discuss specific debugging tools for some languages as we meet them.

Recap

Computer code should be tested to ensure that it not only works but also that it produces the right result.

If code does not produce the right result, the code should be modified in small, disciplined, and deliberate stages to approach the right result.

Paul Murrell

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 New Zealand License.