User:Avarma/Coding Style: Difference between revisions

added more
(added section on closures)
(added more)
Line 15: Line 15:


Furthermore, the comments before functions or sections of code are usually commented using [http://www.wikicreole.org/ WikiCreole] syntax, so as to be compatible with [http://www.toolness.com/wp/?p=441 Code Illuminated].
Furthermore, the comments before functions or sections of code are usually commented using [http://www.wikicreole.org/ WikiCreole] syntax, so as to be compatible with [http://www.toolness.com/wp/?p=441 Code Illuminated].
= Whitespace, Line Length, and Indentation =
I follow the conventions laid out in the Mozilla JS style guide.
In particular, I am pretty hardcore about making sure that lines are never longer than 79 characters, and I never use tabs.
== Annoyances ==
Ideally, I'd like to indent code like this:
  setInterval(function() {
    doThing()
  }, 1000);
But <tt>js2-mode</tt> doesn't like it, so I do this:
  setInterval(
    function() {
      doThing()
    }, 1000);
Similarly, I can't do this with <tt>js2-mode</tt>:
  jQuery.ajax({
    url: "blah.txt",
    method: "GET"
  });
So I have to settle with this:
  jQuery.ajax(
    {url: "blah.txt"
    method: "GET"
  });
There's a number of other such indentation annoyances that I have to navigate around because I use <tt>js2-mode</tt>, but in general I find that the editing mode solves a lot more problems for me than it creates, so I live with it.
= Brackets =
Being a fan of Python, I feel that brackets detract from readability so I don't use them when I don't have to, e.g.:
  if (x > 1)
    doThing();
A decent editor that supports auto-indentation will help ensure that you don't accidentally modify the code such that the semantics deviate from what the indentation implies:
  if (x > 1)
    doThing();
    doOtherThing();


= Private Variables, Methods, and Closures =
= Private Variables, Methods, and Closures =
93

edits