Matthew Price

Waiting for the Best Solution

The Amazon Fire TV and Xbox One have started implementing voice control, and it’s likely we’ll see future Apple TV upgrades that offer the same.1 But why did it take us so long to make input less awkward? Were we so focused on the best solution that we forgot to look for better solutions that would suffice in the meantime?

We have a wonderful iOS Remote application for controlling the Apple TV but no quick way to open it. Even if our iPhone is within arm’s reach we still need to unlock it, find and open the Remote app, wait for it to load, select the Apple TV, and wait for it to connect before we can start navigating.

Earlier this month I tweeted:

The iOS Remote app should display a notification on your lock screen whenever a linked AppleTV is waiting for keyboard input.

How long would it take to build a solution that opens Remote and connects to your Apple TV when you tap on a notification? It’s not perfect, and it doesn’t cover every situation, but it would have temporarily eased our pain over the last five years.2

I’m not in any position to question Apple’s decisions—they’ve had unequivocal success without my input—but the rest of us could use this as a reminder. The next time you’re brainstorming, try not to toss aside all the imperfect solutions you come up with along the way. Maybe one of them is a simple solution you could implement now while continuing to work on the holy grail.

  1. WWDC 2014 kicks off next week, but the Apple TV’s release schedule isn’t consistent and there are ever-circulating rumors of bigger changes coming this Fall.

  2. iTunes Remote was released in 2008, and iOS has had push notifications since 2009.

Golang: Missing or Incomplete Socket Messages

I recently ran into a puzzling error while working with Go socket connections. When performing a read operation, incoming messages would periodically arrive incomplete or not at all. The fix turned out to be easy, but the problem wasn’t immediately obvious to me.

Almost every Go server implementation is built on top of the io.Reader interface. Go also provides us bufio.Reader, which implements buffering for an io.Reader object. It’s common to run into the following pattern for reading bytes from a connection:

data, err := bufio.NewReader(conn).ReadBytes('\r')
if err != nil {
    // Handle error
}

But we rarely need to read just one message from a socket connection. To continuously wait for new messages, we can enclose our code in a for loop and hand off processing to another goroutine. If we’re feeling clever, we can reduce memory usage and processing time by using a pointer to pass off our data:

for {
    data, err := bufio.NewReader(conn).ReadBytes('\r')
    if err != nil {
        // Handle error
    }

    go processData(&data)
}

Everything looks good so far, but this code actually introduces a big problem that you might not notice during testing: we’re creating a new bufio.Reader, and underlying buffer, on every iteration. Since the buffer is not persistent across iterations, any messages received before the new Reader is created will be lost.

The proper way to write this code is to create a new Reader outside of the for loop. You can then call its read methods like normal on each loop iteration:

reader := bufio.NewReader(conn)

for {
    data, err := reader.ReadBytes('\r')
    if err != nil {
        // Handle error
    }

    go processData(&data)
}

Printing TODO Comments with Ack

TODO Output

There are a lot of terminal commands floating around that will list the TODO comments in your source code, but I’ve always found their output format lacking. Last week I decided to come up with something better. It’s language agnostic and it organizes everything into vertical columns to make reading easier:

ack -i -o --group --color --sort-files "(TODO|FIX(ME)?|OPTIMIZE|BUG)(\(\w+\))?: (.*)" * | perl -pe "s/:/\t/" | perl -pe "s/\t(\w+)(\(\w+\))?:(.*)/\t\$1:\$3 \$2/"

Here’s a breakdown of the options we’re using, and a few others you might find useful:

Option Description
--color Highlight the matching text
-follow Follow symbolic links
--group Group matches by filename
-i Ignore the case of matches
-n Don’t search within subdirectories
-o Display only the matched text1
--sort-files Sort the outputted files in alphabetical order

Updates

  • Added support for “BUG” and Go-style attributed comments such as “BUG(who)”. — 29 April 2014
  • Moved Go-style attribution to the end of the comment. This should make the list easier to scan. For example, “XYZ(who): Comment text” is now displayed as “XYZ: Comment text (who)”. — 25 May 2014
  1. When combined with wildcard matching, this helps us discard any tabs or spaces that come before the comment.

Merry Christmas

Christmas Card 2013

Wishing you all a merry Christmas and a happy New Year.