NetBeans: Creating a unified diff patch

While trying to submit a patch to a drupal project, I ran into a problem creating a unified diff that could be suspected.  It turns out that the way I was using the built in Subversion diff tool in NetBeans wasn't the way to go.  Now, on the command line, I'm used to doing a "svn diff myfile.php > myfile.patch" to have a useable patch with the latest changes to a file.  In NetBeans, I was thinking the same way, calling up a diff and then saving the output as a patch file – but that omitted crucial information like the name of the file to patch.

After hunting through the menus for a bit, I found the Export Diff Patch option in the Team menu.  This is what you should use to create a unified patch that other developers can use. If you have the focus on a single file, it will generate a patch for just that file.  If you click on a directory in your project and then create a patch it will include all changes for that folder.

A List Apart: Articles: Apps vs. the Web

Another viable option for mobile development is to use a framework like Titanium or Phonegap to quickly build out an app using Javascript.  I’ve been looking at Titanium a lot lately and digging into its API.  Seems like you can build anything short of a game using its API and there’s a real buzz when you compile the same (or nearly the same) codebase to run on both Android and iPhone devices.

With this article, I’d like to share my experiences with both mobile web and software development to guide your future developments on the iPhone platform.

A List Apart: Articles: Apps vs. the Web

NetBeans 6.9 is out.

There's a new release of the the one full-blown IDE that I've managed to stick with.  If Eclipse doesn't fit your style, give Netbeans a spin – I like its inline code completion, the source-code beautifier, and the integration with SVN and Mercurial.  I particularly find the source control usability much better than Eclipse.

After bug fixing period NetBeans 6.9 final build is available. You can download it as usual from NetBeans site. What is new, you can read in this document and also you can watch some new features in NetBeans 6.9 Overview video.

NetBeans for PHP : weblog

Make them Interact!

The more that a team can interact and iterate, the better you're final product can be.  In my ideal world, I'd skip a detailed wireframing exercise and get to building a prototype as soon as possible.  Its so much easier to explain what is difficult to build if there's something to click on.  Or you might learn that what you thought was hard turns out to be easy, or vice-versa.  Finally, if you already know if you are using Sharepoint, Drupal, or anything else, you're front-end designer has to know assumptions the platform makes, how it manages content, and other implementation details.

In my practical experience, I find that teams are more efficient when roles overlap and people understand what is happening outside of their silo.

Developers and Designers « Content Here

More Parameters != More Better

Guilty as charged – my very early code was littered with functions with horrendously long signatures. All the worse in PHP, which doesn’t have a meaningful way to overload functions. Too many parameters means your function is likely trying to do too many things, instead of doing any one thing well.

A simple way to make your code better: Stop adding more parameters

Parameters are good, but there are some things that you need to consider when creating functions with parameters, or when adding parameters to existing ones.

Drupal – Designers vs Developers, can’t we get along?

There’s a lot of good insight in this blog post from the creator of the Views module for Drupal. I’m a big fan of the module, and when I’ve had to dive into the code behind it, I’ve come away pretty impressed. That’s pretty rare for a Drupal module, IMNSHO. Also, if you don’t understand or like the CSS classes and id’s that the Views module uses in its markup, I’m afraid you don’t have a good grasp on the “Cascading” part of CSS or how to use selectors effectively, if at all.

Before reading this, I’d never thought to frame how designers vs developers work in terms of the re-usability, perceived or real. Of course, that clarifies a lot for me, like why Designers love to design in Drupal, the latest site is a standalone piece of art, of course! Or, changing interactions from one site to the next, because each site is a “blank” slate, only to be mystified why it takes so long to implement this version of, say, a Document Library, when you’ve done dozens sort-of-like-it before. An Observation About Designers Versus Developers | Angry Donuts

The converse, however, is not true. If a designer desires a particular piece of functionality, the services of a developer are required. Now, in the Drupal world, we have created a pretty good illusion that you may not need the developer. Drupal, particularly with Views and CCK, allows you to utilize surprisingly complex bits of functionality without needing a developer. But this is only an illusion that you don’t need a developer. The truth is, that you only don’t need a developer as long sa the functionality that these packages provides you happens to be the functionality that your end goal requires. When these things are in sync, this is great. And Views and CCK can do a lot out of the box, so it’s pretty easy to get to 90%, but sometimes that last 10% can be daunting.

