Follow Coding Standards

May 17th, 2007 by rvdavid Leave a reply »

Being a self-taught beginner at one stage, I understand that code formatting and other concerns NOT regarding syntax is taken for granted, I mean, when you pick up your first programming language, the one thing you want is for your application to work, heck, when some beginners first started programming PHP, they have no idea what a “coding standard” is let alone it’s existence and haphazardly hack away line for line at their scripts without regard.

This is expected tho’, you’re only beginning to pick up the syntax and you feel invincible and could stay up for days without sleep due to the exciting progress you are making. I speak from experience, I myself have done it. Connect to mysql, copy and paste some queries and display some dynamic content on a page. No problem! Great! Who cares what it looks like? “Nobody, as long as it works.”

Then came the maintenance side of things. It was hard to keep track of what was happening. An example of a problem I was having was when my 1000+ line php script had a bug somewhere and I did not know where it was, so I had to follow my left align formated, quirky variable named, 1000+ line “ball of mud” to finally locate that I had an extra concatenation operator where I shouldn’t have had- 3 hours and a bunch of tests later.

Here are some of the problems I had experienced in my earlier days of writing code without “coding standards”:

  • No set structure and formatting – I was getting lost in my If else statements.
  • Meaningless variable names – My variables had no logical meanings for instance I had $fa and $sum as variable names.
  • Inconsistent casing – So whenever I had to come up with a new variable, I had to think about representing spaces as underscores, camel case or none at all.
  • No documentation – So when I had to come back to code I was working on a few hours later, it all looks foreign to me. I have to retrace steps.
  • Miscellaneous inconsistencies – This goes from SQL queries, indentation (when I was bothered), output methods, php tags etc.

Most of these sorts of problems with inconsistency are solved by “coding standards”. It’s basically a set of rules that you and/or your team follow to the letter when writing up your PHP applications. A “coding standards” document is like a “rule book”, if you will, with agreed guidelines on naming conventions, code layout/formatting and other rules which must be followed to the letter.

I liken it to the formatting guidelines of a book. If you pick up a book, you’ll find that the style throughout is consistent – although books differ from one another, a book would have a consistent styles and measurements for text, margins, illustration styles, position of page numbers chapter headings, headers and footers. applied throughout it’s pages. Could you imagine how hard it would be to read if you had different styles on each page?

Same goes for your PHP Scripts. “Coding Standards” for your scripts and online applications give you guidelines for variable naming, function naming, class naming, indentation, SQL formatting and other miscellaneous issues such as documentation, handling braces and common rules to name a few.

Coding Standards are good because it smooths out the coding process by making it so that you don’t have to think about or consult each other for the things mentioned above.

Once you have the syntax down pat and have finished jumping up and down (maybe even “during”) marvelling at your new found skills in PHP, you can either form your own coding standards, or follow one of the following existing coding standards published on the following links and make modifications as you see fit:

  • Zend Framework PHP Coding Standards – I recommend and use this one as it matches the requirements of a framework I’ve developed.
  • PEAR Coding Standards – I tried to use this coding standard, but then, discovered a PECL Coding Standards document on the appendix of a book. Unfortuately I can’t find a link to that, however I’ve read somewhere that PECL uses the PHP Coding Standard – which I can’t find a link to either.
if you enjoyed this post, make sure you subscribe to my RSS feed!
You can also follow me on Twitter here.

No related posts.



  1. Diah says:

    Being a self-taught…No give up if found some problems.. Keep it up..Thanks for Sharing!

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.