Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
dev:client_coding:coding_guidelines [2011/11/03 15:01] – [Public/privileged/private/static methods and members] dstillmandev:client_coding:coding_guidelines [2017/11/12 19:53] – external edit 127.0.0.1
Line 1: Line 1:
 +<html><p id="zotero-5-update-warning" style="color: red; font-weight: bold">We’re
 +in the process of updating the documentation for
 +<a href="https://www.zotero.org/blog/zotero-5-0">Zotero 5.0</a>. Some documentation
 +may be outdated in the meantime. Thanks for your understanding.</p></html>
 +
 +
 JavaScript code in Zotero should conform to the following coding and style guidelines. We encourage Zotero plugin writers to follow these guidelines as well for consistency and possible future integration of code into the Zotero codebase. JavaScript code in Zotero should conform to the following coding and style guidelines. We encourage Zotero plugin writers to follow these guidelines as well for consistency and possible future integration of code into the Zotero codebase.
  
 **Note:** Not all current code in Zotero conforms to these guidelines. While existing code generally should not be modified for the sole purpose of conforming, new code or modifications to existing code should follow these guidelines. **Note:** Not all current code in Zotero conforms to these guidelines. While existing code generally should not be modified for the sole purpose of conforming, new code or modifications to existing code should follow these guidelines.
  
-====== Basic style =====+===== Basic style ====
  
   * Indent using tabs (width 4), not spaces   * Indent using tabs (width 4), not spaces
