Choose Your Weapon

I spend all day writing software on the Microsoft desktop stack, and my toolbox is small: Visual Studio, Resharper, C#/VB, and the .Net Framework. With those I can build pretty much anything. I’ve been playing at web dev on the side, and the best and worst—the bworst1—thing about it is how many things there are to learn. Leave aside the fact that writing a modern website or web app involves at least one programming language, a markup language, and whatever the hell you classify CSS as: I’m talking about the tools. There are so many, and it’s not at all clear at a glance which will suit you and your project best, and so staying productive is a challenge. What if you choose a tool and pour time into learning and using it only to discover it’s ill-suited to your task, or is abandoned by the larger community in a year in favour of another very similar tool?

I haven’t found a good approach to this, a good balance of research and testing versus flipping a coin and getting to work. Discussing the problem with a colleague the other day, though, I did manage to discover the Best Game Ever:

Start typing nouns followed by “js” into Google and see how many in a row turn up a development tool or library.

After amusing ourselves with this for a while we concluded that, as frustrating a problem as this is, we should count ourselves fortunate that it’s our job to play with computers; we could be laying bricks all day.

But that got me thinking, and I found:

Seriously. What is this madness?2


  1. Copyright, me. 
  2. Madness?! THIS IS SPARTAAAAAAA.js!!! 

www. is not deprecated

Tricky: I vastly prefer the look of the “naked domain” to, for example, www.danj.ca. But maybe caring what your URL looks like is ridiculous in this day and age.

Anyrate, I’ve been a proponent of using the naked domain but making sure you redirect www for a while. It’s interesting to know there are actual, technical reasons to prefer the subdomain.

You should use www because today you have a small web site, and tomorrow you want a big web site. Really big.

The technical reasons to use www primarily apply to the largest web sites which receive millions (or more) of page views per day, web sites with a large number of services across several subdomains, and virtually any web site hosted in “the cloud” by an application service provider.

But I’m not switching yet.

