Stepwise Refinement, and such

As you may have already heard from me, I feel like I’ve made some significant progress in the quality of the code I am able to produce over the past few months. I haven’t written about it yet, as I’ve been exploring the specific implications and applications of what I’ve stumbled upon.

I’ve had a chance to tell a few new people about it yesterday, and again it’s hard to communicate the kind of benefit that I see potential for, and frankly, it’s hard for a person who has not seen it to believe it. So I’m hoping to be able to communicate to Josh Leuze, author of Meteor Slides, and to Reid and Peter – who, as I understand, both read the source code of The Events Calendar – some of the techniques through the source code of their systems.

Here’s the first example of code that implements some of these principles. I think the refactoring / diff format in which the examples are presented fit well with what I’m trying to communicate, which is basically a collection of changes to how code I commonly read is written. Here it goes: is a very good example of where a function is commented to express the functionality. I find it better to just simply name the function using those words – comments very often get out of date. shows using a static variable for a value that is obtained from the database, and doesn’t change during the execution of a program. I like this in part because of use of the uncommon static keyword, in part because it enables removing duplication ( for cases such as when I want to change the option name ), and I find it is a nice way of encapsulating something that needs to get initialized once, like getting a database connection. shows a technique I use to name parameters to functions that take several parameters. Especially nice for naming those “true”/”false” parameters to functions that even the WordPress coding standard suggests trying to avoid. It says: “Since PHP doesn’t support named arguments…”, but I think this example proves otherwise. Of course, python-style named parameters, where order does not matter any more, do not exist in PHP, but it is entirely possible to name parameters to functions as I show there. is the meatiest example of the bunch. I could have done several smaller refactorings. I suggest reading it by reading the change to meteor-slides-plugin.php first. An important thing to note is that it would have been much easier to express the functionality with well-named function stubs, like the new version does, when originally writing the software. This is because it is easier to express the algorithm of using the overrides if they exist without at the same time having to think about what the implementation of “is this file overridden in the child theme” is. It is also easier to read, because it is in English, not in “WordPress”. It is also important to note how at least one level of implicit meaning has been replaced by explicit meaning, with naming functionality such as “if ( the_child_theme_has_overridden_css_for_meteor_slides() ) {“. I’ll leave you with that for now.

What do you think? I’d love to get feedback from other software developers.

WordPress and NetBeans

Here’s a few tips for how to make the most out of NetBeans for developing WordPress websites.

Making NetBeans aware of WordPress functions so it can suggest autocompletion

Working with WordPress, I often have to look up how to use a function I’ve used before, but have forgotten the order of parameters. The codex, and in particular the WordPress function reference and WordPress template tags reference pages are great places to look for such things. However, if you set up NetBeans to be aware of the WordPress functions, sometimes you can save yourself a trip to the codex. NetBeans is by default aware of any functions defined within any files in your PHP include path, as well as any functions you have defined in your project. One way to make NetBeans aware of the WordPress functions would be to have WordPress core within your project, but I would prefer to not have it there. When working on files on remote servers, main reason I would use NetBeans over vim, it is a little bit cumbersome to download all of WordPress core, and I usually only download what I need from wp-content. In such a scenario, you can teach NetBeans to also load a local copy of WordPress into memory. To teach it that, go to Tools -> Options, then PHP tab.

Code completion will then be enabled, and the PHPDocs in WordPress will be parsed shown when looking at the available functions. In the picture below, I typed ‘WP_Q’ and the autocomplete functions became available below it, as well as the documentation dialog above it.

WordPress Coding Style Adherence

The WordPress coding standard defines how your code should look so that it looks the same as the code in WordPress core. Adhering to a coding standard is a good idea because it promotes consistency and makes for easier reading of the code you write. I’ve prepared an export of the Netbeans configuration for the editor to adhere to the WordPress coding standard. I hope you find it useful! To import it, Click Tools -> Options in the top menu, then press the “Import” button in the dialog window that comes up and follow the instructions there.

What’s Next?

A nice feature that is not available to the extent of my knowledge would be templates for common WordPress files, like there are template for Java classes, and such. The way this would work is you could say “I’m going to make a new theme” and it would create all the files a theme requires. Or “I’d like to make a widget.”, “I’d like to write a shortcode.” and NetBeans would create the “boilerplate” code so you can get to coding right away.

Do you have any NetBeans tips you’d like to share with your fellow WordPress site builders? Do you know of a plugin for NetBeans that adds those templates I mention in the section above? Thanks for reading.