04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 20: Best Practices<br />

What Is Maintainable Code?<br />

Maintainable code has several characteristics. In general, code is said to be maintainable when it is all of<br />

the following:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

Understandable — Someone else can pick up the code and figure out its purpose and general<br />

approach without a walkthrough by the original developer.<br />

Intuitive — Things in the code just seem to make sense, no matter how complex the operation.<br />

Adaptable — The code is written in such a way that variances in data don ’ t require a complete<br />

rewrite.<br />

Extendable — Care has been given in the code architecture to allow extension of the core<br />

functionality in the future.<br />

Debuggable — When something goes wrong, the code gives you enough information to<br />

identify the issue as directly as possible.<br />

Being able to write maintainable JavaScript code is an important skill for professionals. This is the<br />

difference between hobbyists who hack together a site over the weekend and professional developers<br />

who really know their craft.<br />

Code Conventions<br />

One of the simplest ways to start writing maintainable code is to come up with code conventions for the<br />

JavaScript that you write. Code conventions have been developed for most programming languages, and<br />

a quick Internet search is likely to turn up thousands of documents. Professional organizations have long<br />

instituted code conventions for developers in an attempt to make code more maintainable for everyone.<br />

The best - run open - source projects have strict code convention requirements that allow everyone in the<br />

community to easily understand how code is organized.<br />

Code conventions are important for JavaScript because of its adaptability. Unlike most object - oriented<br />

languages, JavaScript doesn ’ t force developers into defining everything as objects. The language can<br />

support any number of programming styles, from traditional object - oriented approaches to declarative<br />

approaches to functional approaches. A quick review of several open - source JavaScript libraries can<br />

easily yield multiple approaches to creating objects, defining methods, and managing the environment.<br />

The following sections discuss the generalities of how to develop code conventions. These topics are<br />

important to address, although the way in which they are addressed may differ, depending on your<br />

individual needs.<br />

Readability<br />

For code to be maintainable, it must first be readable. Readability has to do with the way the code is<br />

formatted as a text file. A large part of readability has to do with the indentation of the code. When<br />

everyone is using the same indentation scheme, code across an entire project becomes much easier to<br />

read. Indentation is usually done by using a number of spaces instead of by using the tab character,<br />

which is typically displayed differently by different text editors. A good general indentation size is four<br />

spaces, although you may decide to use less or more.<br />

636

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!