Instructional Module X11d

Coding XHTML for Easy Debugging and Maintenance


to Top Why Bother?

Why bother setting up your code for easy debugging and maintenance? There are several reasons why people don't bother, such as, "I'm almost done anyway," or "It's just a short, little page," or "I know I won't have any problems with this page - it's so simple," or "This page is just temporary (or Just for a class) so nobody will care if it's not easy to maintain."

The fact is, making code easy to debug and maintain does require extra effort. So the only reason to do it would be if it repays your effort. The bottom line is, it does repay the effort. Here's why:

  • Computer code can get very complex quickly. Every step we take to reduce complexity is worth the effort.
  • Small coding errors can be very hard to find and cause big delays. Every step we take to make errors easier to detect is worth the effort.
  • Temporary pages can serve as the basis for more permanent pages - even if the origin is a class assignment. But that's only likely if the code is clear and maintainable to begin with.
  • You may think you'll never see the code for this page again, but don't be too sure. The maintenance worker you save may be yourself.

Well, I don't know if that will convince you, but if I don't, experience will. Here are some practical tactics you can use make life easier for yourself.


to Top Coding Tactics
Line Breaks

to Top
Link to Top

Putting all your code in one solid mass, without whitespace between sections, can make it difficult to identify important sections quickly and easily. Compare these two examples:

Poor
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello!</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>Hello, World </p>
</body>
</html>
Better
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Hello!</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<p>Hello, World </p>
</body>
</html>

The simple addition of blank lines separating the major sections (the Prolog, the Head, and the Body) make it much easier to find relevant parts of the code.

think about it...
  • What are some other parts of code you could separate to make things clearer?
  • Are blank lines better for separating parts of a file, or are lines of characters, such as
    <!-- ----------------------------------------- -->
    more effective? Why?

Do it...
  • Edit your template file (from module X10i).
  • Separate the Prolog, the Head, and the Body by putting in blank lines or rows of characters.

Another way in which line breaks can be used is to line up structures that are similar.

Poor
<p>We can create millions of colors on Web pages, the most popular of which are <span style="color: red;">red</span>, <span style="color: yellow;">yellow</span>, <span style="color: green;">green</span>, and <span style="color: blue;">blue</span>. </p>
Better
<p>We can create millions of colors on Web pages, the most popular of which are
<span style="color: red;    ">red</span>,
<span style="color: yellow;">yellow</span>,
<span style="color: green; ">green</span>,
and
<span style="color: blue;   ">blue</span>. </p>
Browser Display (of both)

We can create millions of colors on Web pages, the most popular of which are red, yellow, green, and blue.


think about it...
  • What makes the second arrangement of code better than the first?
  • Are there other coding situations you've worked with where lining similar items up helps? If so, what are they?

Do it...
  • Create a new XHTML file using your template file.
  • Paste this code into it, and line the code up for clarity:

<p> <span style="font-size: 6px;">Example text, 6 pixel height</span><br /> <span style="font-size: 6px;">Example text, 11 pixel height</span><br />
<span style="font-size: 6px;">Example text, 16 pixel height</span><br /> <span style="font-size: 6px;">Example text, 21 pixel height</span><br />
<span style="font-size: 6px;">Example text, 26 pixel height</span><br /> </p>

Indenting

to Top
Link to Top

Indenting lines of code is another way - perhaps the most useful way - of making your code clear. Here's the general idea:

  • When one (X)HTML block entity contains another, the one inside should be indented.
  • When an entity has more than two or three lines of code, line up the start and end tags so there is a clear line of sight between them. This is done by indenting the lines between the start and end tags.
Better
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Hello!</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<p>Hello, World </p>
</body>
</html>
Best
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
   <title>Hello!</title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
   <p>Hello, World </p>
</body>

</html>

Do it...
  • Edit your template file again.
  • Indent tags in the Head and tags in the Body, as shown above.

The benefit of indenting becomes more obvious as the HTML structures become more complex. Even if you're not yet familiar with more complex structures like tables and lists, you may be able to appreciate the clarity of the better code in the following example, which uses nested lists.

Key:

  • <ul> = Unordered List
  • <li> = List Item
Browser Display (of both)

Grocery List

  • Produce
    • Lettuce
    • Carrots
    • Asparagus
  • Dairy
    • Milk
    • Cheese
      • Mozzarella
      • Monterrey Jack
    • Eggs
