JMU JMU - Department of Computer Science
Help Tools
ECMAScript/JavaScript Style Guide


1 Local Style Guide

You must follow certain guidelines when writing ECMAScript/JavaScript code.
  1. Class names must start with an uppercase letter.

    In addition, each "word" within a class name should start with an uppercase letter. For example, TextMessage and SimpleTrafficMonitor are both appropriate class names.

  2. The names of "constants" must be in all uppercase.

    In addition, there should be an underscore between each "word". For example, PAGE_LENGTH.

  3. Other variable and method/function names that contain multiple characters must not start with an uppercase letter.

    Further, each "word" within a variable name should start with an uppercase letter. For example, importantMessage and campusMonitor are both appropriate variable names.

  4. Variable names that consist of a single character may be uppercase.

    In general, even single-character variable names should be lowercase. However, in some situations, mathematical notation uses uppercase letters. In such situations, uppercase variable names may be used. For example, matrices are often written using uppercase letters. So, an expression like (b = A*x) would be appropriate.

  5. Variable and method/function names must be descriptive.

    Variable names like aaa are not appropriate. Index variables and counters can, however, have names like i and j.

  6. All variables must be declared using var .

    Since ECMAScript/JavaScript does not have block scope, they should be declared at the top of the function/class.

  7. Statements must end in a semicolon.

    In other words, even where semicolons are optional (i.e., implicit), semicolons must be included explicitly.

  8. Avoid lines that are longer than 80 characters.

    In other words, even where semicolons are optional (i.e., implicit), semicolons must be included explicitly.

  9. Each class must be in its own file.

    The name of the file and the name of the class must coincide exactly. For example the Queue class should be in a file named Queue.js.

  10. Each function library must be cohesive and must be in its own file.

    The name of the file must be descriptive.

  11. Each class/function library must have a descriptive block comment.

    This comment should describe the complete class/function library (rather than the methods in the class or functions in the library).

  12. Each method/function must have a descriptive block comment.

    This comment should describe the methods, its parameters, and its return value.

  13. Block comments must use the JSDoc format.
  14. Comments in the body of a class should use // rather than /* ... */.

    This isn't a requirement but it will make your life easier since you can't nest comments inside of /* ... */. There is nothing more annoying then trying to "comment out" a section of code while you are debugging and being unable to because it contains block comments.

  15. In the block comment for each class/function library you must include a comment (or comments) containg the author(s) names. For example:
    	 *
    	 * @author David Bernstein
    	 *
             
  16. In the block comment for each class/function library you must include a comment attesting to your compliance with the JMU Honor Code as follows:
    	 *
    	 * This work complies with the JMU Honor Code.
    	 *
             
  17. Always start curly brackets on the same line as whatever they are opening.
             if (...) {
                 // ...
             } else {
                // ...
             }
             

    This will avoid problems with implicit semicolon insertion.

2 Other Style Guides

Different people have different opinions about what is "stylish". If you are interested in such things (and you should be) you might want to take a look at:

3 Style Humor

As mentioned above, different people have different opinions about what is "stylish". This leads to many fights (and some jokes).

../lectures/comics/Hackles-Style.png
(Courtesy of Hackles)

http://imgs.xkcd.com/comics/code_quality.png
(Courtesy of xkcd)

http://imgs.xkcd.com/comics/code_quality_2.png
(Courtesy of xkcd)

Copyright 2019