Naming Standards, Commenting and Formatting Your Code
It’s valuable to have a naming standard for the functions you write in PHP. If you aren’t using
OO-style PHP programming, you can quickly run into name-space problems. I generally start my
function names with the name of the sub-section I’m in. For instance, if I’m in the “bug tracker”,
all my files are in the “/bug/” directory and the functions start with “bug_”. Bug-tracker-specific
tables in the database would also start with “bug_” for clarity. Give yourself 1 point if you have a
rational naming standard.
OO-style PHP programming, you can quickly run into name-space problems. I generally start my
function names with the name of the sub-section I’m in. For instance, if I’m in the “bug tracker”,
all my files are in the “/bug/” directory and the functions start with “bug_”. Bug-tracker-specific
tables in the database would also start with “bug_” for clarity. Give yourself 1 point if you have a
rational naming standard.
PHPDoc, which is based on JavaDoc, has been adopted as the standard for commenting and documenting
functions in PHP. I use PHPDoc on more elaborate projects, but I generally just use short comments
for tight-budget client work. Give yourself 2 points if you use PHPDoc or 1 point if you use standard comments.
functions in PHP. I use PHPDoc on more elaborate projects, but I generally just use short comments
for tight-budget client work. Give yourself 2 points if you use PHPDoc or 1 point if you use standard comments.
I also encourage you to take pride in formatting your code. I always use plenty of braces
(“{” or “}”) in my code and parentheses wherever it makes sense. Braces are not strictly
required all of the time, however the idea is to make your code absolutely clear and obvious to anyone who
takes on its maintenance after you part ways with it. I also use appropriate vertical and horizontal (tabs)
white space to make things clear. Here is an example:
(“{” or “}”) in my code and parentheses wherever it makes sense. Braces are not strictly
required all of the time, however the idea is to make your code absolutely clear and obvious to anyone who
takes on its maintenance after you part ways with it. I also use appropriate vertical and horizontal (tabs)
white space to make things clear. Here is an example:
<?php
if ($test) {
//do something
} else {
if (
$foo == 123) {
//operation here
} else {
//other operation
}
}
?>
Notice the extra returns and generous use of tabs to space things out for clarity.
Few people will struggle with understanding instantly what you are doing here. Give yourself 1 point if you
are generous and consistent with your braces and white space.
Few people will struggle with understanding instantly what you are doing here. Give yourself 1 point if you
are generous and consistent with your braces and white space.