Writing efficient code

Last modified: May 4, 2009 - 08:19

In the programming world, it is a race. It's a race to see who can write code that runs the fastest. Not just the quickest code, but the most useful code. And if you're a programmer, who then is your opponent? Yourself, of course! As programmers, we seek to write code that not only does something, it does something well. We refer to this "wellness" as efficiency. Efficiency is often derived from both speed and purpose. Together, the two must thrive harmoniously. As in the case of Drupal, speed is of great importance because we do not want the user to wait or have the server timeout. Purpose is equally important because Drupal is really nothing if there is nothing useful it can provide to it's audience.

Here is one way to think about: a tangible product. Say you own a company that produces product B. Your company, like any other company that produces a product, is being challenged: do you create a lot of product B really fast, or do you make product B extremely useful. In a world with no boundaries we'd do both to maximum extent possible. Unfortunately, manufacturing processes for product B do not support both ideas simultaneously, hence a boundary. The company will then spend time and effort to figure out where the "perfect" balance is. Balance is struck by realizing certain conditions: price of components, labor, value, and many other factors. Thus, once enough research has been done, the company adjusts it's manufacturing processes to produce the most of product B while at the same time giving purpose and quality to product B.

The parallels in the programming world are strikingly similar. You want your program to serve a purpose (the purpose is your "product B"). You also want the program to provide incredible speed and equally incredible purpose (and we'll assume quality is part of purpose). However, due to limitations of modern technology, you can not have both to their fullest extent simultaneously. Thus, you must find a balance between the two because you cannot have one or the other: speed alone is nothing without purpose, and purpose is nothing if it is not delivered speedily. That-- the finding of that balance-- is what the pages under this chapter are concerned about.

Topics introduced here will relate to easily understood problems, which will be described as well. Topics and problems include: efficiency of recursion, working with a set of unrelated data, and working with large sets of data, among other things. Problems in programming can often very easily be boiled down to a simple question, yet the simplest question can some times require a lot of thinking to solve.

If you have a topic relating to efficiency that you'd like to see here, feel free to post a comment under this page, jump on IRC and mention it, or even the add the page yourself, if you feel knowledgeable on the topic.

 
 

Drupal is a registered trademark of Dries Buytaert.