Rendering collections

Like anything as a developer, there are many ways to accomplish the same task.

While making some updates to code and doing some refactoring, I couldn't help but notice a smell in our logs. I hadn't really thought much about it before, but it caught my eye this time - for whatever reason.

For a code base that's been alive for two+ years, there are quite a few spots that we just haven't felt the need to touch. We've refactored heavily over the past few months, but we haven't touched everything. Our churn is almost 0 in some files for no particular reason.

This particular code smell showed up in our logs. It's a spot where we render a collection with some partials. More specifically, it's a place where we render comments for a particular user. Looking at the code now, it's obvious what we should do to improve it.

For the record, it's a file we haven't changed in over a year. And better yet, the 2nd time it's ever been changed was in late 2011.

Here's what I'm talking about:

Currently, we're using Example 1 to render comments. I've shown the code smell in the logs. My first question: Why do we need to render all of these partials? There's an obvious reason in that we have many comments. But, there must be a better way.

Use the built-in methods for rendering a collection

Most of you probably know that Rails offers a built-in way to render a collection of items with a partial. It's even optimized for you.

In Example 1, Rails doesn't know you're trying to render a collection. It simply does what you ask of it. Loop and render the item each time. It's not smart enough to tell you there's a better way.

In Example 2, you're simply providing a collection and letting Rails iterate. Not only will it iterate for you, it does it in a smart way.

In partial_renderer.rb above, there's a branching case where it checks for a collection you provide. If it sees one, it takes a slightly different approach.

It caches the partial and maps the results of the collection. It's something you can let Rails worry about.

You can see the results in the logs above. In Example 1, it looks up the partial 3 times and renders it. In Example 2, there's only one look up. You can imagine how this gets cumbersome if you're trying to render hundreds of comments.

Example 1 is O(n) in regards to looking up your partial. Example 2 is O(1). Although, to be fair, Example 2 will still have to render each partial with the proper data. Example 3 is just another way to write Example 2 (if you follow the the proper naming conventions).

Taking time to disconnect

The Chicago Tribune has a digital section called 'Blue Sky Innovation' where they talk about work, business and entrepreneurship in the new economy. They did a story about people who take time to disconnect, and happened to find me as a good fit to profile for their article. Maybe the saddest, funniest, most true thing ever written about me:

He attends yoga about three times a week, finding the moves similar to stretches he did when he was a diver in college. He found himself welcoming the hour of disconnection and realized it was the only time he was away from glowing screens.

Podcast Episode 15 - People didn't leave radio, radio left the people

In the latest episode of our podcast I'm joined by Shawn Campbell, the founder of CHIRP radio, a community radio station in Chicago.

Shawn built CHIRP from the ground up with a slew of volunteers, and now organizes over 200 people that bring 21 hours of live on air time every day.

You can hear the passion Shawn has for radio and what it means to her when she's talking, and her story is a real inspiration for anyone looking to make their dent in the world.

During the course of building CHIRP, Shawn has run web and mobile development projects, fundraising efforts, applications with the FCC and a host of other things. The workload seems almost unimaginable, and yet, she and her team built a radio station from scratch in America's third largest city.

Amazing.

As always listen right here on the blog, or subscribe to the podcast in iTunes.

Design moves down the stack

The purpose of design, is to solve problems. Yet when most people think of design, we still think in terms of visuals, not functionality.

It's easy to do this because a lot of the time, those who have solved usability problems in an elegant way, have also worked hard to create visually beautifully interfaces. But it would be a mistake to think that function is born from beauty.

I do think though that we can say beauty is indeed born from function.

One of the things I've noticed about the people and companies I admire the most, are that they treat design as much as a programming/functional/build problem as much as they do as a visual problem.

When I say design moves down the stack, I mean this in both small and large ways. 'The stack' can mean anything from a programming framework, to a computer network, to an electrical grid.

Some examples that come to mind:

In each of these examples, we see design moving further and further down the stack, and I can only see this trend accelerating.

What this means in terms of impact, I would imagine, is that design and engineering will become so closely intertwined that it will be almost impossible to separate the two. And even if you could, there wouldn't be much value in doing so.

What I particularly like about looking at design in this way though is that it can help identify opportunities and hurdles alike, which can in turn be used as a guide.

Take Comcast as an example, a company we all love to hate. One of the things they could be doing, is trying to figure out how to make their own hardware obsolete. Their design could move down the stack by allowing you to subscribe to their service just by downloading an iOS app.

Interestingly, Netflix is now competing with Comcast (and HBO, Showtime, etc.) by moving their design down an entirely different stack. Not a technology stack - which they already have - but a content stack. They now make their own TV shows.

The other nice thing that comes with thinking about design in this way is that it opens up opportunities for all of us, even people who might not consider themselves a 'designer'.

Because we all have our own world-views and sets of problems, we can all find design direction by looking down the stack.

Podcast Episode 14 - The fantasy dies when the marketing begins

Well I'm giving another go at bringing the podcast back! We'll see how it goes. In this episode I talk with my good friend Ryan Evans, the founder of Lift Marketing and BiteSizePR. Both companies help small businesses get marketing and in this episode we talk about building products, marketing them and why so many people put off marketing and sales for so long.

Subscribe to our podcast in iTunes, or listen to it below!