background preloader

Best Practice

Facebook Twitter

Key Principles of Maintainable JavaScript. JavaScript is a curious language.

Key Principles of Maintainable JavaScript

It's easy to write, but difficult to master. By the end of this article, hopefully, you'll transform your spaghetti code into a five-course meal, full of readable, maintainable yumminess! Why is it So Tough? The thing to remember, above all else when writing JS code, is that it's a dynamic language. This means there are a lot of ways to do things. The "hardness" of JavaScript is clearly evident when considering the following image: The teeny tiny book on the left is Douglas Crockford's MUST READ book, JavaScript: The Good Parts. JavaScript Module Pattern: In-Depth. The module pattern is a common JavaScript coding pattern.

JavaScript Module Pattern: In-Depth

It’s generally well understood, but there are a number of advanced uses that have not gotten a lot of attention. In this article, I’ll review the basics and cover some truly remarkable advanced topics, including one which I think is original. The Basics We’ll start out with a simple overview of the module pattern, which has been well-known since Eric Miraglia (of YUI) first blogged about it three years ago. If you’re already familiar with the module pattern, feel free to skip ahead to “Advanced Patterns”. JavaScript best practices - W3C Wiki. Introduction Writing a best practice article is quite a tricky business.

JavaScript best practices - W3C Wiki

To a number of you, what you are about to read will appear to be very obvious and just the sensible thing to do. However, looking around the web and getting code handed over to me from other developers for years has taught me that common sense is actually quite a rarity in live code on the web, and the “sensible and logical thing to do” gets pushed far down the priority list once you are in the middle of a project, and the deadline is looming.

So I’ve decided to make it easier for you by creating this article, which is a compilation of best practices and good advice I’ve amassed over the years, much of it learnt the hard way (experimentation and suchlike). Take the advice below to heart and keep it in a part of your brain that has a quick access route so you can apply it without thinking about it. Call things by their name — easy, short and readable variable and function names Keeping to English is a good idea, too. 24 JavaScript Best Practices for Beginners. As a follow-up to "30 HTML and CSS Best Practices", this week, we'll review JavaScript!

24 JavaScript Best Practices for Beginners

Once you've reviewed the list, be sure to let us know what little tips you've come across! JavaScript utilizes two different kinds of equality operators: === | ! == and == | ! = It is considered best practice to always use the former set when comparing. "If two operands are of the same type and value, then === produces true and ! However, when working with == and ! For those unfamiliar, the "eval" function gives us access to JavaScript's compiler. Not only will this decrease your script's performance substantially, but it also poses a huge security risk because it grants far too much power to the passed in text.

Technically, you can get away with omitting most curly braces and semi-colons. However, consider this: One might think that the code above would be equivalent to: Unfortunately, he'd be wrong. As you'll notice, the indentation mimics the functionality of the curly brace. Consider the following code: Google JavaScript Style Guide.

We follow the C++ formatting rules in spirit, with the following additional clarifications.

Google JavaScript Style Guide

Curly Braces Because of implicit semicolon insertion, always start your curly braces on the same line as whatever they're opening. For example: if (something) { // ... } else { // ... } Array and Object Initializers Single-line array and object initializers are allowed when they fit on a line: Multiline array initializers and object initializers are indented 2 spaces, with the braces on their own line, just like blocks. Long identifiers or values present problems for aligned initialization lists, so always prefer non-aligned initialization.

Not like this: Function Arguments When possible, all function arguments should be listed on the same line. // Four-space, wrap at 80. When the function call is itself indented, you're free to start the 4-space indent relative to the beginning of the original statement or relative to the beginning of the current function call. Another 10 Interesting JavaScript Features. I previously posted about 10 Interesting JavaScript Features.

Another 10 Interesting JavaScript Features

Here’s ten more JavaScript features that I’ve recently found interesting. 1. Dynamically call object methods Javascript objects can contain functions as object members. Object members can be referenced using square bracket [] notation. 2. JavaScript best practices - W3C Wiki. Enterprise js pratices. Javascript Best Practices. Introduction This document is a list of best practices and preferred ways of developing javascript code, based on opinions and experience from many developers in the javascript community.

Javascript Best Practices

Since this is a list of recommendations rather than a list of absolute rules, experienced developers may have slightly differing opinions from those expressed below.