An example of using anonymous functions with PHP

If you’re not familiar with them, anonymous functions seem to be a term of art that very advanced programmers throw around to make others feel dumb.  Until now, my closest experience has been in using them with jQuery and javascript, but tonight I found a use for them in PHP.   If you don’t read the wikipedia article linked before, let me pull out a quote that reflects the "Aha!" moment I had tonight. 

Anonymous functions can be used to contain functionality that need not be named and possibly for short-term use.

A sorting and filtering example

While true anonymous functions won’t be available until PHP 5.3, you can use create_function to generate functions on the fly by passing them a signature and string containing valid php code.  These functions are perfect for use with array sorting functions like usort or uasort as well as array_filter.

For this example, suppose we have the following data:

<ol><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1"><span class="re0">$sam</span> <span class="sy0">=</span> <span class="kw2">new</span> StdClass<span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="re0">$sam</span><span class="sy0">-&gt;</span><span class="me1">minutes_played</span> <span class="sy0">=</span> <span class="nu0">1300</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="re0">$sam</span><span class="sy0">-&gt;</span><span class="me1">goals</span> <span class="sy0">=</span> <span class="nu0">5</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"><span class="re0">$sam</span><span class="sy0">-&gt;</span><span class="me1">fouls</span> <span class="sy0">=</span> <span class="nu0">10</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1"><span class="re0">$max</span> <span class="sy0">=</span> <span class="kw2">new</span> StdClass<span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="re0">$max</span><span class="sy0">-&gt;</span><span class="me1">minutes_played</span> <span class="sy0">=</span> <span class="nu0">1900</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="re0">$max</span><span class="sy0">-&gt;</span><span class="me1">goals</span> <span class="sy0">=</span> <span class="nu0">6</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"><span class="re0">$max</span><span class="sy0">-&gt;</span><span class="me1">fouls</span> <span class="sy0">=</span> <span class="nu0">10</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1"><span class="re0">$joe</span> <span class="sy0">=</span> <span class="kw2">new</span> StdClass<span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="re0">$joe</span><span class="sy0">-&gt;</span><span class="me1">minutes_played</span> <span class="sy0">=</span> <span class="nu0">1100</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="re0">$joe</span><span class="sy0">-&gt;</span><span class="me1">goals</span> <span class="sy0">=</span> <span class="nu0">4</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"><span class="re0">$joe</span><span class="sy0">-&gt;</span><span class="me1">fouls</span> <span class="sy0">=</span> <span class="nu0">8</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1"><span class="re0">$team</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">(</span><span class="re0">$sam</span><span class="sy0">,</span> <span class="re0">$max</span><span class="sy0">,</span> <span class="re0">$joe</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol>

You’ve been tasked with reporting on the data, specifically, finding who has the most minutes, goals, and fouls. Traditionally, you’d be inclined to write three functions that sort the array by field you are interested, and return the elements with the highest values. It’s a bit of a brute force approach, but let’s see how we can do this with a single function. Below is that function.

<ol><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1"><span class="co4">/**</span></div></li><li class="li1"><div class="de1"><span class="co4"> * Returns an array of all elements in the array that have the highest value in a given field.</span></div></li><li class="li1"><div class="de1"><span class="co4"> * @param string field</span></div></li><li class="li2"><div class="de2"><span class="co4"> * @param array stats data</span></div></li><li class="li1"><div class="de1"><span class="co4"> * @return array</span></div></li><li class="li1"><div class="de1"><span class="co4"> */</span></div></li><li class="li1"><div class="de1"><span class="kw2">public</span> <span class="kw2">function</span> findMaxStat<span class="br0">(</span><span class="re0">$field</span><span class="sy0">,</span> <span class="re0">$data</span><span class="br0">)</span></div></li><li class="li1"><div class="de1"><span class="br0">{</span></div></li><li class="li2"><div class="de2"><span class="co1">// sorting comparision</span></div></li><li class="li1"><div class="de1"><span class="re0">$sort_code</span> <span class="sy0">=</span> <span class="st_h">'</span></div></li><li class="li1"><div class="de1"><span class="st_h">        if ($a-&gt;'</span> <span class="sy0">.</span> <span class="re0">$field</span> <span class="sy0">.</span> <span class="st_h">' == $b-&gt;'</span> <span class="sy0">.</span> <span class="re0">$field</span> <span class="sy0">.</span> <span class="st_h">') return 0;</span></div></li><li class="li1"><div class="de1"><span class="st_h">        return ($a-&gt;'</span> <span class="sy0">.</span> <span class="re0">$field</span> <span class="sy0">.</span> <span class="st_h">' &lt; $b-&gt;'</span> <span class="sy0">.</span> <span class="re0">$field</span> <span class="sy0">.</span> <span class="st_h">' ? -1 : 1);</span></div></li><li class="li1"><div class="de1"><span class="st_h">'</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2">&nbsp;</div></li><li class="li1"><div class="de1"><span class="co1">// get the data sorted highest to lowest by the field we desire.</span></div></li><li class="li1"><div class="de1"><a href="http://www.php.net/usort"><span class="kw3">usort</span></a><span class="br0">(</span><span class="re0">$data</span><span class="sy0">,</span> <a href="http://www.php.net/create_function"><span class="kw3">create_function</span></a><span class="br0">(</span><span class="st_h">'$a,$b'</span><span class="sy0">,</span> <span class="re0">$sort_code</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="re0">$data</span> <span class="sy0">=</span> <a href="http://www.php.net/array_reverse"><span class="kw3">array_reverse</span></a><span class="br0">(</span><span class="re0">$data</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li2"><div class="de2"><span class="co1">// remove all elements of the array that do not match the max value</span></div></li><li class="li1"><div class="de1"><span class="re0">$max</span> <span class="sy0">=</span> <span class="re0">$data</span><span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="sy0">-&gt;</span><span class="re0">$field</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="re0">$filter_code</span> <span class="sy0">=</span> <span class="st_h">'return ($a-&gt;'</span> <span class="sy0">.</span> <span class="re0">$field</span> <span class="sy0">.</span> <span class="st_h">' == '</span> <span class="sy0">.</span> <span class="re0">$max</span> <span class="sy0">.</span> <span class="st_h">');'</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="re0">$values</span> <span class="sy0">=</span> <a href="http://www.php.net/array_filter"><span class="kw3">array_filter</span></a><span class="br0">(</span><span class="re0">$data</span><span class="sy0">,</span> <a href="http://www.php.net/create_function"><span class="kw3">create_function</span></a><span class="br0">(</span><span class="st_h">'$a'</span><span class="sy0">,</span> <span class="re0">$filter_code</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li2"><div class="de2"><span class="kw1">return</span> <span class="re0">$values</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="br0">}</span></div></li></ol>

What’s going on here? 

  1. First, to use the function we simply pass it the field for which we want to get the maximum value and our data array. 
  2. Line 11, defines the body of a function that will be used to sort the array by that field.  You’ll have to follow closely the use of quotes to build this string.  If we’re looking for most minutes, then the code for this function would look like ‘if $a->minutes == $b->minutes) return 0’ and so on, where $field is replaced by the string ‘minutes’; 
  3. Lines 17 and 18 do the hard work, usort uses our anonymous function to sort the data and then reverse it so that it’s ordered from highest to lowest.  You could also achieve this by changing what th $sort_code function returns and eliminating the call to array_reverse altogether.
  4. In line 21, We get our maximum value for the field we care about from the first element in our sorted array.
  5. Lines 22-23 are here to ensure that we return all the elements that have the max value of our field.  This acocunts for situations where more than one element may be "tied for first place", so to speak.
  6. Finally, we return the values that we’ve found.

