PHP Debugging Tutorial - Coding Style Primer
(Page 2 of 5 )
First thing you need to think of is your own coding style. A coders coding style is a very personal and there are thousands of programmers out there all claiming their way is "the best".
NOTE: The only reason i'm mentioning these things below is so that people who are not aware of the importance of coding style will find out. Having a fixed coding style will help you a heck of a lot when debugging your php code, which is why i'm mentioning it here.
Here are a few things in your coding style that I generally feel is good practice all round :
Comments
<?php // Always comment your functions - even if just with a short description function commentedFunc1() { }
/* Some prefer block comments */ function commentedFunc2() { }
/* But i usually use block comments if i need a lengthy description like : { Because this way indent folding + auto-indenting in jEdit works quite well. } */ function commentedFunc3() { }
// You should generally comment blocks of complex/messy code with a line or two. for ($c1=0;$c1<(floor($height/10));$c1++) { $ratio = $c1*RATIO_CONST; for ($c2=0;$c2<$width;$c2 += $ratio) { // ... // code // ... } }
// Commenting every single line is overkill and they tend to get in the way // and nearly impossible to maintain the block look correctly : for ($c1=0;$c1<(floor($height/10));$c1++) { // Loop the thing $ratio = $c1*RATIO_CONST; // Do the ratio maths for ($c2=0;$c2<$width;$c2 += $ratio) { // Loop the width // .... // Do something else // code // More stuff // ... // Etc. } // End loop $c2 } // End loop $c1
// I generally don't comment the end of loops either, since when you copy and paste it and // modify the code somewhere, you tend not to alter the comment to reflect the changes // properly.. that gets messy. ?> |
Naming conventions
You need to grab a naming convention from somewhere so that you'll be able to remember your function/variable names far easier. Decide whether you are going to use plurals in your names, whether you're going to prefix all your variable with their type/context/usefulness/etc. But when you have, try stick to it.
Obviously, if it's a mission sticking to your naming convention then you've chosen a far too complex one for what you need. Remember: PHP is sticky about uppercase only for variables and constants(I think), not functions. Here's the php naming convention that I personally use :
<?php // I lend a lot of the naming conventions from Java since it made more sense. $variableNumberOne = "this is a normal variable"; $postedvariable = "I use all lower case for POSTed/GETted variables. always.";
// Common posted variable names : $action = "I'd have a switch at the bottom of my php page to use $action to tell my page what to do"; $conf = array("i generally have $conf as a POSTed array containing all the data from a form"); // This means i can declare $conf global in a function (or assign $conf=$_POST["conf"]) and not // have to worry about declaring hundreds of variables.
// I use the same convention as java for function names, even if it doesn't matter in php // because it's more legible. and i don't have time for underscores function functionName() { }
// I use all caps and underscore to seperate words in constant names define("INTERESTING_CONSTANT",1); ?> |
One last thing. INDENT YOUR CODE! PLEASE?! Code that runs alongside the page is completely illegible and it is horrible to try and debug.
Ok, enough of coding style. The rest (things like whether to put braces on newlines, etc.) does not really pertain to keeping your code easily debuggable, so I won't delve into it..
Next: Common Errors >>
More Miscellaneous Articles
More By Codewalkers