Doing it Right from the Beginning
Posted Mon, Dec 17, 2007 in:My coworker Scott Martin recently posted a list of things to do from the beginning of a project..
I had planned to post something along those same lines myself, but I guess he beat me to it.
I’ll throw in my two cents with a much shorter and vaguer list, with my comments and a few extra items as well. This will make a lot more sense if you read the list at the above link first.
- Internationalization + Smarty
These two items go together. Basically, there shouldn’t be any text which makes its way to the users which comes from anything but a template file. Seriously, don’t put text in your code.
- Code standards
Here’s a secret: what your standards are really doesn’t matter. It’s the fact that you have standards that matters. It’s really annoying to read code with two different layouts. Pick one and stick with it.
- Code generation
I disagree with Scott about code generation being bad. The one thing that needs to be done for it to be okay, however, is for it to be generated, and updated, automatically. The first code you should write isn’t the code to generate the code, but the code to call the code to generate the code when the source of the data used to generate the code is changed.
For example, if you are reading in one file to generate another, then before you read in the file, run some code which checks the last modified time of the generated file and compares that with the last modified time of the source file, if the source file is newer, regenerate.
If you feel so inclined, also check the timestamp on the code which generates the file, and if it is newer than the generated file, regenerate also. This will get rid of the instance where you change the generator code but don’t change the input file.
Alternatively, if you have some sort of interface or tool which generates the input file, have that tool run the script to update the generated file.
- URL rewriting
As we all know, Cool URIs don’t change. So make some sensible decisions at the start of your site design, and stick with the URLs from there on out. All you need is a single script to redirect all URLs to (except images and CSS and things), which then delegates the work out to some actual PHP files. This is also helpful in that it makes it so you don’t have to have all of your PHP files inside of your document root, just the one that does the routing, which brings me to my first new rule…
- Keep non-documents out of your document root.
The fewer things you actually have in your document root, the fewer unexpected security holes there will be. If a file is not going to be directly accessed from a web browser, don’t put it where a web browser can get to it. All of your configuration files and almost all of your code should be kept out of your document root.
A structure like the following works well:
application root
- htdocs - static HTML files, images, CSS files, a single code file to load in other files based on the requested URL
- src - all of your actual code. Set your inclide_path to here and keep everything neatly organized into subdirectories
- templates - Smarty, or whatever templates you are using.
- cache - whatever sort of disk cache you need. This includes Smarty cache, RSS feed cache, etc…
Don’t Repeat Yourself (DRY)
This is basic stuff right here, but it can never be said enough: Don’t write code that does the same thing as code you’ve already written. Or if you have to do so, then combine the two pieces of code together. It is a huge pain when you have to figure out why something is working the old way when you already changed the code that does that thing to do it a new way.
If you are copying and pasting code, then something is probably wrong.
- Don’t Repeat Other People/Don’t Reinvent the Wheel
This one is a bit different. What I mean by this is don’t write any code that you don’t have to. If somebody else has a library that already does what you want, use that, unless there is a very, very compelling reason for you not to. If a library has licensing issues, or is really slow, or doesn’t fit into your platform somehow, it might makes sense to write it yourself, but first check to see if there’s already something else which meets your needs.
Spending an hour searching something is going to take less time than writing it yourself.
What guidelines do you follow from the start with every project that you do?