• Diff for "JavaScriptStyleGuide"
Differences between revisions 3 and 8 (spanning 5 versions)
Revision 3 as of 2012-12-06 01:46:49
Size: 923
Editor: baalexander
Comment:
Revision 8 as of 2013-04-29 17:44:27
Size: 1991
Comment:
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
This page defines a style guide to be followed in writing JavaScript code for ROS.  This guide applies to all ROS code, both core and non-core. This page defines a style guide to be followed in writing `JavaScript` code for ROS. This guide applies to all ROS code, both core and non-core.
Line 14: Line 14:
 * camelCase variables and function names, CapitalCamelCase for classes, MY_VAR for constants
 * Semicolons are good
  {{{
function foo() {
  var indentation = 2;
}
}}}
 * Camel case variables and function names
  {{{
var myNewVariable = 'robots';
}}}
 * Capital camel case for classes
  {{{
MyNewClass = function() { ... };
}}}
 * Single-word caps for namespaces (typically ending in `JS`)
  {{{
ROBOTJS.MyNewClass = function() { ... };
}}}
 * Underscored caps for constants
  {{{
ROBOTJS.MY_NEW_CONST = 3.14;
}}}
 * Options object for all class constructors
 * Public functions should use `prototype`
 * Semicolons are required
Line 18: Line 40:
 * Single quotes for strings
 * Same-line brackets
Line 25: Line 49:
 * @param foo - task selector  * @param options - possible keys include:
 * * myMessage (optional) - an example message
Line 27: Line 52:
function exampleTask(foo) {
  var myVar = 'My message';
ROBOTJS.MyAwesomeClas = function (options) {
  options = options || {};
  var myMessage = options.myMessage || 'My message';
Line 30: Line 56:
  if (foo === 'Do this') {
    anotherExampleTask(myVar);
  if (myMessage === 'Do this') {
    alert('Not a pubic function');
Line 34: Line 60:
    someOtherTask(myVar);     this.myPublicFunction(myVar);
Line 36: Line 62:
} };

/**
 * Alert a string in this public function.
 *
 * @param str - a string to alert
 */
ROBOTJS.MyAwesomeClas.prototype.myPublicFunction = function(str) {
  alert(str);
};
Line 38: Line 73:

== Linting ==
Linting of `JavaScript` files should be done using [[http://www.jshint.com/|JSHint]]. The standard configuration file can be found [[https://github.com/RobotWebTools/starter-template/blob/stable/utils/.jshintrc|on the Robot Web Tools GitHub]].

ROS JavaScript Style Guide

This page defines a style guide to be followed in writing JavaScript code for ROS. This guide applies to all ROS code, both core and non-core.

For Python, see the Python Style Guide and for C++, see the C++ Style Guide

Coding Style

A quick summary is:

  • 2 space indention
    • function foo() {
        var indentation = 2;
      }
  • Camel case variables and function names
    • var myNewVariable = 'robots';
  • Capital camel case for classes
    • MyNewClass = function() { ... };
  • Single-word caps for namespaces (typically ending in JS)

    • ROBOTJS.MyNewClass = function() { ... };
  • Underscored caps for constants
    • ROBOTJS.MY_NEW_CONST = 3.14;
  • Options object for all class constructors
  • Public functions should use prototype

  • Semicolons are required
  • JSDoc for documentation

  • Triple equals (===) for comparisons

  • Single quotes for strings
  • Same-line brackets

An example can be worth a thousand words:

/**
 * Performs an example task.
 *
 * @param options - possible keys include:
 *   * myMessage (optional) - an example message
 */
ROBOTJS.MyAwesomeClas = function (options) {
  options = options || {};
  var myMessage = options.myMessage || 'My message';

  if (myMessage === 'Do this') {
    alert('Not a pubic function');
  }
  else {
    this.myPublicFunction(myVar);
  }
};

/**
 * Alert a string in this public function.
 *
 * @param str - a string to alert
 */
ROBOTJS.MyAwesomeClas.prototype.myPublicFunction = function(str) {
  alert(str);
};

Linting

Linting of JavaScript files should be done using JSHint. The standard configuration file can be found on the Robot Web Tools GitHub.

Wiki: JavaScriptStyleGuide (last edited 2014-11-07 15:16:53 by Russell Toris)