better habits for database-driven web-apps
by Derek Sivers
Related link: http://www.sebastian-bergmann.de/en/phpunit.php
I've finally gotten into some better habits for my database-driven web-apps. I'm trying to get myself out of years of spaghetti-code.
#1 - Make a shared directory where ALL common classes for this project will go
(example: Client.php Customer.php Item.php Invoice.php etc)
#2 - Create a CVS project for it immediately
#3 - Create a /tests/ directory inside of it, and make a PHPUnit2 test file for each class, and a unit test for each method.
#4 - Make ALL parts of the project (the front-end, the back-end management, the shell-scripts, the members login-area) ALL use the exact same shared classes.
#5 - No SQL anywhere outside the shared classes - any interacting with the database HAS to be done through the classes. Many times I'm tempted to just pass a simple one-line SQL command right in my code, but I resist the urge (remembering years of mess this caused me before) - and keep everything centered around the shared classes.
BEFORE I start in with the web-interface, I make many of the methods/functions I know I'll need, and test them at the unit-test level at a terminal/shell. It's always nice to know that your guts are in good order.
Funny thing is... now that that stuff is out of the way, I'm spending seemingly all my time just on the web UI interface! Damn I wish I had a good re-usable framework for all these web-apps. More on that in my next post.
other good habits?
One habit I got in recently is to spend more time looking at (and thinking through) the details of my user interface before coding. Until recently, I had always accomplished this by throwing a mockup together in Photoshop, or using a crappy WYSIWYG HTML editor to do a slap dash mock up. Perhaps you already have a method you use for this - your sites are always really usable, so I imagine you must put a lot of thought into it.
For testing PHP, I highly recommend Apache-Test. You can read more about using Apache-Test to test PHP applications here:
there's probably a kajillion frameworks but i know this one is worth looking at:
The original fusebox framework was developed for ColdFusion but they have a PHP variant which I understand is just as popular.