5

In JavaScript, the standard rules for code formatting don't seem to cut it. You still end up with messes of });}); all over the place and I don't believe I even know of established rules for the correct indention of anonymous functions declared as arguments to other functions.

In short, I have trouble reading my own JavaScript, and I bet I'm not alone.

I think the idea that I am coming around to is to just not define functions within other functions. Write every function with a name and as a child to the script tag. When you need to pass functions around, use their names. Of course, this makes it a lot harder to use closures [edit: you cannot use closures at all if you write your code that way]. Also, this might cause problems with memory management?

Anyone else have thoughts on this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Joe
  • 7,922
  • 18
  • 54
  • 83
  • 3
    If I'm understanding your suggestion correctly it doesn't _just_ make it harder to use closures, it makes it impossible. You should add some examples to show what you mean. Personally I don't have trouble reading Javascript which uses closures, but I have a few years under my belt. – coderjoe Nov 02 '11 at 20:42
  • 3
    Not really. I write massive complex js sites and have no problems reading my own code or anyone else's so long as they indent as they would any other language. anonymous functions included. It's all up to you to decide to write in things like });}); instead of breaking them into their own lines and indent them properly. – Kai Qing Nov 02 '11 at 20:43
  • I find my JavaScript pretty readable, and I stick pretty much to typical formatting conventions with JavaScript. You *do* end up with `});`, but if you're nesting deeply enough that you can't read it later, then break it up as necessary: code turned on its side is not a graph of how awesome it is. But small, anonymous methods are canonical, and make sense most of the time. – Dave Newton Nov 02 '11 at 20:46

3 Answers3

5

I bet you should just follow already established coding standards.

You may follow coding standard for one of two best JavaScript frameworks:

Basically use tabs instead of spaces (this may be difficult for some, but I believe is established standard for JS), avoid unnecessary spaces (eg. in ) { in function definition space is not welcome and should probably look like: ){), etc.

PS. It is my personal > opinion < that MooTools & jQuery are 2 the best JS frameworks.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
4

You need to format your code in order to be readable. And always while typing JS use IDE features to format code and each new command put into new row. Then you won't have a problems.

You can use Online JavaScript beautifier for your existing javascript.

Senad Meškin
  • 13,597
  • 4
  • 37
  • 55
0

http://en.wikipedia.org/wiki/Indent_style http://en.wikipedia.org/wiki/Coding_conventions

Are a good place to start. I personally use the K&R style for clarity. When it comes down to it though all you need to do is find a style you like and stick with it. Consistency ;]

k4t434sis
  • 519
  • 4
  • 17