Program to Perform from the Start, Do not Leave it until the End

Date Published: 15/05/2009 12:37

I have recently spent a lot of time working through code written by my co-workers, in an attempt to add functionality to sites which were built some time ago. Its only natural for projects in web development to deteriorate after a period of time, they're usually built to very tight schedules and often don't last long before they are rebuilt or rewritten. This however is no excuse for poor programming practice and even though our more recent work is to a much higher standard, our back catalog is poorly maintained. There are obvious things to work towards ofcourse, such as documenting and structuring your code properly but that's not what this feature is about. Here a few things all developers should think about whilst developing to improve the performance of their code whilst also assisting readability in the long run. Performance considerations should not just be something we do at the end of a project.

File System Access

<?php

include("libraries/htmllib.php");
include("libraries/xmllib.php");
include("libraries/postgrelib.php");
include("libraries/mysqllib.php");
include("libraries/utils.php");

?>

Reduce the number of file system calls you make while executing your code as this can easily be a bottleneck at run time. Consider whether some of your includes can be combined into less files or whether they really need to be included at all.

Refine Database Calls

-- Instead of selecting all ...
SELECT *
FROM tblUserData

-- ... only select the data you need.
SELECT strFirstName, strLastName
FROM tblUserData
WHERE bitUserActive = 1

Only selecting the information you really need not only reduces the amount of data you hold in memory but it also reduces the communication between the application and the database server. Requests for large amounts of data are going to take much longer to pass from the database to your application, at times when traffic is high you'll want to decrease this transfer as much as possible.

Close all Database and File System Connections

<?php

$db = mysql_connect("localhost", "mysql_user", "mysql_password");
/*
Interact with Database
*/
mysql_close($db);

?>

If you have finished working with your file or database, close the connection to ensure that no resources are wasted and it is not locked. In PHP closing a database connection isn't necessarily necessary but its always good practice to close your connections ensuring that any connections opened up later on do not get confused.

Use Switch Cases as Opposed to Massive If Statements

<?php

switch($var){
   case 1:
      echo "One";
      break;
   case 2:
      echo "Two";
      break;
   default:
      echo "None";
      break;
}

?>

Where possible use switch cases instead of creating massive if statements. The performance gains for this one are very slim, so much so that newer languages such as Python do not have a direct equivalent to the switch case but it is important for code readability.

End Loops as Soon as Possible

<?php

$names = array("Bill", "John", "Oliver", "Michael");

foreach ($names as $name){
   if ($name == "Oliver"){
      echo "Name is Oliver!";
      break;
   }
}

?>

On many occasions I have seen code with loops which run beyond what they were used for in the first place. Always break your loops as they can significantly effect the running time of your code. If the example above did not break and the array had a few thousand entries it could be looping long after the name has been found. Most people will say this is a simple one and is something they naturally deal with, I would still suggest checking your code as it is more common than you think.

Searching Forwards or Backwards

<?php

$string = "This is a string where the thing I am looking for is at the end";
$end_pos = strrpos($string, "end");

?>

When searching through strings and arrays check to see which direction the search is best done in. If you were parsing an XML document looking for the XML version it would make sense to start at the beginning as it should be at the top whereas if you were in search of the last closing tag you should start from the back. Always give consideration to how the function will be used most and program it accordingly.

Take Care Using Regular Expressions and String Functions

Dealing with strings is one of the most intensive actions your code will carry out therefore its very important to take care when doing it. Make absolutely sure you require a regular expression before using one as they can seriously effect execution time. Just think to yourself when using them whether there is a better less intensive way to do the same job.

Destroy Redundant Variables

With modern programming environments this is less of a consideration than it was previously. With garbage collectors and they likes taking away pressure from us programmers allowing us to get on with more important things it is easy to lose this element all together. Most of the time its not too much of a worry but if your code is quite memory intensive take a look to see where the big variables are and remove them as soon as they have served their purpose. It may seem a trivial thing but a redundant variable in memory is memory which could be used elsewhere.

Conclusion

These are just a few things which need to be considered when writing streamlined code. These also differ between languages so there may be some here which don't apply to certain environments. There are ofcourse many more and if anyone has one or more they think I should add comment below or contact me and I'll happily add them in.

Comments

Sorry comments are currently disabled for maintenence

5 Most Recent Articles

Manually Triggering Events in ASP.NET from JavaScript

A quick guide for ASP.NET developers on how to manually trigger ASP.NET events from JavaScript.

Advanced Use of MySQL Stored Procedures

An article for users of MySQL databases describing how they can use advanced stored procedures to improve efficiently in their applications.

Using MySQL Stored Procedures and Extending MySQLi in PHP

A guide for LAMP developers to using stored procedures in MySQL and extending the MySQLi class.

Reading and Writing to Excel Spreadsheets in Python

An introduction to using the xlwt and xlrd modules for python to interact with Microsoft Excel spreadsheets.

Interact with the Web Using Python and the HTTP Library

This is an introduction to making HTTP requests from a python script/application using httplib.

Sponsors