Explicit Versus Implict Coding

Konstantin Kovshenin recently posted on his blog about creating a WP_Plugin class. He posted an example of a class to fit his thoughts around it on Gist, and from there, discussion has taken place on how such a class would be implemented.

There has been a fair bit of discussion on my fork of Konstantin’s code about this, and I’ve been updating the class with new ideas as we come across them.

However, as is usual with discussion regarding any semi-complicated piece of code, there has been some disagreement on how best to hook methods in. Mike Schinkel is a fan of mapping method names directly to hooks, whereas I much prefer prefixing methods that I want hooked with either action_ or filter_. (We both agree that PHPDoc tags are a good idea though, although Mike also adds a @wp-nohook to ignore any methods.)1 I thought I’d further flesh out why I’m not a fan of mapping the methods directly.

Personally, while I see the merit in naming methods for hooks directly, I hate magic. I hate not knowing when my code is used, and I think one of the biggest strengths of WordPress is that this hardly ever happens. If I want something used, I explicitly declare that through add_action/add_filter.

Don’t get me wrong: I love making things easier for myself. One of my favourite pieces of code ever is one written by Morten Fangel that I use in almost everything I do: _sortArgs(). This piece of code will take an associative array, like array('a' => 'b') and map the variables to parameters to my function. Combined with $_GET and $_POST, it’s an extremely powerful tool. However, _sortArgs isn’t really that magical when it comes down to it. I’m specifying which parameters I want, and everything is explicitly written by me.

I can see the same thing with this plugin class. If I prefix a method with action_ or filter_ (or using PHPDoc tags), I’m explicitly stating that I want this hooked. On the other hand, a method like init is completely implicit. It happens to match a WordPress action, but that could be a coincidence.

As an example of where this would be a problem for me: I often write a method like admin_page for whatever page I’m adding to the admin. If I have things spread across several pages, I’ll factorise the common header bits and footer bits into admin_header and admin_footer. Except with implicit hooking, I’ve accidentally just hooked my footer method into the administration footer. Now, I have to undo that by specifying that I don’t want it hooked.

To hook implicitly requires that I know every action/filter in WordPress to avoid conflicting with them.

Even worse than this is that hooking implicitly breaks forward as well as backward compatibility. Let’s say I add a method called after_post which I call from another class in my plugin, so I need it to be a public method. Everything is going well, until WordPress adds a hook into templates for adding content after a post. Oops, suddenly, my plugin breaks through no fault of my own, and through something that core developers shouldn’t (and wouldn’t) have to worry about.

Hooking implicitly breaks compatibility in every direction, and is too magical. It is absolutely not the way to consume a public API.

Sidenote: A discussion also emerged on how to use priorities. Mike and I both agree (I think) on using PHPDoc, while Thomas Scholz prefers preferred suffixing the method (i.e. action_init_2). My problem with this is that distinguishing between an named init with priority 2 and an action named init_2 is impossible.

Edit: Thomas dropped support for priorities in the method name, which I initially missed. Thanks for the correction.

Edit 2: Updated with a footnote about Mike’s position regarding implicit/explicit hooking.

  1. Mike has informed me that he does support explicit hooking for published code, but implicit hooking for prototyping. I’m not a fan of this either, since I can forsee people forgetting to do so. []

Think Different

Here’s to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They’re not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or vilify them. About the only thing you can’t do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do.

We’ll miss you, Steve. Thank you.

What’s Up?

And, yet again, I’m falling into the trap of not posting. Despite the fact that I have at least four draft post sitting around, I haven’t had the motivation to post any yet. But never fear, I shall get around to it eventually!

So, here’s a quick update on what’s happening with me. I’ve begun my senior year in high school, with less than 10 months until I graduate. I’ve been slogging away at my projects, as per usual, and I’m hoping to get the SimplePie website fully migrated to Automattic’s servers some time in the next few weeks. The sooner that happens, the sooner I can push out a bug-fix release.

I’ve also had the pleasure of working on YesPlugins with Anthony Cole and Mark Bao, but I can’t reveal too much on this yet. Watch this space, as many things are yet to come. (Interested in working with us? Get in touch.)

I think that’s about it for now. I intend on keeping this journal up-to-date much more than previously, and with some luck, I can stick to that. Until next time, ciao.

Random Antics

Quite a while ago, I was online with some friends in Bad Company 2. We decided to make some explosive footage and compile it into a masterpiece. While we wait for producer Newsworthy to finish our actual in-game video, here’s some of the raw footage. Enjoy!

WordPress 3.0 and Custom Post Types

This morning, I switched the theme on here, Beached, over from using meta fields to WordPress 3.0’s new custom post types. Aside from having to do a manual SQL query to covert the posts, it has worked wonderfully. This also enables me to have different permalinks for links (for example, this post is /92/wp3-custom-post-types/, while a link with the same title would be /link/wp3-custom-post-types/), and in the future, other fun stuff like separate feeds for links and posts, with one combined one. It’s definitely a very powerful feature.

Holidays

Apologies for the lack of posts for the last couple of days. I’ve just started school holidays, so I’ve been asleep for the majority of them. Have these two links to make up for it.

TV Tuners

I have to say, I have definitely not had much luck with TV tuners so far. The first tuner I bought was internal, and thus, didn’t work once I woke my computer from sleep. For some reason it also started making my computer fail to boot.

My newer tuner is a USB tuner, yet it still appears to have the same problem, in that it doesn’t work after sleeping.

The advantage, however, is that I can just unplug it from one USB port, and straight into another. I ♥  hotplugging.

Why Steam Beats Pirating, Hands Down

Valve’s Steam is by far my favourite method of digital distribution for gaming. It is simply so easy to use and it’s getting better too, with the new UI beta being a massive improvement on the previous version of Steam.

Why is Steam so good? In case you’re not familiar with Steam, let me run you through the process of buying and playing a game:

  1. Browse to the game on the Steam store
  2. Click “Buy”
  3. Enter payment details
  4. Wait for the download to complete
  5. Play.

That’s it. No complicated installation to go through; no waiting while the game downloads the latest updates; nothing. Compare this to buying a game physically, where you have to install the game (usually swapping discs halfway through), download the latest updates, make sure PunkBuster1 (or similar) is up to date and usually insert the disc every time you want to play a game.

Even compared to pirating games, it’s so much easier. Torrents are usually slow because of the amount of other users downloading at the same time (seeds:peers ratio is usually low), while Steam’s content servers have always been exceeding fast for me (2Mb/s compared to 40kb/s for a comparable torrent). Additionally, simply finding a torrent is usually 10 minutes of searching itself.

Steam’s ease of use shows. Of the past 10 games I’ve played (excluding free ones), 9 of them I bought from Steam, and the other was Modern Warfare 2, which uses Steam.2

Steam is an absolute win for gamers everywhere. The ease of buying, downloading and playing is fantastic. I’m not aware of any other distribution method for games that is as easy to use.

  1. Don’t get me started on how much I despise PunkBuster. VAC is so much better. []
  2. The only reason I bought it physically rather than via Steam is because I couldn’t be bothered downloading it. []