CIT 041J Index > Programming Standards

CIT041J–JavaScript Programming Standards

  1. For all of our class programs, you will provide the following comments at the beginning of your files as a comment after the <!DOCTYPE>:
    <!--
      Your Name
      The course and Program Assignment Number
      The purpose of the program
    -->
    
    For example:
    <!--
      Fred J. Doakes
      CIT041J Assignment 1
      Get information from users and manipulate it
    -->
    
  2. We have installed Textpad on the lab computers to use to create and edit your JavaScript programs. You are NOT required to use Textpad; you may use any text editor you choose. However, Textpad “remembers” your level of indenting and also displays line numbers,which are quite useful when trying to decipher error messages. Textpad can be started with the textpad icon icon in the quick start tray (bottom left of the screen).
  3. You may have only ONE statement per line.
  4. Statements exceeding 80 characters must be broken into multiple lines. These continuation statements are indented one tab from the initial continued statement.
  5. Follow the indentation style as used in the text book.
  6. Use Tabs for indentation, not spaces.
  7. In the book, the beginning brace for a block of code is on the line directly below the statetment preceding it. You may also put the brace on the same line; thus either:
    if (n == 3) {
    or
    if (n == 3)
    {
  8. Data Declarations
    1. All declarations are at the top of the block of code they are used in. (For now, that means top of the function or top of the script itself.)
    2. Only ONE variable or constant declared per statement.
    3. Declare variables in the proper scope. Use global variables only when necessary.
  9. Names are descriptive.
    1. Names for variables and functions begin with a lower case letter.
    2. Names that contain more than one word use “intercaps” style to separate words, for example: quantityOnHand.
    3. Names for constants are in all capital letters.
    4. Names for constants that contain more than one word use an underscore to separate words, for example: DAYS_PER_WEEK.
    5. Variable names are generally nouns, for example: itemsSold.
    6. Boolean variable names “ask a question,” for example: isValid.
    7. Function names are generally verbs, for example: findUserName( ).
  10. Spacing
    1. Separate blocks of algorithms and blocks of code with ONE blank line.
    2. Separate binary operators from their operands with one blank space. Example: a = 2 + (b - 3) * 4;
    3. Do not separate unary operators from their operand with any blank spaces, but separate the pair from other expressions with a space. Example: a = b++ - ++c; (This is a good example of the spacing, but poor example of decent code. The result of this statement may not be what you think. Write some code to try and see if you are correct in predicting the final value of a.)
    4. Follow commas with a space.
  11. Literal Usage:
    1. You may use string literals.
    2. Numeric literals are not to be used except
      1. To initialize declared constants
      2. When they are self defining (2 to double or halve a value, 100 to convert from and to percentages, 0 or 1 to initialize counters or accumulators).
      3. When they are given in a mathematical equation.
  12. Input Prompts should be descriptive and informative.
  13. Comments are for programmers, not students, and should not be redundant to the code. (Our textbook contains, as it should, comments for the students.) Example:
    var count;           // Count temperatures: valid
    count = count + 1;   // Add 1 to count: NOT valid
    
  14. Constraints:
    1. There will be only ONE exit point in a function.
    2. If you use a return statement to return a value, there will be only ONE return statement in a function, and it will be the last line in the function.
    3. There will be only ONE exit point in a loop. This means no usage of break or continue in loops, unless required in an assignment (to make sure you know what they are).
    4. You may use break inside a switch statement, where it is often required.
  15. All functions will be preceded with a comment containing the purpose of the subroutine.
  16. All functions will include comments describing the parameters.
  17. Decision structures will have separate lines for the condition and the true statement. Never put the true (or the false) on the same line as the condition. Never do anything like this:
    if (x > y){isGreater = true;} else {isGreater = false;}
    Besides the standards violation, the above is bad practice. Preferred would be:
    isGreater = x > y;