With any program design it is important to use a programming convention when writing code. This not only helps the code look consistent (and one hopes avoids certain bugs), but also allows different programmers to work on the same code.
These conventions are guidelines, but not requirements, preferably all the rules should be met, but there are cases when there is good sense to break the convention.
AltServlet provides a certain degree of organization with its module system, but I will go a few steps further to say that:
- Each class should be in its own file of the same name, and should be within a descriptive package. For example, the SQLSchema class is in the alt.squeal package, and thus can be found in the file alt/squeal/SQLSchema.js.
- Scripts should require only the fewest number of scripts as necessary.
- The wildcard module require should not be used. (I.E. Alt.require("alt.*").)
Syntax conventions are written similar to the Java conventions:
- 4-space soft-tabs (or hard-tabs)
- Optional semi-colons should always be included.
- Open curly braces should be on the same line as their statement (such as, if, while, for, etc.).
- Optional curly braces around single-statement blocks should be left out (if, while, for, but not switch, try, or catch). If an if statement is combined with one or more else statements and one of the blocks has curly braces, then all the blocks should have curly braces (even if they're optional).
- White space should be used generously to separate ideas and make code more readable.
- Anonymous functions should not be named. ("arguments.callee" can be used for recursive calls in anonymous functions.)
- Avoid global functions if an inner function is sufficient.
- Always use var for local variables.
- Use comments to describe any complex bit of code, especially in long functions.
- Always put jsdocs on functions, even if they are considered "private." Try to document all parameters, exceptions, and return values.
- Classes should be defined with the standard named function syntax whenever possible (instead of assigning anonymous functions to variables).
- Class functions, on the other hand, should be defined as members of the prototype object with the anonymous function syntax.
- Class inheritance is defined by: SubClass.prototype = new BaseClass;
- Super class constructors can (and should) be called with the Function.call method.
- Use literal XML
notation (as opposed to strings) whenever it makes sense. If there is a lot of XML, consider an external file or using Onion ML.
- Alt Framework exceptions should inherit from alt.Exception and call its constructor.