RailsConf 2008

The Beauty of Code

Apr 06, 2007 4 comments

I don’t remember the origin, but there’s a quote that has a tendency to pop into my head during situations like the one I encountered this past week. It goes roughly:

I was able to turn my 20 lines of PHP into 2 lines of Ruby in 30 minutes, but then I wasted the next week trying to make it just one line.

I was tasked to build a fairly complex system on Chowhound. The problem could have been solved just by throwing a lot of code at it. Conditional after conditional, case statement after case statement, easily becoming a mess of code due to its complexity.

Goal-Oriented to Beauty-Oriented

For better or worse, Ruby has moved me away from just accomplishing a goal to accomplishing the goal beautifully. It forces me to accomplish my task in a way that is not just complete, but also simple and elegant.

That doesn’t sound so bad, right? Well, the problem lies in the quote above. When you’re working with extremely talented people, there’s a fair bit of pride/ego involved. Who can write the clearest, most concise code in the least amount of time. It becomes a game you play.

It’s also part of the reason I enjoy what I do. A friend said to me years ago:

Programming is fun, because I get to solve puzzles every day.

I didn’t really believe that at the time during my CS program, but he was spot on.

The problems start when you’re putting in the extra hours for no real benefit. At the end of the day, the site visitor just wants to use the product and they don’t care how it’s built.

Having said that, if you do your best to make your code the best, you’re making it that much easier for the next developer that comes along to understand how it works in the least amount of time.

I’ll leave it up to you how to strike that balance, I’m still trying to figure it out.

4 comments


Mark Hatham said about 3 hours later:

I’ve met many developers who like to play that game you described, and 99% of them are either single or having marriage problems.

Maybe there’s a pattern between people who try to build rolls royce in everything they code. Such people usually get sidetracked with everything else in life.

William Fisk said about 3 hours later:

”..the site visitor just wants to use the product and they don’t care how it’s built.”

On the other hand, don’t you find that a tidy program inside is often easier to use outside.

Dave said about 6 hours later:

Sometimes it’s dangerous to write less code. Sometimes I’d rather work with 20 lines of readable Ruby code than 10 lines of compact and not so straightforward code. But then again, it feels good to gloat a little when you tell your friends you’ve condensed a method by 50%.

Michel said about 1 month later:

Nice point!

Well, code should accomplish two things:

1) Being readable easily, and 2) Work well.

If it accomplishes both goals, then it’s OK.

Sometimes a line less or more is not so important. :)

Name
Url