Skip to content

X:style

haehn edited this page May 15, 2012 · 10 revisions

XTK coding style draft

Naming Convention

  • use camelCase for variables and functions
  var topLevelObjectsLength = 0;
  X.renderer.prototype.resetViewAndRender = function() {..}

Class Design

  • class names are lowercase (with events as an exception which are upper-camelcase)
  • if possible, always inherit from X.base - this enables the event system
  • each class has a className attribute (for duck typing and down casting)
  /**
   * @inheritDoc
   * @const
   */
  this._className = 'renderer';
  • export symbols (only methods/functions) to avoid optimization by the compiler but only as little as required since it blows up the library size

Class variables / Properties / Getters-Replacement

  • All properties start with an underscore _
  • Only protected properties
  • To make properties public, use a getter/setter mechanism and remove the underscore '_'
/**
 * Get the className of this object.
 * 
 * @return {string} The className of this object.
 * @public
 */
X.base.prototype.__defineGetter__('className', function() {

  return this._className;
  
});
  • Protected properties (will be renamed during optimization)
 this._objects = new X.array();
  • Only use the protected properties internally (no getter access)

Local variables

  • however you like

Loops

  • decrementing while loop is faster than for
  • Fast Duff's Device is the fastest but takes more lines of code
Clone this wiki locally