Book Image

Appcelerator Titanium: Patterns and Best Practices

By : Boydlee Pollentine , Trevor Ward
Book Image

Appcelerator Titanium: Patterns and Best Practices

By: Boydlee Pollentine , Trevor Ward

Overview of this book

<p>Titanium Mobile has quickly become the platform of choice for many mobile developers and is growing and changing at a rapid rate. From the implementation of CommonJS,&nbsp; MVC design patterns and more, the last year in Titanium development has been a rollercoaster of change for the better. With this knowledge at your disposal you’ll be creating top quality, highly capable and stable apps in no time.<br /><br />This book shows you exactly how to implement all the latest Titanium Mobile best practices into your apps, from a thorough explanation of CommonJS with plenty of examples, through to structuring a complete MVC style codebase. With advanced topics such as implementing patterns and utilizing ACS, through to a thorough investigation of CommonJS and prototype, this book will take you from Titanium Novice to Titanium Ninja in no time!<br /><br />"Appcelerator Titanium: Patterns and Best Practices" starts off with some explanations on JavaScript practices and advanced topics, before getting stuck into the new CommonJS pattern and using that to implement MVC-style architectures for your application. It continues in a practical, hands on manner, explaining how to perform cross device layouts with different techniques, and how to implement SQL alternatives such as JSONDB.<br /><br />The book discusses some of the major advanced JavaScript topics, such as prototype and micro optimizations, before leading the developer into a thorough explanation of the CommonJS pattern, MVC implementation and advanced topics such as SQL alternatives and implementing designs for cross device layouts.</p>
Table of Contents (12 chapters)

Micro optimizations


While the following practices may not bring you vast speed improvements (and not doing them will certainly not make your code "bad" in any way), they are nonetheless good habits to get into.

Declaring variables outside the for statements

When you're executing a lengthy for statement, there is no need to make the JavaScript engine work harder than it has to. Take the following example, which calculates the length of the array myArray every time the loop reruns:

//avoid constant recalculation of the length of myArray
for(var c = 0; c < myArray.length; c++){
   //do something with myArray[c]
}

A much better way of writing this for statement is to cache the length of the array in a variable. This way, the value of myArray.length is retrieved only once, and used during the entire loop.

//a better way of looping through myArray
var max;
for(var c = 0; max = myArray.length,c < max; c++){
   //do something with myArray[c]
}

If the order that you access the array values in is not important, then it's also slightly quicker to count down than up. This is because it's more efficient to count down to zero than it is to calculate and equate your values to an arbitrary number, such as the length of myArray. For example:

//looping backwards is faster
var c;
for(c = myArray.length; c--;){
   //do something with myArray[c]
}

//alternatively, you can use a while loop in this instance
while(c--) {
  //do something with myArray[c]
}

Using forEach instead of for loop

Where possible, it's best to use a forEach loop in lieu of a standard forloop, particularly if you're looping through array values. For example, we could alter our previous sample code to look something like this:

function printResults(element, index, array){
  //do something with myArray[index];
  Ti.API.info(index + ' = ' + element);
}

myArray.forEach(printResults);

Using shortcuts for simple if statements

Sometimes, you simply need to check a single value and determine whether it matches a specific value before proceeding in your code. This might be as simple as determining whether an object is null, or checking that an operator is true or false. In many cases, it's simpler to use a few shortcuts.

//here we are checking if this object is null
var myObject;
if(myObject !== null) {
  myObject = {
    name: 'Boydlee'
  };
}

//a better way of declaring this object would be 
var myObject = myObject || {
    name: 'Boydlee'
};


//in this example we're checking if a value equals true, and 
//assigning a variable accordingly
var dinner = '';

if(isBritish === true) {
  dinner = 'Burger & Chips';
}
else {
  dinner = 'Hamburger & Fries';
}


//a simpler way to perform this check would be to use the
//pattern "variable = (condition) ? True-value : false-value;
dinner = isBritish ? 'Burger & Chips' : 'Hamburger & Fries';