Skip to content

MathQuill Style Guide

laughinghan edited this page Mar 31, 2012 · 7 revisions

We're pretty OCD about our code. Since you'll have to put up with our idiosyncrasies to contribute to MathQuill, we figured we should write them down. Format and code samples graciously borrowed (and by borrowed I mean plagiarized) from: http://docs.jquery.com/JQuery_Core_Style_Guidelines

Semicolons

  • Semicolons terminate all statements, including inside one-liner functions, except statement blocks.
// Heresy:
f(function() { return blah() })
while (predicate()) {
  a = x.something()
  b += a
};

// Sacred:
f(function() { return blah(); });
while (predicate()) {
  a = x.something();
  b += a;
}

Spacing

  • Indent your code with soft tabs, 2 spaces. There should be no hard tab characters anywhere, ever.
  • Strip all trailing whitespace, including on empty lines.
  • Make sure your files end with newlines (looking at you, TextMate users)
  • A single space always follows keywords, colons, and commas, and always separates oppositely-directed delimiters (like ) {)
// Heresy:
if(blah==="foo"){
  foo("bar","baz",{zoo:1});
}

// Sacred:
if (blah === "foo") {
  foo("bar", "baz", { zoo: 1 });
}
  • The exception to this is the function keyword, which is always followed directly by (
// Heresy:
function (foo) { bar(); }

// Sacred:
function(foo) { bar(); }
  • No parentheses padding
// Heresy:
foo( "bar", "baz", { zoo: 1 } );

// Sacred:
foo("bar", "baz", { zoo: 1 });
  • Opening braces are always prefixed by a space.
// Heresy:
{a: 1, b: function() {return 2;}}

// Sacred:
{ a: 1, b: function() { return 2 } }

Equality

  • Always use ===. Seriously. If you use == we will flay you.

Blocks

  • Braces should always be used on if/else/for/while/try, unless they are a single line. if statements with else blocks should always be multiple lines and have braces.
// Heresy:
if (cond)
  blah();

// Heresy:
if (cond) { blah(); }

// Heresy:
if (cond) blah();
else foo();

// Sacred:
if (cond) {
   blah();
}

// Sacred:
if (cond) return;
if (cond) blah();
  • else/else if/catch go on the line after the close brace.
   if (cond1) {
     f1();
   }
   else if (cond2) {
     f2();
   }
   else {
     f3();
   }
  • No using && as a conditional; i.e. variable && variable.method(). Use if (variable) variable.method() instead.

Strings

  • Strings should always use double-quotes in html/css, and single-quotes in javascript.