Jetty Java Coding Standards
The following is an example of the Java formatting and naming styles to be applied to Jetty:
For convenience' sake, Code Template and Java Code Formatter configuration files for Eclipse are available.
indents, then spaces should not be used. ie all spaces or all tabs - no mixed.
Use objects rather than functions.
we should write:
Private fields and functions should have names starting with '_'.
Note that this is a very light weight form of object and we are not using any concept of class. It is equivalent to a javaclass of static members and simply groups related functions together for name space clarity.
JS files as packages
Use semicolon to end statements
I know it is optional, but it makes it easier for old C-hacks to parse as well as making it easier for the interpretors to spot errors if a typo is made.
Unique Id's, multiple classes
The ID of a DOM element must be unique, while multiple DOM elements can have the same class. A frequent mistake is to use a non unique id, for example on a cell in a table (if the table has multiple rows then a cell id is not unique). A class should be used if there are visual ramifications, otherwise normal attributes or external associative arrays should be used.
The this automatic field in js works differently to java. If a member function of an object is called as a callback, then the this does not refer to the object but the calling scope. For example in the following code:
the _handle method is called when the mybutton is clicked, but the
this will not refer to the
myhandler object, but rather the DOM element of the button! The object var
myhandler can be used instead of
this in these circumstances.
Use 'single' instead of "double" quotes
The exception to this is in JSON, which should always use double quotes as that is what is defined by the standard.
Use var keyword for variables inside for loop
I know this is supposed to be optional but IE does not seem to handle well for loops with variables that are not declared with var. Thus instead of writing:
we should write:
Many libraries have a concept of behaviours, not least the behaviour library: behaviour.
be used where possible/suitable.