Question

We have a CMS built on Java and it has Mozilla Rhino for the server side JS. At the moment the JS code base is small but growing. Before it is too late and code has become a horrible mess I want to introduce some best practices and coding style.

Obviously the name space control is pretty important. But how about other best practices - especially for Java programmers?

Was it helpful?

Solution

Here's some tips from the front lines:

  • Like Java, use docblocks in Doxygen/JsDoc style for functions
  • Unit test. Personally like JsTestDriver, as it can be executed automatically from CI server too.
  • Use JSLint. It will nitpick about bad code
  • Consider using Google Closure Compiler. It will nitpick about code like JSLint, but it can be helpful for spotting poor doc blocks etc.
  • Make sure everyone on your team understands how closures work. Otherwise it'll lead to headaches
  • As you mention, namespaces are important especially if you want your code to work nice with other JS libraries (var myns = myns || {};)
  • Personally I find using a library which provides OOP helpers like classes etc. helpful. You could use prototypal inheritance but it's often a bit trickier that way.

OTHER TIPS

I would look at CommonJS (formerly ServerJS). It's very much a work in progress, but they have a standardized module system with several implementations. There are already some useful libraries written to the CommonJS spec, like Narwhal.

As Douglas Crockford likes to say, JavaScript is the worlds most misunderstood programming language. Though many people don't know it, there is a right way to code in JavaScript. I have no doubt that if you let Java developers start coding before understanding how to write good JavaScript you will run into serious trouble.

The first thing to do would be to make sure everyone has read Mozilla's excellent article, A re-introduction to JavaScript (https://developer.mozilla.org/en/a_re-introduction_to_javascript). One of the biggest problems with JavaScript is that there are many ways to do most common tasks, and this article should get people on the same page. Another essential reference is Douglas Crockford's work, including JavaScript: The Good Parts.

One other thing that gets a lot of Java/C++ programmers is that JavaScript uses function scope NOT block scope. This can cause some very tricky problems. There's a great article about this issue at A List Apart called Binding in JavaScript.


To summarize the major issues talked about in the above resources, the most crucial differences to learn are

  • how to write object oriented code using prototypal inheritance (vs.class based inheritance)
  • how to use closures and lambdas
  • how to utilize the power of dynamic objects
  • how to write function-scoped code

Since you have a JS engine in Java, make it a habit to write unit tests for your JS code. Select a coding style and apply it vigorously. If possible, use tools to check that the code submits to the coding style.

Below are few links which may help you :

Javascript Best Practices

JavaScript Best Practices pdf file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top