What’s the big deal here?  Well, with one function we quickly get the rows we care about.

<ol><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1"><span class="co1">// returns $sam and $joe</span></div></li><li class="li1"><div class="de1"><span class="re0">$max_fouls</span> <span class="sy0">=</span> findMaxStat<span class="br0">(</span><span class="st_h">'fouls'</span><span class="sy0">,</span> <span class="re0">$team</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="co1">// returns $max</span></div></li><li class="li2"><div class="de2"><span class="re0">$max_minutes</span> <span class="sy0">=</span> findMaxStat<span class="br0">(</span><span class="st_h">'minutes_played'</span><span class="sy0">,</span> <span class="re0">$team</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="co1">// returns $max</span></div></li><li class="li1"><div class="de1"><span class="re0">$max_goals</span> <span class="sy0">=</span> findMaxStat<span class="br0">(</span><span class="st_h">'goals'</span><span class="sy0">,</span> <span class="re0">$team</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol>

More importantly, if we add more fields to our data, we don’t have to write more code!

Conclusion

Used correctly, anonymous functions provide very customized functionality that can save you from having to write more code. Also, by learning to use PHP’s built-in array manipulation functions, you’ll quickly learn how to slice and dice nested arrays and arrays of objects without resorting to complicated loops and if statements. I’m not sure what the performance implications might be of using the technique, but if it saves you from writing long, complicated code-blocks it’s worth the trade off.  There’s a lot of existing discussion about create_function, and its impact on memory usage and DANGER!

