Ideas about clean if statements

If you’re reading a lot of code you may get to the point where you’d like it to be clean, so it’s easier for you to read. There’re a lot of resources about the beauty of code around and in this post I’d like to share some ideas about writing concise if statements.

Although these ideas may apply to a lot of different programming languages I chose to give examples written in Java, PHP or Python. There should be no problem to translate this into other, similar languages.

Continue reading →

WordPress: Add notes to the comment form

Most themes use the comment_form action which can be used to add arbitrary things to the comment form with a plugin. This post presents a simple plugin that uses custom fields to add special notes to this section.

You can download the plugin here, unzip it, upload it to your wp-content/plugins directory and activate it.

Continue reading →

Hot math: twin and sexy prime numbers

Just recently I discovered that there’s something called sexy prime numbers. I read about twin primes but hadn’t heard the other term before. The concept is pretty simple: take a prime number and check whether the next prime minus the first one results in a certain number, e.g. 2 for twin primes or 6 for sexy primes. This post presents a simple solution to calculate arbitrary combinations of prime number pairs, triplets, etc.

The Eclipse project with the code for this post can be downloaded as tar.gz or zip. You can browse the code online here.

Continue reading →

WordPress: Using custom fields to load scripts and styles

If you’re developing a WordPress plugin that depends on JavaScript or CSS you may advice WordPress to include these things for you. It’s as easy as using two functions – wp_enqueue_script and wp_enqueue_style – and your script or style will be embedded into each and every page generated by WordPress.

Most of the time this is a good thing because it’ll be easier for your users to implement the plugin on their sites. It gets really ugly if the theme loads certain scripts by default which are incompatible with the scripts needed by your plugin. And users concerned about performance – e.g. using Yahoo! YSlow or Google’s PageSpeed – may want to include only things that are really needed on a particular page.

This post presents a solution using custom fields that allows the user to tell the plugin when to include scripts and styles. Doing it this way helps the user to manage your plugin because of it’s added flexibility without hacking the code. Newbies trying to circumvent a certain incompatibility and power users tuning their sites to maximum performance both will love this feature of your plugin.

Continue reading →