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


Credit: J. Trauger (JPL/NASA)

Programming Style

One of the most important concepts in programming is the separation of complexities. Two complex structures that interact closely with one another create a complexity that is often far more than twice as complex as either, because of their interaction. By identifying, separating, and isolating those elements we can often achieve a much simpler and far more maintainable structure. An excellent example is that of 3D computer graphics where the underlying geometry of a structure is isolated from its surface properties which are defined as geometry-free texture, bump, transparency maps, etc. that can be applied to the geometry.

For Web programming, it is very useful to separate a web page into these elements:

  • CSS style sheets that control the appearance of most of the HTML elements, thus limiting most of the HTML to content and structure as opposed to color, size, or other aspects of appearance. CSS style sheets can reside in external files or in the <head> part of an HTML document.
  • JavaScript functions that control the dynamic aspects of the page and are usually stored in separate external files or after the CSS sheets in the <head> of the page.
  • HTML elements that contain the text and structure information, such as paragraphs, tables and bullet lists.
This separation reduces complexity, adheres better to standards, and greatly increases flexibility as we can change the appearances by manipulating aspects of the CSS or JavaScript without affecting the HTML itself. The best example of this is XML which is wholly concerned with content, is completely free of appearance controls, and so the information can be converted to a very wide variety of formats.

Another aspect of style is adherence to xHTML standards. xHTML, among other things, requires strict adherence to a perfect tree structure which allows JavaScript to manipulate any structures in an HTML document (see this xHTML tutorial for more information on xHTML).

JavaScript 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

These examples illustrate the concepts previously introduce -- click view > source to see the source code.
Previous Home Next

Course Topics

Client-Side JavaScript

JavaScript as a Programming Language

Execution of JavaScript

'JavaScript Document Object Model

Programming with JavaScript

Using Cookies in JavaScript

JavaScript and DHTML

Homework Assignments

 
Previous Home Next

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

©1999 UW Technology