Javascript coding guidelines & conventions

T

Thomas 'PointedEars' Lahn

Found these coding guidelines for C#, HTML, Javascript, Java, HTML,
PL/SQL, T-SQL, VB and VBScript.

Well written [...]

But still not good enough, e.g.:

| 2.4.5 Naming conventions
| [...]
| JavaScript supports the encapsulation of functions into classes.

Wrong. JavaScript before version 2.0 is an object-oriented programming
language that uses only _prototype-based inheritance_. Functions make
up Function objects that serve as constructors for objects when used as
operand of the `new' operator:

function Foo(x)
{
this.y = x;
}

var f = new Foo(42);

In JavaScript 2.0 which also provides class-based inheritance, but of
which currently only available a test implementation codenamed Epimetheus
is available, the `class' keyword is used to define classes, not the
`function' keyword:

<http://www.mozilla.org/js/language/js20/core/classes.html>

| Our standard is to prefix all member function definitions with
| the class name followed by an underscore as the following example
| will make clear:
|
| // Class definition, full comments omitted for clarity
| function CProducts
--------------------^
SyntaxError: missing ( before formal parameters

| {
| // ** Methods
| this.render = CProducts_Render;
| this.load = CProducts_Load;
| }
|
| function CProducts_Render()
| {
| ...
| }

This way of implementing methods counteracts proper object-oriented
programming style as the methods also become available as global methods.
Sometimes this is intended (e.g. for the purpose of backwards compatibility
to previous versions of a library that did not use OOP properly), however
here it obviously is not. Thus it is much better to write

function CProducts()
{
// ...
}

CProducts.prototype = {
render: function CProducts_Render()
{
...
}

// ...
};

here, or, if the prototype of CProducts should extend another prototype:

CProducts.prototype.render = function CProducts_Render()
{
...
};

| [...]
| 2.4.10 Including files
|
| Including JavaScript files is easy as this is supported by HTML.
| It will be no surprise that the syntax is:
|
| <SCRIPT LANGUAGE="JavaScript" SRC="javascript/library.js"></SCRIPT>

But it should be in fact a surprise to find that in a guideline for
commercial scripts since it is from deprecated to wrong. New projects
should use at least HTML 4.01 and in HTML 4 (and XHTML) the `type'
attribute is required for the `script' element while the `language'
attribute is deprecated (Transitional) or not even valid (Strict).

| 2.4.11 Clean up your objects
|
| Even though JavaScript has a built in garbage collector to reclaim
| resources from un-references objects and variables, it is good practive
| destroy your objects as soon as possible. This will result in a lower
| memory footprint in the customer's browser.
|
| The JavaScript syntax to dereference an object is
|
| objectName = null;

Wrong. JavaScript and Java have garbage collection for a good reason, that
is, an object may be referenced by more than one identifier. Assigning
`null' to an variable that previously referenced an object neither in Java
nor in ECMAScript, JScript or JavaScript forces the garbage collector to
destroy the referenced object; it is only destroyed if there is no further
reference to it *and* the garbage collection algorithm decides that it
should be destroyed. So using that statement does not reliably reduce the
memory footprint, instead it wastes computing time for the unnecessary
assignment.

| 2.4.12 Reading & writing configuration settings
|
| JavaScript in the browser can not directly access either the registry or a
| configuration database, therefore configuration settings need to be passed
| by JavaScript using ASP. [...]

Wrong. Even JScript in ASP can only access databases using a server-side
API, that is, in most cases, but not restricted to, that of the Internet
Information Server. So it is rather a question whether a client-side API
for the described processes is available which in turn depends on the
environment client-side scripts run; e.g. if running within a HyperText
Application (HTA), it is entirely possible to access the Registry via
objects of Windows Script Host using client-side JScript; the best example
for this is probably the Windows NT 5.0 (2k/XP) Control Panel where e.g.
the "Add/Remove Application" dialog is based on an HTA.

Furthermore, ASP is definitely not required server-side, there are
other platforms that enable one to perform the described processes.


I second many other recommendations of the Demachina Coding conventions &
guidelines (such as the indentation style), however it has to be pointed
out that it in its current form cannot be recommended as viable reference
material.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top