Coding standards are critical

January 19th, 2008

Programming on the World Wide Web

Odds are, if term coding standards is new to you, you are wasting time on a daily basis arguing over the syntax semantics of your programming language. (e.g. tabs or spaces? 4 spaces or 2? print or echo? etc.)

Programmers are required to make hundreds to thousands of micro-decisions like this every day, and especially when there is no standard, this costs time and time is money. If you were to benchmark the programmer's brain, you could begin to see exactly just how much time is wasted asking, answering, remembering, and changing each answer.

This form of unorganization has many more side effects than one, however.

Good code works; better code is also easy to read, maintain, and debug.

According to Sun Microsystems, 80% of the lifetime cost of a piece of software goes to maintenance, and hardly any software is maintained for its whole life by its original author.

There's nothing worse than inheriting a project that requires a lot of your time to decipher. It should not require the original author to translate. What if that author were to die or simply become uncooperative? Frequently, this is the sort of coercive survival technique novice programmers resort to when their job is threatened. Even more frequently, it becomes the primary cause of frustration for new programmers. The novice instinct is to throw everything out and start over from scratch--do not do it!

Good programmers rewrite; better programmers reuse.

The real problem is the lack of coding standards. Insist on them with every project.

What can coding standards do for me? 

  • Standardized code is easy to document. (e.g. Doxygen, JavaDoc, PHPDoc, etc.)
  • Standardized code takes care of easy decisions for you, leaving you free to concentrate on the real work.
  • Standardized code reduces friction in collaborative group development.

So what do you recommend?

The set of standards can vary by language and by project. It doesn't matter what they are, as long as they are there. In PHP I recommend the PEAR Coding Standards. These are the same set of standards used by many large open source projects, such as Drupal. Examples include:

  • Indent with 4 spaces; no tabs.
  • Always use <?php ?> to delimit PHP code, not the <? ?> shorthand.
  • Class names should always begin with an Uppercase letter.
  • Constants should always be all UPPERCASE.
  • etc.

Of course, every programmer starts out with their own quirky way of writing code. In high school, my C++ teacher used to pride himself on being able to detect cheaters (i.e. plagiarism) in class... He knew when I was helping my friends because he could recognize my work. Even though the program looked the same on the outside, on the inside the code was unique and personal--like poetry. I find it interesting now, when working with teams of developers, that now two of us can write code to accomplish the same goal--and the resulting code is nearly identical.

Great minds think alike.

Thanks to coding standards!

Posted in:

4 Responses to “Coding standards are critical”

  1. Richard K Miller (not verified) Says:

    I agree with you on tabs and markup case. The other big questions for a PHP developer: * UPPERCASE, lowercase, camelCase, or under_scores for function names and variables? * Does the left brace ({) go on the same line as the if() and while() statements, or on the next line?

  2. Muhammad Haris (not verified) Says:

    "Function names should always be in camelCase."

    It is not necessary to use camelCase.

    According to Drupal:
    "Functions and methods should be named using lower caps and words should be separated with an underscore."

    You should include more examples for beginners.

  3. mikesmullin Says:

    Thanks Haris! Good point; I have updated the article to reflect it. I appreciate your feedback.
    Readers can follow the link to the PEAR Coding Standards to see even more in-depth examples coupled with explanations.

  4. Andrew Young (not verified) Says:

    I'm a big fan of Java's coding conventions personally. They're actually on the Sun Certified Java Programmer exam.

    http://java.sun.com/docs/codeconv/

    (Some of the text of your article is from this page, so I assume you already have a link to it.)

  5. Post new comment

    Please type the letters you see in the picture below.

    Image CAPTCHA