This article outlines some of the facets of good and resource-friendly coding in PHP. The following, drawing from my six years with PHP and interactions with fellow coders, should also be helpful to developers who work with other languages, and even make general sense to the non-developer audience who may be looking at hiring a coder.
Does raw performance translate into goodness in coding? Well, resource-friendly coding is one thing. While it certainly is an important aspect of good coding, it alone does not good coding make. We'll move ahead and cover both areas in brief. First, let's get the general goodness covered.
On good coding practice
Think over the structure of the application and the elements required before you start coding to ensure the effective use of your time and code. Even, make dummy files for each element before you start filling them in.
Code with security in mind. In applications that take user input, which is most of the lot, ensure that the input cannot be used to exploit the code in unintended ways. Go over your own script and try to exploit it yourself.
Wherever possible, create replicable patterns instead of hard-coding everything. Use functions for recurring processing events, use arrays and loops for output with recurring patterns. That'll make the changing and the extending of the code much more pleasant an experience in the future. Even, go object-oriented and use classes.
In general, think of the code as something you need to possibly extend in the future, and keep the ends open so as to not have to re-code aspects from a scratch to accommodate future features.
Keep your code well formatted and commented to keep it readable both for yourself and for others. Commenting the code helps you in the long run, as a time will eventually come when you'll need to return to that old chunk of code, wiped off your mind ages ago. And of all things that affect performance, a bit of space here and there isn't one!
The above are a few bits on being a good citizen in the world of PHP development.
On being resource-friendly
While by no means an exhaustive outline, the following are some things that generally need attention, noted in an order of importance in my code lair.
Avoid superfluous MySQL database-queries. Use JOIN to fetch data from across tables into a single query. You can do either one well thought-out query or five queries, and that makes all the difference.
Especially when long TEXT database fields are involved, for example in blog or news entries, don't select * if you need to just show summaries such as headlines - select only the few fields you really need.
If you have an application that takes advantage of dozens of PHP files via include, don't include every file all over the place - use conditional statements to assess which includes are required, thereby skipping both the disk access time and the memory overhead caused by a host of functions, classes and variables.
Don't do the same things many times over. Cache elements that don't change. For example, if you need to loop over an array to output something in several places, just run the array once and store the output into a variable you output every time the desired output is required.
As ought to be obvious from the above, the mere knowing of language constructs doth not a good coder make. Aside memorizing the manual - and whoever in the world did that! - you need to have the ability to envision the big picture, the entire scope of the application, and have a ton of raw logical brain power to generate efficient structures.Good thinking is the foundation of good coding.
Published by Madhavananda
Madhavananda hails from a sacred line of thought called Gaudiya Vaisnavism, its roots in medieval India and Sri Caitanya's way of divine love. A student, practitioner and teacher of the heritage, he spends t... View profile
Foreign Language Skills and the International Job MarketSeveral speakers from the UN, Italian television, and the NYC academic and business community gathered to discuss both the importance of foreign language skills, and the opportu...- The Pimsleur Language Learning SystemReview and guide to Pimsleur Language programs.
- Environment Dominates a Bilingual Speaker's Choice of LanguageIf a person is truly bilingual, meaning that he reads, writes and speaks two (or more) languages, his environment will determine which language he uses even more so than his native tongue.
- Why is Happiness so Important for Our Good Health?Happiness is very important to our daily lives and good health. It can be achieved and is not elusive. Where an individual live will matter because different factors affect the capacity to achieve happiness. Happiness...
- Peak Performance: Good Habits Start EarlyThe major focus of the article is that it takes more than just hard work to become a great athlete, that you must develop good habits from an early age including discipline and hard work ethic.
- Teach Yourself a Foreign Language
- Self-Study of a Foreign Language
- How to Discern a Good Movie
- Bennigan's Restaurant Review: Food was Good as Always, Service was Below Average
- Language and the Cerebellum
- Use of Language in Shakespeare's Much Ado About Nothing
- The Da Vinci Code Dazzles the Box Office
- Good thinking is the foundation of good coding.
- Develop coherent structures, build routines to automate tasks.
- Avoid processing redundant data to optimize performance.





1 Comments
Post a CommentYou say, "use conditional statements to assess which includes are required..." That's very beneficial advice as is your suggestion to cache things that don't change. Thanks for this article.