04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

❑<br />

❑<br />

Chapter 20: Best Practices<br />

File size — You write code in a way that makes it easy to read, which is good for maintainability<br />

but bad for performance. The browser doesn ’ t benefit from the extra white space, indentation, or<br />

verbose function and variable names.<br />

Code organization — The way you organize code for maintainability isn ’ t necessarily the best<br />

way to deliver it to the browser.<br />

For these reasons, it ’ s best to define a build process for your JavaScript files.<br />

A build process starts by defining a logical structure for storing your files in source control. It ’ s best to<br />

avoid having a single file that contains all of your JavaScript. Instead, follow the pattern that is typically<br />

taken in object - oriented languages: separate each object or custom type into its own file. Doing so<br />

ensures that each file contains just the minimum amount of code, making it easier to make changes<br />

without introducing errors. Additionally, in environments that use concurrent source control systems<br />

such as CVS or Subversion, this reduces the risk of conflicts during merge operations.<br />

Keep in mind that separating your code into multiple files is for maintainability and not for deployment.<br />

For deployment, you ’ ll want to combine the source files into one or more rollup files. It ’ s recommended<br />

that web applications use the smallest number of JavaScript files possible, because HTTP requests are<br />

some of the main performance bottlenecks on the Web. Keep in mind that including a JavaScript file via<br />

the < script > tag is a blocking operation that stops all other downloads while the code is downloaded<br />

and executed. Therefore, try to logically group JavaScript code into deployment files.<br />

Once you ’ ve organized your file and directory structure, and determined what should be in your<br />

deployment files, you ’ ll want to create a build system. The Ant build tool ( http://ant.apache.org )<br />

was created to automate Java build processes but has gained popularity with web application developers<br />

because of its ease of use and coverage by software engineers such as Julien Lecomte, who have written<br />

tutorials explaining how to use Ant for JavaScript and CSS build automation (Lecomte ’ s article can be<br />

found at www.julienlecomte.net/blog/2007/09/16/ ).<br />

Ant is ideal for a JavaScript build system because of its simple file-manipulation capabilities. For example,<br />

you can easily get a list of all files in a directory and then combine them into a single file as shown here:<br />

< project name=”JavaScript Project” default=”js.concatenate” ><br />

< !-- the directory to output to -- ><br />

< property name=”build.dir” value=”./js” / ><br />

< !-- the directory containing the source files -- ><br />

< property name=”src.dir” value=”./dev/src” / ><br />

< !-- Target to concatenate all JS files -- ><br />

< !-- Credit: Julien Lecomte, http://www.julienlecomte.net/blog/2007/09/16/ -- ><br />

< target name=”js.concatenate” ><br />

< concat destfile=”${build.dir}/output.js” ><br />

< filelist dir=”${src.dir}/js” files=”a.js, b.js”/ ><br />

< fileset dir=”${src.dir}/js” includes=”*.js” excludes=”a.js, b.js”/ ><br />

< /concat ><br />

< /target ><br />

< /project ><br />

661

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

Saved successfully!

Ooh no, something went wrong!