Tips for working with jquery

I’m a fan of jQuery because it promotes unobtrusive markup and functionality and has a wealth of plugins to take care of common web development/ajax functionality. Marc Grabanski offers some tips for working with it effectively. I’ll need to look at livequery in some apps I’ve put together. If you need to attach events to elements, and re-attach the same events when parts of the page are updated, it should make keeping track of it all a lot easier. My tip for working with jQuery? Use Firefox with Firebug to inspect the page and debug ajax requests.

5 Tips for Better jQuery Code: jQuery, Tutorial

I’ve been coding using jQuery since shortly after it came out, and well — I’ve been using it almost every work day. Here is a few tips that have saved me time.

Practical use of message queues

I stumbled on this post over at the flickr developers blog while chatting with Jo about web applications designed and built for “the cloud”. An article earlier this week piqued my curiosity about message queues, and got me thinking about how they can be used to move processing tasks out of the way. I’d known already that minimizing those tasks within, let’s say, a PHP script that builds a page can help your application scale or seem more responsive.

To do so, I’ve relied on cron jobs, but the main problem with that approach is either that your cron job runs when it has nothing to do, if you run it too frequently, or it has too much to do and you run the risk of one job starting while another is in progress and working with potentially the same data.

Code: Flickr Developer Blog » Flickr Engineers Do It Offline

For scale, Flickr separates these three lookups into three different places. When you upload that photo, we immediately tell you about it, and get out of your way so you can go off and marvel at it. We don’t make you wait while we tell your contacts about it, insert it into the search system, notify third-party partners, etc. Instead, we insert several jobs into our queueing system to do these steps “later”. In practice, every single one of these actions subsequently takes place within 15 seconds of your upload while you’re free to do something else.

Finding some late night coding time.

Nicholas has started sleeping through the night, as of last week, and while it hasn’t been every single night yet, its happened more often than not. I’m glad for Staci, since she’s not a night owl like me, staying up past 10 or 10:30 was harder on her.

This also means I’ve found some time to work on my project sites and blogs. I’ve recently discovered the utility of PHP5’s autoload function, specifically the spl_autoload_register method. So I’ve taken to refactor my codebase to use it Its nice not having to figure out where and when to include class definitions. I’m using the PEAR naming convention to handle autoloading and, as a bonus side effect, this has forced me to organize my class files and directories in a consistent manner.

It hasn’t been all under-the-hood, improvements that only I will see, but I can’t unveil anything new just yet. Sadly, this is the most interesting coding I’ve been doing lately. At work, we’re learning to use Drupal effectively, which has, for the better, reduced the back-end programming we get to do. But I’ve gotta keep my skillz up2date.