Category: php

  • Less code vs. good code

    Alex Netkachov, Vidyut Luther, and Richard Heyes discuss the pros and cons of writing code that is short. Here are some thoughts from me.

    In general, I don’t think turning three lines of code into one line makes an application better, or more readable, or prettier, or whatever is your goal. The logical extension of this is to switch to writing Perl code:

    #!/usr/bin/perl
    # Valentine.pl, copyright 2001-2007 Bill Karwin
    map( ( $r=$_, map( ( $y=$r-$_/3, $l[24-$r].=(‘ ‘,’@’)[$y**2-20*$y+($_**2)/3<0] ), (0..30) ), ), (0..24) );
    print join(“n”, map(reverse($_).$_, @l)), “n”;

    The problem with such super-compressed code is that it becomes harder to code, harder to debug problems, and harder to maintain. Imagine being a newly hired developer assigned to a project that contains code all written like the above.

    Of course, we’re not talking about code as obfuscated as the above. But taking a step in the direction of compressed code for the sake of compressed code, instead of for some functional improvement, is a step toward making the code less maintainable.

    For example, think about reading diffs in your revision control system, when a bug fix occurs on such a compressed line of code. It’s harder to see the nature of the bug fix, compared to a more expanded form of coding. For this reason, I prefer to write expanded code–even when writing Perl code!

    The ternary operator is another good example. Vidyut expresses that the ternary operator should not exist. It’s true that it can be abused like any programming construct, but it is definitely useful to have the ternary operator in the language. It is a simple if/else construct, which has a return value. The conventional if/else construct doesn’t have a return value. Alex gives an example that is a perfect use of the ternary operator.

    $message = ($age < 16) ? 'Welcome!' : 'You are too old!';

    But notice I said simple. The ternary operator is inappropriate to use for any if/else blocks that contain anything more complex than a single expression. What if the application requires that we log the event when someone who is too old attempts to access the system?

    One can try to do acrobatics in code to accomplish two steps in one expression, but this is not a good practice because if a bug occurs in either step or the requirements change, you end up breaking both.

    // Wrong
    $message = ($age < 16) ? 'Welcome!' : log_and_return_message('You are too old!');

    Once either the positive or negative block becomes anything other than a single expression, you do need to change to using an if/else construct. So if you are tempted to use a ternary operator because it’s shorter or it’s prettier, consider if there is any likely scenario in which you would have to restructure it anyway. If so, that’s probably a good reason to use if/else instead of a ternary expression.

  • Leaving Zend

    I’ve worked at Zend for the past 13 months, heading up an open source project called the Zend Framework. Zend Framework is a library of PHP 5 classes providing simple, object-oriented solutions for most features common to modern web applications. I was the project manager as well as developing a lot of code, tests and documentation, and engineering the product releases through its 1.0 release.

    When I joined Zend in September 2006, that project had made a few “Preview Releases”, but it was losing momentum. My assignment was to organize the project, finish development of the 50+ components in the library, make regular beta releases to demonstrate progress, and to move the product to a general 1.0 release as rapidly as possible.

    To achieve this goal, I knew we had to manage the scope of the project carefully. There’s always tension between CSSQ (Cost, Scope, Schedule, and Quality) in any project. The project already had bare-bones cost, we had high standards for quality (who doesn’t?), and Zend placed a very high priority on making a general 1.0 release as soon as possible. So the only thing remaining to control was the scope.

    We were blessed with an enthusiastic user community, but this meant that we had dozens of people submitting feature requests and proposals for new components every week. Though the ideas were genuinely very attractive, there was simply no way to add them to the project scope without causing consequences to the schedule or quality of the project. And there were some features that we wish we had time to do before we had to reach that 1.0 milestone.

    Some members of the user community voiced objections to the emphasis on schedule over feature-set. I hope they understand that we were following the priorities of Zend, who is after all the sponsor of the project.

    We released Zend Framework 1.0 on June 30, 2007, and followed it with a couple of bug-fix releases in July and September. Zend Framework is accelerating in popularity, with over 2.3 million downloads to date. I feel proud to have contributed to a successful web application component library. It’s very satisfying to see so many people using code I worked on, making their own projects more successful.

    I was honored to work with a great team of software developers. I learned a lot from Darby, Matthew, and Alexander as we worked together, about technical subjects, productivity, and teamwork. Those guys were great to work with, and I hope to work with them again someday. There are a lot of other fine people at Zend and in the Zend Framework developer community, but I worked most closely with those three.

    I completed my assignment at Zend successfully. But Zend and I were unable to define a next objective for me. That usually means it’s time to declare victory and move on, so I gave notice and I finished working there last week.

    During the time I was on the project, the Zend Framework increased by:

    • 104,000 lines of PHP code in its core library;
    • 100,000 lines of PHP code in its unit tests;
    • 3,945 unit test functions (overall code coverage from tests is 84%);
    • 40,000 lines of documentation;
    • 200 new community contributors.