Skip to content
  • Home
  • Agro
    • Agro
    • Akuaponik
    • Hidroponik
    • Ayam Kampung
  • DIY
    • DIY
    • Rumah Kampung
  • Tech
    • Tech
    • Titanium Mobile
  • Media
    • Media
    • Downloads
    • Dashcam Drive
  • Misc
    • Pelbagai
    • Makanan
    • Fotografi
    • Idea
    • E-mau
  • Home
  • Agro
    • Agro
    • Akuaponik
    • Hidroponik
    • Ayam Kampung
  • DIY
    • DIY
    • Rumah Kampung
  • Tech
    • Tech
    • Titanium Mobile
  • Media
    • Media
    • Downloads
    • Dashcam Drive
  • Misc
    • Pelbagai
    • Makanan
    • Fotografi
    • Idea
    • E-mau
blog.azwan082.my


Blog ini tidak lagi dikemaskini, sila ke
👉 azwan082.my 👈
untuk dapatkan kandungan terbaru.

#Code  #Tips  #Titanium Mobile

Efficient code

On 17 July 2013 in Tech

From my experience, an efficient code is code that have these characteristics:

  • it works
  • use optimum resource
  • manageable

Note: example codes are in Javascript, specifically Titanium mobile.

An efficient code must be code that can be run. There’s no point of evaluating a code that doesn’t run at all. Secondly, it must solve the problem it’s required to.

Example, a function must return letter ‘S’ for person with weight 50kg and below, ‘M’ for 51-70kg and ‘L’ for 71kg and above. So, the code must comply to this requirement

function getSizeFromWeight(weight) {
  if (weight <=50) {
    return 'S';
  }
  else if (weight <= 70) {
    return 'M';
  }
  return 'L';
}

Note: don’t bother to assign the result to another variable, just return it immediately.

An efficient code that use optimum resources, means the code only create necessary variables and use external resource such as filesystem, network connection, database or plugins only when needed and release those resources when done.

For example, iterating a list of files and getting each of the files’ modification timestamp, for each files that are modified a minute ago will need to be deleted. Some newbie programmer will collect a list of files that need to be deleted and then delete them, one by one. There’s nothing wrong with this method, except the code need to iterate twice in order to delete the expired files, which is inefficient.

var now = new Date().getTime();
var files = []; // assume this contains Ti.Filesystem.File objects
var i = files.length;
while (i--) {
  var f = files[i];
  if (f.modifiedTimestamp + 60000 < now) {
    f.deleteFile();
  }
}

Notes: Some loop statement (e.g while loop) is generally faster that other (for, foreach, do while).

Lazy loading is one of the technique that explains how to use optimum resource. Example, in a Titanium app, when a window opened, we only include those modules that required in order to open the window. If your app need to play audio, only load the audio player module when user invoke the functionality (via play button click etc.)

// open a window
var win = Ti.UI.createWindow();
var TopBar = require('ui/TopBar');
var ContentGrid = require('ui/ContentGrid');
win.add(new TopBar());
win.add(new ContentGrid());
win.open();

// play an audio, when click the play button
playBtn.addEventListener('click', function() {
  var Player = require('lib/Player');
  Player.start(this.audioUrl); // assume playBtn has this property
});

Note: During opening a window, only include UI components required in order to build the app window. And only when the play button is clicked then include the audio player module.

Code manageability means when you need to fix bug, add new feature or rewrite the code to accommodate new infrastructure, the existing code require little or zero adjustment. These small adjustment won’t break the current system.

Example, also in Titanium app, you have a feature to check for a new version of your app from the webserver. In initial release, the app only need to know the version number. Then in subsequent release, you want to add download link, thumbnail image etc. Current code shouldn’t have to be adjusted too much, till it break the calling component.

// initial release
function notifyNewUpdate(p) {
  Ti.UI.createAlertDialog({
    message: 'Version ' + p.version + ' is ready to be downloaded'
  }).show();
}

// subsequent release
function notifyNewUpdate(p) {
  var dialog = Ti.UI.createAlertDialog({
    message: 'Version ' + p.version + ' is ready to be downloaded'
  });
  dialog.addEventListener('click', function() {
    var Updater = require('lib/Updater');
    Updater.download(p.downloadUrl);
  });
  dialog.show();
}

Note: notice that input parameter to notifyNewUpdate() function is maintained. This allow developer to add more or remove unused parameter without breaking the function call.

Share this post:
  • Share
  • Tweet
  • LinkedIn

Related posts:

  • Titanium app components interaction using events and callbacks
  • Lazyloading JS modules in Titanium app
  • Enable x86 libs in production build Titanium app
  • Titanium iOS builder script
  • Method calling thread for Titanium module (Android)
  • Webview evalJS return null in Android 4.2.2
  • Flurry Android 3.2.0 SDK for Titanium module
  • Load nib file in Titanium module iOS
  • Android BOOT_COMPLETED handler module
  • Add custom framework to Titanium iOS module

Filed under Tech with tags Code, Tips, Titanium Mobile

Post navigation

Previous Post Previous post:
HTC phone from a block of aluminium
Next Post Next post:
Dragonball comics on iPad

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Media sosial

  • facebook
  • instagram
  • twitter
  • pinterest
  • youtube

Carian

Artikel popular

  • Saiz standard kayu, papan dan plywood
  • Pemasangan dinding plywood rumah kampung secara solo
  • 3 Jenis Asas Sistem Hidroponik Yang Digunakan Dalam Akuaponik Serta Variasinya
  • Kepincangan dalam menggunakan paip PVC sebagai saluran NFT hidroponik
  • Cara membuat sistem NFT untuk akuaponik – Saliran masuk
  • Cara pasang pintu rumah
  • Jenis skru kayu
  • Slack MacOS client keep crashing

Artikel terkini

  • 2 video #emau dari 2020/0221 February 2020
  • Gaya hidup minimalis14 February 2020
  • How to sync Mac OS Photos Library to an external disk storage17 January 2020
  • Sejarah dividen KWSP10 January 2020
  • Sejarah dividen Tabung Haji10 January 2020
  • Sejarah dividen ASB10 January 2020
  • Selamat dekad baru 2020-an3 January 2020
  • Dashcam Drive #17 – Taman Danu Serian → Ranchan Recreational Park1 January 2020

Tag

1N2D Akuaponik Android Apache Ayam Kampung Bash C# Cache Cili CommonJS Controller Dashcam Drive E-mau Fedora Fotografi Git Gnome Happy Together Hidroponik Idea Invincible Youth iOS Java Javascript Ke Indonesia Ke Kita? Ke Jepun Ke Kita? Ke Korea Ke Kita? Kewangan ListView Makanan Mr. Bean PHP Python Rant Roundtable Plus RPM Rumah Kampung Star Golden Bell Subversion SVN Titanium Mobile Titanium Module Titanium Studio windows phone WrestleMania

Arkib

Perihal

@azwan082

Seorang pembangun perisian (software developer) untuk peranti mudah alih (mobile devices) dan laman web, menggunakan bahasa pengaturcaraan Swift (iOS), Java (Android), PHP, MySQL dan Javascript (aplikasi web). Blog ini adalah tempat untuk saya berkongsi perkara, minat dan projek sampingan, seperti berkebun, kerja² kayu DIY, video dashcam drive dan pelbagai lagi.

© 2021 Noodlecode

Back to top
sponsored