(via Zack Tollman's retweet)

Fighting to Stay Creative

Great post by Shawn Blanc:

These are things that will whisper in our ear that our idea is pathetic and our implementation of it even worse. They urge us to give up, to move on, to quit, and to pacify our minds. They tell us that we have nothing unique to offer, that we have no value, and that everything will come crashing down any minute, so why even bother.

And,

You have to trust your skills, trust your gut, and trust your value as a contributor. You’re not an impostor. And the more you learn and the more experience you gain, the more your skills will grow. But if you wait until you’ve “arrived” to begin your journey, it’s a logical impossibility that you will ever actually arrive. You have to step out the front door and start walking.

I could stand to spend less time, I’m sure, reading things on the internet. But I won’t feel too bad about reading something like this, something that makes me want to rush home and make things.

Exponent Postcast 002: The Missing Episode

Ben Thompson became one of my internet heroes pretty much as soon as I found him (linked from Daring Fireball, probably, I can’t even remember now). I was sad when he vanished from the Cubed podcast, and pleased when he started his own as part of a push to turn his website into more of a career. He’s now switched it up again, but who cares: Ben is brilliant, and in this episode he and co-host James Allworth eloquently describe where Silicon Valley’s often-frustrating attitude comes from, and what that means for the future.

They focus more on what effect privilege has on the problems tech aims to solve, rather than on the plight of the many groups it fails to serve—women, people of colour, etc.—but their insight is nonetheless valuable in deconstructing the Problem With Tech. Have a listen.

What Would Make App.net a Success?

There’s an argument to be made that App.net’s core mistake was building the Alpha web interface only far enough to match Twitter’s features and then moving on to other things. Instead, they could have kept improving Alpha until it was significantly better than Twitter, so good that it couldn’t be ignored. By doing so, maybe they would have also more effectively demonstrated the power of the API underneath.

That so good it couldn’t be ignored is interesting. Why does Twitter have millions and millions of users? I hear lots of people, especially in tech, talking about how Twitter’s value proposition to new users is precarious1. So what would have made Alpha (App.net’s original service) substantially better than Twitter, and better to whom? Developers? Manton would know better than I, but it seems like lack of users and the opportunity they represent would keep devs off the API more than anything else.

I’m not deleting my App.net account, by any means. But it will be interesting to see what it turns into, now. As “plumbing” for interesting cloud-backed apps like Manton’s Sunlit, App.net has always had potential, but does the service need user growth for that potential to be realized? Or is building good enough software on top of it enough? I have no idea.


  1. I heard Ben and Jon talking about it on Stratechery.fm just today, in fact. 
(via Marco Arment)

Alas, A Lack

Since App.net never became the new primary place where our friends all hung out, most of us never left Twitter — we all just accept that they’re dicks now, and we forgot about App.net.

I always wanted App.net to succeed as an awesome basis for apps with social features, even if it never displaced Twitter. But lately services that provide what seems like the ideal consumer-friendly model are winking out left and right (Everpix shuts down, Loom sells to Dropbox, this). This isn’t a solution for most, but self-hosting all the things is becoming increasingly appealing to me… Wonder how tent.io is doing. :P

Be Nice

Hold yourself to a higher standard than the minimum the rules allow for

It’s no more complicated than this.

WordPress Theme Tip: Remove Leading Whitespace in Excerpts

One of my favorite expressions appropriated from the military and applied to software development is “no plan survives contact with the enemy”. Well, no theme design survives contact with the content, either.

Sometimes posts don’t start with the opening word. Sometimes they start with images or sub-headings, sometimes the writer follows the print convention of indenting paragraphs instead of the web convention of not. That’s all fine until you want to display an excerpt of the post, especially when that excerpt is only a few words long, crammed under a thumbnail image. Having a dozen words shoved to the side by a patch of nothing (since images and most formatting are stripped out of excerpts) is design-breaking in some cases and ugly in the rest. What’s a theme developer to do?

Enter the the_excerpt Filter

When we want to change the way WordPress displays pieces of content without changing the content itself, we turn to filters. Filters are PHP functions that are associated with (“hooked to”) a key (“filter hook”) that WordPress’s functions use when producing their output. In our excerpt example, WordPress’s the_excerpt() function will apply all filter functions hooked to "the_excerpt", passing the value it was going to return (a string containing the post excerpt, in this case) to each and replacing that value with the result.

So if I have a post whose excerpt is “foo” and add a filter function that concatenates “bar”, I’d put the following in functions.php:

function my_excerpt_append_bar( $output ) {
    $output .= "bar";
    return $output;
}

add_filter( 'the_excerpt', 'my_excerpt_append_bar' );

Now when the_excerpt() is called, it passes “foo” into that function and comes out with “foobar”.

We can therefore strip leading spaces from the excerpt using PHP’s trim function:

function my_trim_excerpt( $output ) {
    return trim( $output );
}

add_filter( 'the_excerpt', 'my_trim_excerpt' );

Since excerpts tend to end in the middle of a sentence, we can probably ignore trailing whitespace, and call ltrim() to remove space from the beginning only. In this case, I reckon it doesn’t make much difference.

The Problem With the_excerpt

Looks like we’re all set, but it won’t actually work. You’ll end up with output from the_excerpt() that looks like this:

<p> Dammit, the whitespace is still there!</p>

Talk about a plan that didn’t survive contact with the enemy. To understand why this doesn’t work, we should figure out just what the_excerpt is doing (those p tags might tip you off…)

In wp-includes/post-template.php, we find:

function the_excerpt() {

    /**
     * Filter the displayed post excerpt.
     *
     * @since 0.71
     *
     * @see get_the_excerpt()
     *
     * @param string $post_excerpt The post excerpt.
     */
    echo apply_filters( 'the_excerpt', get_the_excerpt() );
}

So that’s where it’s applying whatever filters you’ve hooked onto “the_excerpt”. Not terribly interesting! The logic must all be in that get_the_excerpt() function (which, the Codex tells us, returns the excerpt instead of echoing it).

Conveniently, the source for get_the_excerpt() is located immediately below the source for the_excerpt():

function get_the_excerpt( $deprecated = '' ) {
    if ( !empty( $deprecated ) )
        _deprecated_argument( __FUNCTION__, '2.3' );

    $post = get_post();

    if ( post_password_required() ) {
        return __( 'There is no excerpt because this is a protected post.' );
    }

    /**
     * Filter the retrieved post excerpt.
     *
     * @since 1.2.0
     *
     * @param string $post_excerpt The post excerpt.
     */
    return apply_filters( 'get_the_excerpt', $post->post_excerpt );
}

…So it just takes the excerpt from the current post, if there is one, and applies any filters hooked to “get_the_excerpt” to it. There’s nothing interesting there, either! Are we missing something?

We are: WordPress can hook up its own filters, and does. Searching for our filter hooks, “the_excerpt” and “get_the_excerpt” in the WordPress source leads us to wp-includes/default-filters.php, and a stack of hook-ups:

add_filter( 'the_excerpt',     'wptexturize'      );
add_filter( 'the_excerpt',     'convert_smilies'  );
add_filter( 'the_excerpt',     'convert_chars'    );
add_filter( 'the_excerpt',     'wpautop'          );
add_filter( 'the_excerpt',     'shortcode_unautop');
add_filter( 'get_the_excerpt', 'wp_trim_excerpt'  );

So, out of the box, all those functions get called when calling the_excerpt(), in addition to any that we’ve added. No wonder we get unexpected behaviour. And testing reveals that it’s the wpautop filter that wraps the excerpt in <p> tags. Since the output passed into our my_trim_excerpt() function now begins with <p> instead of with whitespace, trimming it doesn’t do anything! So what can we do? We can hook our filter to a point in the call stack before the_excerpt() gets its hands on it: we can hook it to get_the_filter() itself1.

add_filter( 'get_the_excerpt', 'my_trim_excerpt' );

Now the output of the_excerpt() becomes <p>Dammit, the whitespace is still there!</p>, as expected2.

Except When it Doesn’t

This blog post would be over and we could all get back to our lives, except the solution above still wasn’t working for me. Copying the page output from Safari’s Web Inspector into a text editor revealed that the actual excerpt being rendered looked like this:

<p> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Dammit, the whitespace is still there!</p>

Yes, really. Like I said, no theme survives contact with the content; somehow, the creator of a post will find a way to get HTML like that into the database (when I write “somehow”, you should read “Microsoft Word”).

By default, PHP’s trim() function doesn’t strip non-breaking space entities. But it has an optional second argument that lets you provide a list of other characters you want torn out. Fortunately, we’re not the first to encounter this problem, and a comment on the trim function’s manual page gives us the solution: add the Unicode characters that represent non-breaking space to the default list of characters to trim.

function hr_trim_excerpt( $output ) {
    return trim( $output, ' \t\n\r\0\x0B'.chr(0xC2).chr(0xA0) );
}

Now we’re all set. Excerpts are being trimmed, and we’ve learned a lot about how a few important pieces of WordPress work.

Except it Still Doesn’t Work All the Time

Applying the above solution definitely fixed some of the still-padded excerpts… But not all of them. The ones that weren’t trimmed still had non-breaking space entities in them and, to be honest, I’m still not sure why. Could all non-breaking space entities not be created equal? Could my content have some mixed-mode encoding whereby some of those characters had the Unicode identifier provided above, while others were different? I’m not gonna lie, I took a brute-force approach to the fix at this point:

function hr_trim_excerpt( $output ) {
    // I'm not entirely sure why both str_replace and the use of the Unicode for nbsp are required to rip all non-breaking spaces out, but they are.
    return trim( str_replace( '&nbsp;', '', $output ), ' \t\n\r\0\x0B'.chr(0xC2).chr(0xA0) );
}

Yup. I needed both techniques, replacing the literal string &nbsp; and trimming the Unicode non-breaking space, to make sure all of my excerpts were cleaned up. If anyone knows exactly what’s going on here, I’d be happy to hear it. Until then, however, we’re done. Go forth, and enjoy your excerpts, unpadded, the way God intended.


  1. Make a note to go back and look up that wp_trim_excerpt function, which gets called whenever we call get_the_excerpt() (and, therefore, whenever we call the_excerpt()): It does all the interesting work: If a post has no explicit excerpt defined, it produces one. And since it applies several other filter hooks, there are a bunch of places where we can customize the results: any filters on the post’s entire content are executed, we can put a filter on the excerpt’s length, how the excerpt is truncated (by default … is used), and we can even filter the trimmed text after all those things are applied. 
  2. Yes, the sample text no longer applies. Only you would think of pointing that out, smartass. 

Own Your Words

I understand that slotting an article into someplace like Medium may be a quick way to get some readers, but using it as your regular outlet seems like a huge mistake to me. If you have enough to say that you want to publish something of substance for a wide audience, your ultimate goal surely has to be to publish it yourself, in your place.

This.