Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server side Javascript best practices?

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?

like image 352
Petteri H Avatar asked Nov 30 '09 12:11

Petteri H


People also ask

Can I use JavaScript for server-side programming?

Server-side code can be written in any number of programming languages — examples of popular server-side web languages include PHP, Python, Ruby, C#, and JavaScript (NodeJS).

What is an advantage of using server side JavaScript?

A server-side rendering benefit is the reduced dependency on JavaScript frameworks, resulting in a reduced page weight. Furthermore, JavaScript files can significantly add to the overall page weight. It's not uncommon for a suite of JavaScript libraries to be over 1 MB in size.

Is JavaScript faster than server-side script?

A server is generally going to be orders of magnitude more powerful than a client machine; and managed code is generally much faster than scripting. However - the client machine also usually has a lot of spare computational power that isn't being used, while the server could be running requests for thousands of users.


1 Answers

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.
like image 54
Jani Hartikainen Avatar answered Sep 26 '22 05:09

Jani Hartikainen