Personal Programming Faux Pas & Standards
At risk of starting a holy war, I’ve noticed myself cringing every time I look at a block of code that isn’t formatted, or consists of slow functions… I decided to write a brief list of my pet peeves. Feel free to add!
Formatting
I prefer to use tabs (\t) rather than spaces to indent my code. Nested loops, decision blocks et cetera should all be indented with one tab per indent. Software such as the Zend Framework requires four spaces. This is completely up to you.
Optimization: Loops
One of the biggest issues I have with my fellow developers is not paying attention to optimization within loops. Consider, for instance, this example:
Always try to optimize your loops if operations are going on at the comparing part, since this part is executed every time the loop is parsed through. For assignments a descriptive name should be chosen. A correct example might be:
Now, take it one step further:
This is a minor detail, but pre-decrement (and increment) operators are faster than post-decrement (and increment). Post requires the compiler to create a duplicate of the variable and copy the updated value back at the end, while a pre-operation can be done in-place immediately.
Optimization: in_array();
Avoid using in_array() on huge arrays. Make an effort not place them into loops, especially if the array being evaluated consists of more than 30 or so entries. in_array() can be very CPU intensive. Instead, try using isset() on the arrays keys instead. A call to isset($myArray[$var]) is a lot faster than in_array($var, array_keys($myArray)).
Space your tokens
I loathe trying to sift through code I can’t read; it should read like the English language (yaknow, top to bottom, left to right?) Always leave one space between the tokens. Don’t put spaces just after an opening bracket or before a closing bracket. Don’t put spaces just before a comma or a semicolon. Again, just my personal preference, but I’m used to browbeating my subordinates. It’s the only way they know me as the Omnipotent Dark Lord.
Operator precedence
There is an exact precedence to all the operators in PHP, but I don’t recommend trying to guess them all. Use brackets to force the precedence of an comparison or equation so you know what it does.
Ternary Operators
Try not to sacrifice readability by saving yourself a few keystrokes. Ternary operators should generally be used for comparison, not complex logic.
Code Formating: If/Else
Braces must always be used. Braces always go on their own line. The closing brace should also always be at the same column as the corresponding opening brace.
Switch Statements
Switch statements are extremely useful, but can get a fair bit long. Akin to in-line braces, do not indent breaks.
In Closing…
These are just my personal opinions, based on my own ideologies. Everyone has their own preference on how code should look, but I do recommend paying special attention to the optimization tips.
Suggestions, comments and death threats always welcome!
Fanatic programmer, obsessive technophile, serial entrepreneur and Web 2.0 enthusiast. Zed23.com is the personal blog of Ryan Brooks, a Web Application Developer out of Calgary, Alberta.
Vincent
April 30th, 2008 at 1:18 pm
I like the break; non-indentation. Pretty logical when you think of it, but I never did :P