Server side Javascript recommendations?

We have a Java based CMS and it has Mozilla Rhino for the JS back end. At the moment, the JS code base is small, but growing. Before it's too late, and the code has become a terrible mess, I want to introduce some best practices and coding style.

Obviously, namespace management is pretty important. But what about other best practices - especially for Java programmers?

+7
javascript coding-style serverside-javascript rhino
source share
5 answers

Here are some tips from the front:

  • Like Java, use Doxygen / JsDoc-style docblocks for functions
  • Unit test. Personally, like JsTestDriver, since it can be executed automatically from the CI server too.
  • Use JSLint. This will be bad code.
  • Consider using the Google Closure Compiler. It will ignore code like JSLint, but it can be useful for identifying poor blocks of documents, etc.
  • Make sure everyone on your team understands how closures work. Otherwise, it will lead to a headache.
  • As you recall, namespaces are important, especially if you want your code to work well with other JS libraries ( var myns = myns || {}; )
  • Personally, I find using a library that helps OOP helpers, like classes, etc. You can use prototype inheritance, but this is often a bit more complicated.
+20
source share

I would look at CommonJS (formerly ServerJS). This is a very big job, but they have a standardized modular system with several implementations. The CommonJS specification already has some useful libraries, such as Narwhal .

+3
source share

As Douglas Crockford likes to say, JavaScript is the world's most misunderstood programming language. Although many people do not know this, there is a proper way to code in JavaScript. I have no doubt that if you let Java developers start coding before you understand how to write good JavaScript, you will run into serious problems.

The first thing to do is to make sure everyone reads the wonderful Mozilla 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 accomplish most common tasks, and this article should contain people on one page. Another important reference is the work of Douglas Crockford, including JavaScript: "Good Details."

Another thing that many Java / C ++ programmers get is that JavaScript using a function region does NOT block the region. This can cause some very complex problems. There's a wonderful article about this issue in List Apart called Binding in JavaScript.


To summarize the main issues discussed in the above resources, the most important differences to study are

  • how to write object-oriented code using prototypal inheritance (class-based inheritance)
  • how to use shutters and lambdas
  • how to use the power of dynamic objects
  • how to write code with advanced features
+2
source share

Since you have a JS engine in Java, the habit of writing unit tests for your JS code. Choose a coding style and apply it vigorously. If possible, use the tools to verify that the code matches the encoding style.

+1
source share
+1
source share

All Articles