What makes a good PHP programmer?
This is to some degree an impossible question in that a good PHP programmer is perceived differently by different people. However here is what I look for in a good PHP programmer.
Structure
Code must be well structured right from the start. Object Orientated Programming does not guarantee a good structure although to some level it does help.
My definition of a good structure is that commonly used functions are all in one place or properly named includes in a single folder, routines that are commonly executed are all in one place or inside properly named includes or folders. Each page is then broken down in to about 6 sections from top to bottom and are the same on each page that can be accessed directly by the browser.
Section 1
Load the required routines and functions the page will need
Section 2
Print the first half of the template of the site
Section 3
Load functions and other code specific ONLY to that individual page.
Section 4
Process and print any HTML that is not part of the sites template for example a contact form, or the homepage.
Section 5
Print the second half of the sites template
Section 6
Unload the page by disconnecting to any databases, recording any last minute statistics.
Consistency
Keeping code as consistent as possible makes it much easier for the next programmer and yourself to understand the code and make reading it more enjoyable. Do not go switching around the structure of each page as this would not be consistent and would confuse even the best programmers!
There are often many ways of doing the same basic tasks, however which ever method you choose to use always make sure you only use that method as much as possible. I have done two examples below:
If ($form_sent) {
}
Or
If ($form_sent == true) {
}
I know it is a very basic example but it would not show consistency if you switch between the two if conditions. Other examples would be using mysql_num_rows to return the total rows or using COUNT() within an SQL query. Again which ever method you use, stick with it!
Just thought of very good example of consistency! Variable names! There are many types of naming variables such as Cammel Case ($varName) or the method I prefer is keep all letters lowercase and separate with an underscore ($my_name). Again which ever you choose make sure you stick to that same naming convention throughout the code.
Comments
This is VERY important! If you leave comments on your code it can make even the messiest of code easy to understand! Try to stick to remarking every 2 or 3 lines of code, or a block of code using /* and */
Do not over do your comments on your code, but do not feel afraid to say exactly why and what a line or two of code is doing. Better to have more comments than not enough!
Comments are not only extremely useful for other programmers that are having to work on code you have written. They are also very usefull for yourself when it comes to reviewing your code at a later date to fix bugs, make modifications or even take a chunk of code or function to use somewhere else in another website or script!
An example would be if you have a bug in your login script because it is not redirecting properly and you have commented a block of code that is responsible for redirecting the member to the correct page. You can instantly find the code that you need to check through to fix the bug!
Here is an example of a code block:
//==============================================
//=== PROCESS LOGIN DETAILS
//==============================================
Login script here
//==============================================
//==============================================
//==============================================
Feel free to make up your own commenting methods but again make sure you use a consistent method of commenting.
Portability
Always use <?php and ?> when creating a block of PHP code. NEVER use shorthand which is <? and ?> because shorthand does not work well with XML and porting your code across to different servers may cause problems if ASP and other languages are installed on the server. Shorthand tags would then confused the server and break the script!
Duplication of Code
Always try never to duplicate any code. Even if it is a complete copy of a block of code pasted on two different pages with a couple of modifications. This should be avoided since if you change a major part of one block of code, you will then have to change the code you copied to the other file. If you forget, that page will be broken!!!
Always encapsulate code and never duplicate any code! Even if it means putting code in to a function and putting that function in an include file using a switch() to select the correct code for the correct file. It is much better than simply copying the code in to two separate files.
Object Orientated Programming (OOP) is a coding technique that has been created to help prevent code duplication and other problems in many ways by using methods such as encapsulation, inheritance, and so on.
What would you rate this article?Please login to rate and upload coding articles. To start registering a free account with us, click here..