Comma, Separated, Values,

I recently realized how much of the art of programming is just lots of little tricks that you pick up along the way. I’m resolving to share these little tricks, in the hopes that others will do the same, either on their blogs or in the comments here.

Here’s one involving an array and building a string of comma separated values.

Let us pretend you have an array called Tags. It contains the tag names as strings.

Tags[0] = "awesome"
Tags[1] = "groovy"
Tags[2] = "existential"

You want to display these as CSV

awesome, groovy, existential

How do you do it? You might think “well, I want to print a tag, then a comma, then the next tag, and so on until I’m done”

for(i = 0; i < Tags.length; i++) {
    print Tags[i] + ', '

You probably see what’s wrong already. The above code prints

awesome, groovy, existential,

OK. Maybe you could get clever:

first = true;for(i = 0; i < Tags.length; i++) {
    if(!first) { print ', ' }
    print Tags[i]


for(i = 0; i < Tags.length; i++) {
    if(i == Tags.length - 1) { print ', ' }
    print Tags[i]

But ouch. Don’t do that.

Many languages provide a built in function for converting arrays into CSV strings. PHP calls it implode(), Javascript calls it Array.join(). Simply put, it returns a string consisting of the values of the array, with a delimiter in between the values.

Tag_string = Tags.join(', ')


The tortoise was right.


But oh no. Show them one bullshit-laden presentation and the entire Rails community is champing at the bit and selling both kidneys to ditch all previous Ruby implementations and everything they thought they knew about the persistence layer and embrace some questionable closed-source vapourware, from the guys who brought you that previous world-storming web framework Seaside. What’s that, you’ve never heard of Seaside? I wonder why.

I think a great point is made in this post about the level of speedup that can be achieved. There’s some group claiming they can speed up Ruby by 60x. That’s a ridiculous level of improvement, and Sho is calling them on it.

Lots of developers think they’re brilliant, and want to be that one genius that finds this amazing breakthrough that no one else thinks of; but it doesn’t happen that way. When there’s a lot of smart people working on something, mind-blowing breakthroughs are incredibly rare. A 3-5x speed increase is a massive improvement, and certainly a clever code artist could make it happen; it’s rare to have 3-5x, but possible. Doing something 60x better than anyone else is borderline unimaginable, and as Sho says, “requires extraordinary proof”.

Most real progress is slow and steady over time. And boring. But that’s the nature of real progress.

Thanks to Michael for the link.

How to make a single function call handle one or many

I was browsing through Ning’s codebase the other day. I work directly with the code since I’ve customized a Ning network for one of Cloudspace’s clients in some subtle ways, just because he wanted it that way.

I really like the work they’ve done. Their codebase clearly has a lot of best practices.

I found one neat trick that was used to make a function a bit more versatile, in that it can easily be passed a single object, or an array of objects, with no sweat. If you’re interested, you can find an example of this in /widgets/activity/models/ActivityLogItem.php, in beforeDelete()

This isn’t their code, but here’s an example of the trick.

function eatPie($pie_id) {
    foreach( (is_array($pie_id) ? $pie_id : array($pie_id)) as $current_id) {
        // codez

Really straightforward. If an array is passed in, each item in the array gets operated on. If a single item is passed in, it gets turned into an array, then each item in the array gets operated on. I also like how the tertiary statement gets used inline with the foreach. Of course, if you want a bit more code clarity, you can do it like this:

function eatPie($pie_id) {
    $pie_id = is_array($pie_id) ? $pie_id : array($pie_id);
    foreach($pie_id  as $current_id) {
        // codez

Drawing diagrams

I sometimes forget just how useful drawing diagrams can be.

For example, working with a DB. Just a quick sketch of circles as tables, write the column titles inside the relevant circles, and then draw a few lines to represent joins.

It’d probably be useful to do this with large blocks of code too.  Just draw parts of the code that matter, and abstract away the parts that don’t matter.