Last Modified: 1/29/08
  Computer Training
Programming with JavaScript


Credit: J. Trauger (JPL/NASA)

Programming Style

In most cases, you should write programs which emphasize clarity of structure and ease of comprehension, as if you were writing to another person rather than to a machine. Since you often have to maintain and alter your own code, that "other person" will be you 6 months in the future. The points that I strive for include the following:
  • Careful delineation of the start and end of functions
  • Documentation of each significant variable
  • Sparse documentation within the body of a function (usually on the right side of expressions) so as not to interfere with understanding the flow of control
  • Formatting output (especially HTML and JavaScript output) so it closely corresponds with the actual appearance of the output
  • Alignment of expressions so that differences between similar expressions can easily be spotted
  • Using understandable code versus compact code.
  • Using upper/lower case effectively. I stick with the convention that Classes begin with an uppercase letter. Uppercase letters are used in the middle of variable names to emphasize components, e.g., "upperRight" and "upperLeft".
See this for examples of the above style guidelines.

Debugging JavaScript

Click this for an example of a typical JavaScript error message (in order to see error messages, check to make sure that the option [x] Display a notification about every script error is turned on in Tools > Internet Options > Advanced in Internet Explorer).

  • Most error messages display the line number of the offending statement so you need a viewer or editor which displays line numbers. A very simple utility in Unix, named nl, creates a copy of a file with line numbers, e.g.,
         homer% nl < myjavascript.html > temp
         
    where "myjavascript.html" is your JavaScript program, creates a copy of your program with line numbers in the file "temp" which you can view with any editor. There is one aspect of nl that you need to be wary of: if a line is truly empty it is not counted, so if you include blank lines in your script, make sure that you type at least one blank character in the line before terminating it with an <Enter> or <Return >.

    Note: there is also a (rather complicated) way to add line numbers using Microsoft WORD: in the help menu type "Line numbers" and it will show you how.

  • You frequently need to write debugging files in either plain text or HTML. Examples of this appear below.

  • There are also several techniques you can practice that both reduce the probability of errors and also help you locate them:
    • Miniaturization: people often code large programs, or fling huge data sets at a new program the first time and find themselves flooded with error messages. It is almost always wise to miniaturize either the program or data or both so as to locate the error in its simplest form
    • Binary search: if you experience difficult errors, try dividing either the program or the data set in half. When you find the erroneous half, divide that in half, and so forth.
    • Individual modules: the most certain way to control errors, and to schedule your work load to be free of unpleasant surprises, is to debug each module at a time. This requires writing a "test harness" for each module, which takes time, but is usually well worth the effort

Examples

Here we have a large number of examples which illustrate the concepts introduced so far. Each example consists of its source code followed by its actual execution.
Previous Home Next

Course Topics

Major Concepts in JavaScript

Execution of JavaScript Programs

JavaScript Statements and Structures

JavaScript as an Object Oriented Language

The Document Object Model

Programming with JavaScript

JavaScript and DHTML

Using Cookies in JavaScript

Using JavaScript to Write HTML

 
Previous Home Next

References:   References: "JavaScript--the Definitive Guide" by David Flannagan, O'Reilly & Assoc   

Online Tutorials:Advanced JavaScript Tutorial

©1999 UW Technology