Skip to content

Code Readability, Part 1

by Shawn Stratton on January 18th, 2009

The sim­plic­ity of PHP can some­times be over­whelm­ing, with so many dif­fer­ent ways and approaches
to cre­at­ing solu­tions for our prob­lems we some­times lose the abil­ity to think past the cur­rent prob­lem
and into the future. I some­times feel over­whelmed think­ing about the fact that what I write today could
still be run­ning 15, 20, or even 30 years later (yes legacy code refuses to die, now and cer­tainly later,)
so when I revisit the code I wrote a few months ago and look over it from time to time I still have to say
“What the hell was that guy think­ing” not real­iz­ing that I was the author.

As such I’m plan­ning on writ­ing a short series on things to aide in code read­abil­ity and this is the first post of the series.
Where I work we have estab­lished code style and nam­ing con­ven­tion guide­lines, as many PHP Shops have, how­ever we don’t address all
issues that crop up from time to time. Today I’m going to try to address some quick ideas that most of you will already be famil­iar
with, things that will aide in the gen­eral read­abil­ity of code for future generations.

Com­ment­ing Code:

Where should com­ments go and how many should there be, I con­stantly face this deci­sion, rang­ing from sim­ple meth­ods to full
blow con­trollers, what needs to be doc­u­mented and what is under­stand­able enough to stand on it’s own? Well my gen­eral rules that
I try to adhere to are as such:

  • If there is a deci­sion to be made in the com­ment (switch state­ment or if clause) ex.

  • Array para­me­ters, we all know that putting more than a hand­ful of optional para­me­ters on a func­tion can make a func­tion barely usuable,
    how­ever replac­ing optional para­me­ters with an array can be just as hor­ri­ble with­out an appro­pri­ate com­ment. Gen­er­ally this belongs in the
    DocBlocks as such:

  • Any place where the logic could be fuzzy or prob­lem­atic, this one is really up to the indi­vid­ual coder, how­ever,
    if it’s in ques­tion com­ment it!
  • Any­thing you see in the code that needs to be done at a later time or a bug that you don’t have time to work on,
    most IDEs (Zend Stu­dio, PDT, etc) sup­port these tags and will list them for you in a panel or some other list­ing mechanism:
    //@TODO: Future Todo//@BUG: Located or probable bug

  • That brings up to another point, Good API doc­u­men­ta­tion is hard to sit down and write, we’re Code Poets not Man­ual Writ­ers,
    how­ever this is much more eas­ily accom­plished by offer­ing infor­ma­tion in the code and let­ting an appli­ca­tion (such as PHP­Doc­u­men­tor)
    go out and gen­er­ate the API Doc­u­men­ta­tion from that, DocBlocks are usu­ally defined as:

    A list of valid @tags for PHP­Doc­u­men­tor is avail­able at http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.pkg.html

    A quick example:

    * @copyright 2009* @package Example* @since $Id$*/classExampleClass {
    
        /**    * Contains the SomeClass passed by constructor.    *    * @var SomeClass    * @access private    */    private $class;
    
        /**    * Creates a new instance of ExampleClass    *    * @param SomeClass $class    */    function__construct(SomeClass$class) {}    /**    * Takes two parameters and returns True however has been replaced by    * method x    *    * @param string $string    * @param int $int    * @return bool    * @deprecated    */    functionReturnsTrue($string,$int)    {        return true;    }}?>

I hope this intro­duc­tion will help some of you out there, I must admit I’m not much of a writer. I hope to pub­lish
part 2 by the end of this week (Jan­u­ary 12 th, 2009) which will deal more on how to for­mat con­di­tions and error check­ing
to where it’s not dif­fi­cult to decypher.

From → PHP

5 Comments
  1. Excel­lent topic I will def­i­nitely be back for part 2. All too often I run into code that is nearly impos­si­ble to deci­pher with­out refor­mat­ting it. Leg­i­ble code is so key to advanc­ing many of the ter­rific open source appli­ca­tions and ser­vices that it really should be dis­cussed more often.

  2. David Ferguson permalink

    While writ­ing com­ments in the code is an excel­lent thing to do. I feel I have the hard­est time moti­vat­ing myself to do it while I’m writ­ing the code, because I’m more inter­ested in get­ting the job done. At that point I’m not wor­ried about fig­ur­ing it out later. Then once you get done with the code, its just a pain to go back and step through all the code and do it. I have been try­ing to make a con­scious effort to include more doc­u­men­ta­tion in my code lately. This is just a bad habit of mine, and I’m sure many oth­ers as well.

  3. It is indeed nec­es­sary to com­ment and make your code read­able as more than likely another coder will access your work at some­time or another.

    –Brenelz

  4. My key guide­line on com­ment­ing is mak­ing sure I spell out the “why” behind the code logic, much more so than the “how” of the logic. Most anti-commenters will argue “don’t repeat in com­ments what I can clearly read in the code itself”. While this argu­ment holds some weight with me, I can say that as some­one who’s spent a major­ity of his time read­ing other people’s code and hav­ing to divine what’s going on, it’s the “why” behind the logic that I spend the most time research­ing, greater by far than research­ing the “how”.

    If I can under­stand the “why”, then the “how” almost always makes more sense, because I then have some “con­text” around the “how”. With­out know­ing the “why”, I’m left con­struct­ing assump­tions about the “how”. Many times this makes it more dif­fi­cult to main­tain the code, as I can’t be 100% sure about what I *think* is a bug or about where a proper place for a change should go.

Trackbacks & Pingbacks

  1. Shawn Stratton’s Blog: Code Readability, Part 1 | How2Pc

Comments are closed.