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 takes many forms. The code should be crisp and concise. You should be able to explain any module in one sentence, and things should be in alphabetical order, if possible. Just from a visual view of indentation, it shouldn't go off the edge of the paper at any point. It shouldn't have one "if" that's huge and an "else" that's small. Everything should be balanced everywhere. Balance is the key word.

Interviewer: When you write code, does it come out balanced the first time, or does it need a lot of changes?

I do a lot of changing. I like to make an analogy between writing code and sculpting a clay figure. You start with a lump of clay and then you scrape away, add more clay, then scrape away again. And every now and then you decide that a leg doesn't look right, so you tear it off and put a new one on. There's a lot of interaction.

The ideal module should be a page long. If it grows beyond a page, I have to decide, now what is it I'm doing here? How many seperate things am I working on? Should they be broken down into seperate modules? Part of the elegance, and the balance, is that a certain level, in this layer-cake hierarchy of a program, all the modules should be about the same weight, same size, same duty, and same functionality.

Interviewer: How does balance help a program?

The program becomes maintainable. When you have a good balance, it's as if you've discovered some basic physical underlying principle and implemented it. When things get really out of balance, you know something is wrong. There's probably some inherent fault that makes it out of balance. Generally, when I get this feeling that something's out of whack or one module is just too big, I think about what I'm doing, and I reorient or juggle the pieces.

I think you could sum up reams of programming advice with that one concept: balance. We're striving for balance between complexity and simplicity. And we're constantly evaluating and re-evaluating the tradeoffs we have to make to get there.

Balance also applies to the way you physically lay out your code. There's a great visual device in Code Complete's layout chapter where the actual characters of code are replaced with black bars. Like well-designed web pages in other languages, you should be able to understand the general flow of the code even if you can't read any of it:

abstract blocks of code

The aesthetics of your code is purely an internal implementation detail. How you place your squigglies won't affect users in the slightest. But attention to internal code layout details implies that you're equally attentive to the external details. If you're structuring your code to be accurate, consistent, readable, and maintainable, your application will work better-- because it's balanced.

Read more

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

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

By Jeff Atwood · · Comments

I Fight For The Users

If you haven't been able to keep up with my blistering pace of one blog post per year, I don't blame you. There's a lot going on right now. It's a busy time. But let's pause and take a moment

By Jeff Atwood · · Comments

The 2030 Self-Driving Car Bet

It's my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger

By Jeff Atwood · · Comments