-  * Lines should generally be kept to 80 characters, except where doing so would seriously decrease readability+  * Lines should generally be kept to 100 characters, except where doing so would seriously decrease readability
   * Objects should be CamelCased and namespaced (''Zotero.FooBar'' within the [[dev/client_coding/javascript_api#the_zotero_object|Zotero XPCOM object]], ''ZoteroFooBar'' without)   * Objects should be CamelCased and namespaced (''Zotero.FooBar'' within the [[dev/client_coding/javascript_api#the_zotero_object|Zotero XPCOM object]], ''ZoteroFooBar'' without)
   * Functions and variables should be lowercase (''Zotero.Date.sqlToDate()'', ''var foo = true;'')   * Functions and variables should be lowercase (''Zotero.Date.sqlToDate()'', ''var foo = true;'')
-  * Code should comply with JavaScript strict mode (javascript.options.strict in about:config): variables should be initialized before using, etc.+  * Code should comply with JavaScript strict mode, with %%"use strict";%% at the top of files: variables should be initialized before using, etc.
  
  
-====== Braces ======+===== Braces =====
  
-Braces should conform to [[http://en.wikipedia.org/wiki/Indent_style#K.26R_style|K&R style]]:+Braces should conform to [[http://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS|1TBS variant of the K&R style]]:
  
 <code javascript> <code javascript>
Line 26: Line 32:
 </code> </code>
  
-Braces must **always** be used, even if the enclosed block contains a single line.+Add a space after keywords to distinguish them from function calls: 
 + 
 +Bad: 
 +<code javascript> 
 +if(foo) { 
 +</code> 
 + 
 +Good: 
 +<code javascript> 
 +if (foo) { 
 +</code> 
 + 
 +Braces must **always** be used for multi-line conditionals, even if the enclosed block contains a single line.
  
 Bad: Bad:
Line 40: Line 58:
 } }
 </code> </code>
- +===== Public/privileged/private/static methods and members =====
- +
- +
-====== Public/privileged/private/static methods and members ======+
  
 //For an explanation of the difference between public, privileged and private methods and members, please see Douglas Crockford's [[http://javascript.crockford.com/private.html|Private Members in JavaScript]].// //For an explanation of the difference between public, privileged and private methods and members, please see Douglas Crockford's [[http://javascript.crockford.com/private.html|Private Members in JavaScript]].//
Line 53: Line 68:
 } }
  
-Zotero.Item.prototype.numCreators = function() {+Zotero.Item.prototype.numCreators = function () {
     ...     ...
 } }
 </code> </code>
-  * Singleton objects should use privileged methods:+  * Singleton objects should use a wrapper object or object notation:
 <code javascript> <code javascript>
-Zotero.Date new function() { +Zotero.FooBar (function () { 
-    this.sqlToDate sqlToDate(sqlDate, isUTC) {+    // Private members and methods 
 +    var _foo "bar"; 
 +     
 +    function foo {
         ...         ...
     }     }
-} + 
-</code> +    // Public members and methods 
-  * Private methods and members (including those that are actually public or privileged for technical reasons but that should be considered private) should be prefixed with an underscore (e.g. ''var _initialized = false;'', ''Zotero.Item.prototype._loadItemData = function() {...}'') +    return 
-  * Related static functions should be placed in a wrapper object using a singleton or object literal notation: +        getFooObjectsfunction () { 
-<code javascript> +            ... 
-Zotero.FooBar = new function() +        
-     this.getFooObjects function () { +    }; 
-          ... +}());
-     +
-     ... +
-};+
 </code> </code>
  
Line 80: Line 95:
 <code javascript> <code javascript>
 Zotero.FooBar = { Zotero.FooBar = {
-     getFooObjects: function () { +    getFooObjects: function () { 
-          ... +         ... 
-     }, +    }, 
-     ...+    ...
 }; };
 </code> </code>
 +  * Private members and pseudo-private members/methods (i.e., those that are actually privileged for technical reasons but that should be considered private) should be prefixed with an underscore (e.g. ''var _initialized = false;'', ''Zotero.Item.prototype._loadItemData = function() {...}'')
  
  
 +===== Comments =====
  
 +Functions should be commented using JSDoc syntax:
  
-====== Additional notes ======+<code javascript> 
 +/** 
 + * Does something or other 
 + * 
 + * @param {string} value - This is a value 
 + * @param {boolean} [optionalValue] - This is an optional value 
 + * @return {number[]} - Array of itemIDs 
 + */ 
 +function myFunction(value, optionalValue) { 
 +   ... 
 +
 +</code> 
 + 
 + 
 +For readability and neatness, add a space after the slashes in line comments, and capitalize the first word: 
 + 
 +Bad: 
 +<code javascript> 
 +//this is a comment 
 +var foo 'bar'; 
 +</code> 
 + 
 +Good: 
 +<code javascript> 
 +// This is a comment 
 +var foo = 'bar'; 
 +</code> 
 + 
 + 
 + 
 +===== Additional notes =====
  
   * Variables should be defined in the smallest scope possible:   * Variables should be defined in the smallest scope possible:
Line 108: Line 156:
 function sum(num) { function sum(num) {
     var total = 0;     var total = 0;
-    for (var i=0; i<num; i++) {+    for (let i=0; i<num; i++) { // i is local to the for loop
         total += i;         total += i;
     }     }
     return total;     return total;
-} 
-</code> 
-  * Functions should be commented: 
-<code javascript> 
-/* 
- * Does something or other 
- * 
- * Returns array of ids 
- */ 
-function myFunction() { 
-   ... 
 } }
 </code> </code>
Line 128: Line 165:
  
 <code javascript> <code javascript>
-var func = function () { +var func = function (bar) { 
-    ...+    var foo = bar;
 }; };
 </code> </code>
Line 135: Line 172:
   * Global constants should be placed in the ''ZOTERO_CONFIG'' array in zotero.js and capitalized   * Global constants should be placed in the ''ZOTERO_CONFIG'' array in zotero.js and capitalized
   * Debugging messages should use the ''Zotero.debug(message, level)'' function. ''level'' is optional and defaults to 3. Error messages (i.e. things that should never happen and that can't be handled gracefully) should be thrown rather than passed to ''debug()''.   * Debugging messages should use the ''Zotero.debug(message, level)'' function. ''level'' is optional and defaults to 3. Error messages (i.e. things that should never happen and that can't be handled gracefully) should be thrown rather than passed to ''debug()''.
 +  * When throwing an error, use ''throw new Error()'' instead of throwing a string (which doesn't generate a stack trace)
   * All user-viewable text should be put in the en-US locale rather than embedded directly into the code, for localization and maintenance purposes.   * All user-viewable text should be put in the en-US locale rather than embedded directly into the code, for localization and maintenance purposes.
  
  
-====== Other issues ====== 
  
-For issues not specified here, Zotero code should generally follow the [[http://dojotoolkit.org/community/styleGuide|Dojo Style Guide]] from the Dojo project. 
dev/client_coding/coding_guidelines.txt · Last modified: 2017/11/27 05:18 by bwiernik