Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > DEVELOP ROCK SOLID CODE IN PHP: PART 1


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Develop Rock Solid Code In PHP: Part 1
By Amol Hatwar - 2004-01-02 Page:  1 2 3 4

Write rock-solid code

Given that the requirements of your code do not change, you should not find the need to change code significantly. Except for the occasional need for optimization and refinement, your code should run like a well-greased machine.

Sounds difficult? It's not. Frankly put, writing rock-solid code doesn't require a genius. You just need to ask yourself the right questions when in doubt so that you don't stray off-track:

  • Is it secure?
  • Is it simple and comprehensible?
  • Is it platform-independent?
  • Is it fast enough?

Secure your code

Any system that caters to a large audience must be secure. Though PHP itself has been inert from attacks by black-hat hackers, one is never too certain. PHP 4 prior to version 4.2.2 had serious security flaws. Always make sure sensitive data are stored or transmitted on a network with proper encryption. This is more important for applications that transact business, store information like credit-card numbers, and transmit confidential data.

Today, trusting data that users submit is difficult. Make sure that you validate it and ensure that it is clean before using it. Keep in mind that putting your Web application on the Internet exposes your system, your software, your data, and your business to a huge network.

Ensure that your code runs securely at all times.

Keep it simple

Your code should be easily comprehensible, read well, and be well documented. To reduce the time needed to familiarize yourself with your own or others' code, maintain a common naming and coding convention throughout a project. Investing time to ensure these things pays off when the need to maintain code arises.

Documenting code is better done while you are programming it. Tools are now available that can parse all your scripts and make neat looking documentation in HTML for you. If you change the behavior of the code, change the documentation accordingly. Having documentation about code that does undocumented things is of no real use.

Ensure that your code is documented, simple, and easy to comprehend. It helps in the long run.

Make it platform-independent

Another issue that you must address is platform-independence. Of course, scripts written for PHP on Windows will work on PHP on any other platform: PHP is designed that way. However, you still need to take care of minor inconsistencies. The new line character, for instance, is represented differently in Windows and UNIX.

You must also use abstraction when accessing resources like databases that are external to PHP. Say your application uses MySQL as a database server to cut costs. If you decide to have a more feature-rich database later, you will have to change the code in your application. Making massive changes to code is always a drudgery and an error-prone process. Using abstraction isolates changes to parts that can be easily changed. You do not have to redo the entire application.

Ensure that you write platform-independent code. It makes your application more adaptable and scalable.

Build for speed

The last factor worth tackling is speed. Nobody wants to wait forever as your script tediously selects 300 or so entries from a database and displays the results on a page. Putting 20 results on 15 different pages that travel and load quickly on the user's browser is always a better idea. The user perceives the faster response-time as speed. Another easy mistake is to create pages dynamically each time a user visits a page. This does ensure that your site stays fresh, but PHP isn't too forgiving when the number of users increases. You should cache frequently served pages. Caching makes your application faster and reduces the load on the server.

Ensure that your code runs fast. Nobody likes waiting.



View Develop Rock Solid Code In PHP: Part 1 Discussion

Page:  1 2 3 4 Next Page: Summary and Resources

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.