Code Smaller

Unless you’ve been living under a rock for the last few years, you’ve probably heard about the game Katamari Damacy. The gameplay consists of little more than rolling stuff up into an ever-increasing ball of stuff. That’s literally all you do. You start by rolling up small things like matchsticks, thimbles, pushpins, and so on. As the ball gets larger, you roll up ever larger and larger items. Eventually, your Katamari ball gets so large you end up rolling together cities, mountains, clouds – eventually entire planets. It’s unbelievably fun, and completely mesmerizing.

After I played for a while, I realized that Katamari Damacy is a game about the scale of life, reminiscent of the classic Eames powers of ten movie.

Screenshot of 'We Love Katamari'

As Bob Koss points out, code has a natural tendency to become a giant Katamari ball of “stuff,” too:

I travel a lot and I get to visit a lot of different companies. No matter which industry a company is in or which programming language a team is using, there is one commonality in all of the code that I see – classes are just too damn big and methods are just too damn long.

We programmers must take matters into our own hands and become masters of our domains. Unless we take action, things are just going to get bigger and bigger until we have a real mess on our hands.

Bob’s article is about managing the scale of your code:

This notion of breaking a class into smaller and smaller pieces is exactly opposite to what I learned when I first started studying OO. Way back when I worried about bad-hair days, people believed that a class should encapsulate everything that concerned it. A Customer class would know the business rules of being a Customer as well as how to retrieve itself from the database and display it’s data. That’s a fine idea, provided the database schema never changes, the display never changes, or the business rules never change. If any one of those responsibilities change, we are at a high risk of breaking other things that are coupled to it.

So many aspects of software development can be summarized as small is beautiful:

  • The odds of failure for a software project are directly proportional to the size of the project. Slicing a large project into several smaller subprojects is the single most direct way to increase your project’s chances of success.
  • The relationship between lines of code and bugs is completely linear. Fewer code means fewer bugs.
  • Smaller code avoids TL; DR (Too Long; Didn’t Read) syndrome. The less code there is to read, the higher the odds are that someone will actually read it.
  • If you keep your dependencies to a minimum, your code will be simpler and easier to understand.

It’s up to us to resist the natural tendency of any project to snowball into a giant rolling Katamari ball of code. Code smaller!

Related posts

Code Elegance, Code Balance

I’ve been reading a great book of interviews with programmers circa 1989. One of the most fascinating interviews is with Wayne Ratliff, the author of dBase. Wayne’s description of balance in programming really resonated with me: Interviewer: Can you elaborate on this feeling for balance and elegance? Balance

By Jeff Atwood ·
Comments

Recent Posts

Let's Talk About The American Dream

Let's Talk About The American Dream

A few months ago I wrote about what it means to stay gold — to hold on to the best parts of ourselves, our communities, and the American Dream itself. But staying gold isn’t passive. It takes work. It takes action. It takes hard conversations that ask us to confront

By Jeff Atwood ·
Comments
Stay Gold, America

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood ·
Comments
The Great Filter Comes For Us All

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven’t any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don’t stop there – read the Story of Your Life novella it was based on for so much

By Jeff Atwood ·
Comments
I’m feeling unlucky... 🎲   See All Posts