1

I am writing a custom UI builder in js. It uses an implementation of a factory to build elements. To make sure that it was obvious which functions were part of the library versus which were part of plain old javascript I used a naming convention of function _FunctionName(). However, I find it tedious to always be doing Factory._FunctionName().

Should I just remove the naming convention (function FunctionName())or stick to it?

Are there naming convention common / best practices with regards to making libraries like this?

edit:

var __PanelFactory = function () {
 //"private"
 var Panels = [];
 //exposed
 function _GetPanel(id) {
  //etc
 }
 return {
    _GetPanel: _GetPanel,
 };
};

var Factory = new __PanelFactory();
Factory. //this will show certain plain javascript functions 
         //like toString, constructor, hasOwnProperty, isPrototypeOf, etc...

//note that even jQuery can have the previous list used with something like
$(selector).

//So to differentiate I made sure my functions start with _
Factory._GetPanel(1);

//Should I just make it easy on myself and allow
Factory.GetPanel(1);

//Or is there value in leaving the naming convention in?
Travis J
  • 81,153
  • 41
  • 202
  • 273
  • 5
    Why are you making your factory functions global, rather than scoping them to a specific namespace? (for example, like how jQuery methods all live under `$` or `$.fn`.) – Kirk Woll May 22 '12 at 00:13
  • @KirkWoll - However, javascript still allows the basic `.toString`, `constructor`, etc. to any of those. How do you know they are naked functions? – Travis J May 22 '12 at 00:14
  • @TravisJ: If they're scoped to a namespace, I think they'll already be differentiable. I think you *have* to specify the namespace when you call them (?) – Merlyn Morgan-Graham May 22 '12 at 00:14
  • @MerlynMorgan-Graham - They must be accessed only after the factory has been initialized with `var Factory = new Factory()`. – Travis J May 22 '12 at 00:15
  • @TravisJ: So, like `var factory = new Factory();` ... `factory.DoSomething();`? Your question makes it look like you're talking about functions that aren't bound to instances of objects. – Merlyn Morgan-Graham May 22 '12 at 00:18
  • @MerlynMorgan-Graham - Sorry for a lack of context, let me edit with more of an example. – Travis J May 22 '12 at 00:18
  • @KirkWoll - See edit for an example, clearly the functions are *not* global. – Travis J May 22 '12 at 00:29
  • I admit I'm thrown by the naked, `Factory.` -- what is that intended to convey? Regardless, I'm also confused by why you think `Factory._GetPanel(1);` would collide with anything. It would only compete with members defined for `Factory`, which isn't much! Why do you think you need to add any prefixes to distinguish it from anything else? – Kirk Woll May 22 '12 at 00:44
  • @KirkWoll - The `Factory.` was intended to show the stopping point for when exposed methods show up in an IntelliSense type environment. Mostly just to point out that at that point there could be a lot of calls that could be made starting with `Factory.` such as `Factory.toString()`, or `Factory.toHtml()` (which would be custom), or `Factory.constructor` (plain js), etc... As for why I thought I would need to add prefixes, I was just trying to be rigorous in making sure that what I am making is easy to use and to be easily distinguishable. – Travis J May 23 '12 at 18:48

2 Answers2

4

Here are some pretty common conventions in javascript.

Constants

Usually in ALL_CAPS_WITH_UNDERSCORES

var MAX_COUNT = 100;
var BASE_URL = "http://example.com/api/";

Variables

Usually in lowerCamelCase

var currentCount = 0;
var userName = "timrwood";

Constructors

Usually in UpperCamelCase

function Factory(){}
var factory = new Factory();

Methods

Usually in lowerCamelCase

Factory.prototype.getPanel = function(){};
var factory = new Factory();
var panel = factory.getPanel();

Private Variables/Methods

Usually in _lowerCamelCaseWithAUnderscorePrefix;

Factory.prototype._fetchPanel = function(){};
Factory.prototype.getPanel = function() {
    return this._fetchPanel();
}
timrwood
  • 10,611
  • 5
  • 35
  • 42
  • 1
    +1 for nice overview, but I have a remark. Although using `_` for private fields is sometimes used, there are actually semantically better ways of declaring private fields - as a variable in a closure. – Imp May 22 '12 at 00:54
  • Yeah, a variable in a closure is definitely safer, but sometimes you need to add something to the prototype to be referenced with `this`, and using an underscore is pretty good indicator that it is intended to be private. – timrwood May 22 '12 at 00:57
  • 1
    @timrwood: http://stackoverflow.com/questions/4813119/accessing-this-in-javascript-closure – Merlyn Morgan-Graham May 22 '12 at 00:59
1

There is a question already on SO that links to a good document on Javscript coding conventions (including naming):

The key bits you've asked about:

  • Use namespaces (with inital-caps names).

This is somewhat optional, as PanelFactory won't collide with plain Javascript. It might collide with other libraries if you are making a public API to be consumed by third parties, though:

var MyApp = MyApp || {};
MyApp.__PanelFactory = function () { // ...
  • Get rid of the underscores (_) before all function names.

You don't need the underscores, as people who have been working with JavaScript for a while know the default instance-scoped functions built into every object (toString etc):

MyApp.PanelFactory = function () {
  // ...
  function GetPanel(id) {
    //etc
  }
  • Name object constructor functions with initial-caps names, but name global or namespace-scoped functions and instance-scoped functions with initial-lower-case names:

This is just a standard convention. It doesn't help distinguish built-in functions, but as I said in the previous item, you don't need to.

MyApp.PanelFactory = function () {
  // ...
  function getPanel(id) {
    //etc
  }
  • Name local variables with initial-lower-case names:

This will help you tell the difference between a namespace, an object constructor, and an instance of an object (where you would expect custom instance-scoped functions, commonly called "methods", to exist).

var factory = new MyApp.PanelFactory();
var panel = factory.getPanel();
Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183