HTML and CSS Reference
In-Depth Information
Written Communication: Writing Better JavaScript Code
In working environments, the maintenance of a program or script is often shared among
several individuals. The program you write today might be the responsibility of one of your
colleagues next month. Thus, an important goal in writing program code is to make it intel-
ligible to other users so that they can easily maintain and update it. Here are some tips to
help you write better JavaScript code:
• Use consistent variable names : One common source of error is misnamed variables.
You can avoid this problem by being consistent in the use of upper- and lowercase letters
in your variable names.
• Make the code easier to read with whitespace : Crowded commands and statements
are difficult to read and edit. Use whitespace and indented text generously to make your
code more legible to others.
• Keep your lines compact : Long text strings can wrap to new lines in your text editor,
making the text difficult to read. Strive to keep your lines to 80 characters or less. When
a statement doesn't fit on a single line, break it to a new line at a point that maximizes
readability.
• Comment your work : Like HTML and CSS, JavaScript supports the use of comments.
Always comment your work, documenting the purpose of each variable.
• Declare all of your variables : Though JavaScript is a weakly-typed language that will
automatically create your variables as soon as they are used in a statement, you should
still explicitly declare all of your variables at the top of your program.
As your scripts become longer and more complicated you can also simplify your code
through the use of functions and external JavaScript files. You'll explore these concepts in
more detail in the next session.
You've entered the initial code for the JavaScript program and learned how to work
with objects, methods, and variables. In the next session you'll complete your program
by working with JavaScript functions and files. You'll also learn how to use browser tools
to locate and fix errors that may appear in your code.
Search WWH ::




Custom Search