Posts Tagged PHP
6 Ways to make your PHP code more maintainable
Most of us have inherited projects from other developers, or have revisited old code that we’ve written months or years beforehand. I’ve personally seen code that looked like it was written by a 3rd grader. Hopefully none of you will have to deal with code written this poorly. Here are a few ways to make your code flexible, and maintainable so that you, or some other developer won’t be cursing you.
I’ll assume that if you’re reading this article that you’ve written PHP code before, and that you’re fairly familiar with the constructs of the language. But, for the sake of being thorough, I’m going to mention a few things.
1. Comment!
Make sure you’re commenting. I recommend a header comment to describe the file: The Author’s name, a description of the script, and a listing of your founding assumptions are all good things to include in the file description. Also if you write some code that is particularly complex or important I recommend commenting it aswell. Some sticklers will even recommend commenting every function (including function parameter descriptions). I don’t feel that this is necessary but if you’re writing a massive application or an application created by multiple developers than this will be very helpful.
2. Use Functional Programming
I can’t believe I have to say this, but, use functions. If you write a string of code more than once it should be turned into a function. But, be careful to avoid writing several functions that do similar things, if two functions could be consolidated into one in an elegant way I would recommend that. remember the less code you have to look at in the future the easier it will be to understand what the application is doing.
3. Split up your files.
If you find that your script is very long try splitting it into multiple parts. Also, if you have certain portions of your code that are used on multiple pages make sure that this part is included in an external file for easy maintenance. If one page has several modular parts make sure that they are all contained in external files.
4. Make sure you’re using directories.
If you’re working on a large project, it’s very important that you organize your files into logical directories. There’s nothing worse than a completely flat file system. I’ve seen projects where there were hundreds of scripts (with nondescript names) in the same directory. Do yourself a favor and organize these files. It’ll make finding things later much easier. I usually find that it’s best to organize my files into Models, Views, and Controllers. Model are scripts that describe data objects (classes). Controllers manage interactions between user actions and models and views give information to the user (pages, ajax responses, RSS feeds, RESTful API responses, SOAP pages etc.).
5.Don’t create variables where you don’t have to.
If a function returns an iterable object and all you need to do is perform an action on that object using a loop, don’t create a dedicated variable for it. For example say that I want to iterate over an array that is returned by a function. Instead of:
$arr = functionReturningArray();
for($arr as $element)
do some action($element);
try:
for(functionReturningArray() as $element)
do some action($element)
this is a much more concise way of performing an action.
6. Object Oriented Programming
If you’re not familiar with it already read up on Object Oriented Programming. OOP allows you to create objects that contain all the data and functions to represent a physical object or idea. This is very helpful for large project where you want to compartmentalized your code into distinct pieces (easier for debugging easier for understanding). This approach almost always yields less code, and if done correctly can yield reusable code for other projects.
7. (Bonus) Don’t Be Lazy
One of the main sources of sloppy code is laziness, remember that just because you know what you’re trying to accomplish with your code doesn’t mean that others will. Take the time to do it right, because sooner or later almost all code needs to be revised, write it correctly the first time and you’ll thank yourself later.
Regards,
Adam Haney
