Best practices in JS development?

There is a lot to say on the subject.

First, you seem to wonder about the difficulty of keeping your code understandable and maintainable. There isn't a magic recipe. I work on fairly large JavaScript projects and face this issue every day.

Because JavaScript is not statically checked, the amount of knowledge one needs to maintain about the code is greater than with some other languages. Because in JavaScript, you can never be sure of what an object does or what data it contains until the code actually gets executed, it can be very tedious to write complex logic and workflows.

Our answer to that is to make our code as simple as possible. Complex logic is encapsulated into simple, reusable objects. Each function has clear responsabilities, and little side effects. Our code is mostly context-agnostic (it can be re-used in other projects, each module has very few or no dependencies). We try to avoid functions that require a lot of arguments, or objects that do a lot of things. We also avoid writing code that requires a lot of conditional statements. That gives us a library of functionalities that allows us to compose complex logic with little effort. In the end, all that's needed in the HTML view are a few one-line calls to functions. Everything else is in .js files.

Also, consistency is important. Stick to common patterns. Make your code predictable.

Our typical JavaScript file only contains one object. Files are grouped by area of concern (controls, core, services, etc.).

Using libraries such as jQuery is a good idea because they care of most of the boiler-plate code and are supported by large communities of developers. They can simplify aspects of the development (e.g. dealing with the DOM, Ajax, JSON serialization).

Unit-testing has been useful in some cases. We use QUnit to test our JavaScript code. It helps us detect unexpected behaviors or breaking changes.

Depending on your needs, you might also try languages or frameworks that compile code into JavaScript. There are pages with comprehensive lists of those. There are pros (static checking, better type system) and cons (poor support for libraries, projects that get dropped).

Regarding using Ajax to communicate with the server, I recommend that you look into RESTful principles. Ajax is just a tool. You can do a lot if things with it.


It is a big language and can be scary at times. What is stopping you at the moment is a lack of knowledge on the subject. The more you learn the more you know/wont be afraid to try new things.

There are excellent articles/resources on the web; and even though some articles may seem elementary, it is a great starting point. The more time you devote to the language, the better it will take care of you :)

UPDATE

What you are looking for in my opinion is 'design patterns'. They define a set of best practices to perform certain tasks;

  • Here is a related question on design patterns.
  • Here is another link on JS patterns
  • I also suggest you subscribe to javascript weekly to get the latest news on anything JS :)