Why are they still writing crappy code?

At work, I'm currently working with Wordpress and I was amazed at how most plugins are really bad written! PHP developers should really start to make an effort about that and this is even more true for popular projects like Wordpress. At least, the core or the Akismet plugin, which comes already installed, should give an example. But no! Have you ever had a look at this plugin source code? I nearly puked! Come on guys, not even comments ? Don't be surprised why enterprise are a bit reluctant to use PHP.

Most plugins don't use classes, don't comment their code, HTML is in the middle of PHP code...all anti pattern or bad practices can be found in these. While Wordpress does not force you and does not provide means to facilitate clean programming, it is still not a reason to not use it. It is fairly simple to separate HTML from php files and use a class for the plugin. Here how I did it (and I'm sure there's even a better way but at least this one is cleaner than most plugin).

In your plugin file, starts by creating a class. I use a start() method which will register all hooks and one method for each of them.

/**
 * explain what my plugin does
 */
class MyWordpressPlugin
{
    /**
     * Initialize the plugin and register hooks
     */
    public static function start()
    {
        add_action('hook', 'MyWordpressPlugin::onHook');
    }

    /**
     * Callback for the action "hook"
     */
    public static function onHook()
    {
        // do stuff
    }
}

// starts the plugin
MyWordpressPlugin::start();

That's it! Far more cleaner than putting everything in the global scope.

All my HTML content is saved as php files with the phtml extension in an html subfolder. Then for each of them, I create a renderSomething() method which simply includes the phtml file.This method can, before the include, do some logic, like retreiving some data. There should be only presentation logic inside the phtml file.

I also create a separate file with a new class when it comes to create some administration pages. My plugin needed an option page so I create a class called MyWordpressPluginAdmin which registers hooks and do all the suff associated with displaying my option page. Then I include this file in the plugin file and call MyWordpressPluginAdmin::start() from the MyWordpressPlugin::start() method. Here is how it looks like:

/**
 * Register the option page
 */
class MyWordpressPluginAdmin
{
    /**
     * Register hooks
     */
    public static function start()
    {
        add_action('admin_menu', 'MyWordpressPluginAdmin::onAdminMenu');
    }

    /**
     * Register the option page
     */
    public static function onAdminMenu()
    {
        add_options_page('My plugin', 'My plugin', 8, __FILE__, 'MyWordpressPluginAdmin::renderOptionPage');
    }

    /**
     * Render the option page
     */
    public static function renderOptionPage()
    {
        include dirname(__FILE__) . '/html/option_page.phtml';
    }
}

I think, and correct me if I'm wrong, that this way of doing things is better. The code is commented, easily maintenable, designer could work on their own files... Do you think this is a good solution? the comments are yours!

PS: sorry for the harsh beginning but I was quite angry and disappointed when I saw all that.



comments powered by Disqus

27/03/2009