PHPBuilder - A Test To See If You Write Sloppy Software

RSS Twitter

A Test To See If You Write Sloppy Software

by: Tim Perdue
March 10, 2003

As a freelance programmer, I have built quite a few web applications over the years, some pretty and some not-so-pretty. Before I got involved in PHP, I had worked mostly in Java, with its rigid and forced programming style. With PHP, of course, it's easy to get lazy and write sloppy code, and PHP will always perform well no matter what. Recently, however, I took over maintenance of a web application which set new lows for maintainability, quality, and structure. As I set about cleaning up this application, I took a few notes on what its most glaring problems were, and have devised a little quiz for you to take.
Most of this -- 99% of it - is old hat, or common sense, to many programmers. For others who are just starting, it may be useful. Certainly for the person who wrote the web application I took over, this would have been a valuable quiz.
The Barest Basics of Writing Decent Software:
  • Code Structure: Creating a directory structure, config file, naming standard, commenting, and formatting.
  • Error Handling: Checking every database query and properly displaying errors to the user.
  • Database Structure: Using a relational database properly.

Configuration and Directory Structure

One of the most outrageous problems I encountered in this application was the hard-coding of database username/passwords on every page throughout the site. If you ever need to change your database password, you would have to change every page. The simple solution, of course is to have a config file, which is included on every page. Give yourself 1 point if you have a proper config file.
In my applications, I create an "include" directory (it can be anywhere), and in that include directory, I place a file called "pre.php" that includes a config file, a database abstraction layer, and the template for page header/footer. It's then a simple matter to include "pre.php" on every page and inherit its functions.
Logical sub-sections of your application should be in their own directories. If you ever have more than 10-15 files in one directory, it's a sign you need to improve your directory structure. Give yourself 1 point if you have fewer than 15 files in each directory.

Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Tim Perdue



(Maximum characters: 1200). You have characters left.