Poor
<body><h1>Grocery List</h1>
<ul>
<li>Produce
<ul>
<li>Lettuce</li>
<li>Carrots</li>
<li>Asparagus</li>
</ul>
</li>
<li>Dairy
<ul>
<li>Milk</li>
<li>Cheese
<ul>
<li>Mozzarella</li>
<li>Monterey Jack</li>
</ul>
</li>
<li> Eggs</li>
</ul>
</li>
</ul>
<body>
Better
<h1>Grocery List</h1>
<ul>
<li>Produce
<ul>
<li>Lettuce</li>
<li>Carrots</li>
<li>Asparagus</li>
</ul>
</li>
<li>Dairy
<ul>
<li>Milk</li>
<li>Cheese
<ul>
<li>Mozzarella</li>
<li>Monterey Jack</li>
</ul>
</li>
<li> Eggs</li>
</ul>
</li>
</ul>

think about it...
  • Is the second arrangement really easier to read than the first? If so, why?
  • Looking at the example above, how would you explain when to indent a line of code?
  • How would you explain when to un-indent a line of code?

General rules for indenting HTML code:
  1. When two starting tags occur with no ending tag between them, indent the second tag.
  2. When two ending tags occur with no starting tag between them, out-dent the second tag.
  3. A self-closing tag is neither indented not out-dented.
  4. All other tags should line up with one another.

Comments

to Top
Link to Top

Comments can be used to guide you to the part of the code you need to work with. They're especially important in (X)HTML when you're working with long sections of repetitious code, such as large tables and lists. Comments are critical when using tables for layout (see module W24e).

A comment in code is a statement that is visible to coders, but "invisible" to the software that runs the code. In (X)HTML, comments are made "invisible" by putting them in these tags:

<!-- -->

You need a space between the markers and your actual comments:

Wrong Right
<!--Main Section--> <!-- Main Section -->

 

This example improves on the previous one by adding comments:

Better
<h1>Grocery List</h1>
<ul>
<li>Produce
<ul>
<li>Lettuce</li>
<li>Carrots</li>
<li>Asparagus</li>
</ul>
</li>
<li>Dairy
<ul>
<li>Milk</li>
<li>Cheese
<ul>
<li>Mozzarella</li>
<li>Monterey Jack</li>
</ul>
</li>
<li> Eggs</li>
</ul>
</li>
</ul>
Best
<h1>Grocery List</h1>
<ul> <!-- Begin main list -->
<li>Produce
<ul> <!-- Begin produce list -->
<li>Lettuce</li>
<li>Carrots</li>
<li>Asparagus</li>
</ul> <!-- End produce list -->
</li>
<li>Dairy
<ul> <!-- Begin dairy list -->
<li>Milk</li>
<li>Cheese
<ul> <!-- Begin cheese list -->
<li>Mozzarella</li>
<li>Monterey Jack</li>
</ul> <!-- End cheese list -->
</li>
<li> Eggs</li>
</ul> <!-- End dairy list -->
</li>
</ul> <!-- End main list -->
Pointers for comments
  • Place comments so they don't interfere with the indentation.
  • Commenting the end of an entity is just as important as commenting the beginning - particularly if it helps you see what the end-tag is the end of.
  • Don't comment obvious things - that is, where the code is already clear (like the beginning and end of the Head and Body sections).
Copy-and-Paste

to Top
Link to Top

When you're entering code by hand in a text editor, let the computer do the repetition by using copy-and-paste rather than re-typing. This is "working smart" for several reasons:

  • The obvious reason: it's quicker than re-typing the same thing over and over;
  • It cuts down on errors - both the normal ones, and the ones caused by boredom;
  • It makes it easier to be consistent. Consistency makes it easier to spot errors, and easier to correct them.
Re-use Existing Code

to Top
Link to Top

Software development in general is slow because so much software is written "from scratch" - starting from the beginning.

One of the best things you can do is to create a template for your work. In addition to the basic XHTML template described in module X10i, you can create specialized templates for particular kinds of pages. When you do a large site, template files are especially important to help achieve site-wide consistency.

Of course, this means use your own work - not that of others. It's not an invitation for cheating and copyright violation!



to Top About This Document
Review Button

Click here for review questions.

Audience

to Top
Link to Top

This module is for people who are familiar with the basics of XHTML and would like to know how best to write their code to make it easy to use.

Objectives

On successful completion of this module, you will be able to:

  1. Utilize comments to identify different parts of your document
  2. Format your code for easier troubleshooting and reading
Link to Top
Module X11d: Coding XHTML for Easy Debugging and Maintenance
This document is part of a modular instruction series in Computer Instruction. For more information, see the overview or the list of modules in this series, X: XML, etc. This document has been used in the following classes: INP 150.
History
Original: 10 September 2003, by Laurence J. Krieg
Last modification: Monday, 31-Aug-2009 11:48:07 EDT
Copyright
Copyright © 2003, Laurence J. Krieg, Washtenaw Community College
Instructors: You may point to this file in your Web-based materials; however, its location may change without notice.
Students: You are welcome to make a copy for your personal use.
All other uses: Please contact the author, Laurence J. Krieg, for permission: krieg@ieee.org.

Link to Top