Ruby is beautiful. So why is my code ugly?

There are a few subjects I thought I would like to write about for this blog, covering the unique, highly technical solutions we came up here for FTBpro. But eventually I realized I really want to start from the beginning. So this post will involve no code (none!), and hopefully would make a lot of sense for beginners (in Ruby, but in many other things too).

It’s about style.

About 3 years ago I started my first real job as a software developer at a small startup company. In a short period of time, I worked on many projects with different, mostly unrelated languages, that were all new to me: PHP, JavaScript, C#, Java, and a touch of HTML with CSS. It was incredibly challenging and interesting and I think it’s actually a great way to start because it forces you to look at the bigger picture, and teaches you how to learn.

Shut up Ruby, and take my money!

But a year later, that startup didn’t do so well and I was looking for something new. I took some time off to learn something new, and bumped into Ruby on Rails. I fell in love - because I compared it to the code I wrote in all those other languages and I thought to myself - this looks so much better! I went through a few open source projects and read some code, and everything was so concise, and readable and nice. I wanted to write code like that too!

So while I was already interviewing for a few jobs, I decided to turn the offers I got down, took some time to brush up my Ruby skills, and then applied for a job as a Ruby on Rails developer here at FTBpro.

Not all that glitters…

And in the beginning, it was good. I was writing code that looked much better! But something still felt off. A month in, I had another look at something I coded. It was horrible, a complete mess, and it reminded me of the often unreadable PHP code I was working with a year before. Where did I go wrong?

It’s the gap!

Ira Glass, host and producer of the radio show This American Life has a great video where he talks about taste. I embedded it below and you should really watch it. It’s relevant for many fields, but I can really see how it relates to programming, too. Ira gives the example of being a TV junkie. You have your favorite shows that you love everything about, and you have the shows you hate, that you can’t understand who would ever approve. Then you get the opportunity to actually start producing your own TV show. You know what you like, but when you’re just starting out, everything you make reminds you of the shows you hate. Or worse.

Ira calls the reason for this “the gap”. You know what you love, and this passion you have is the reason you are the one doing it in the first place (and not someone else). But you’re just not experienced enough to produce something that’s anything like what you love and consider good!

Just keep hacking

The same happens when you’re writing code. Many of us are doing this because we like to solve problems, but we also really enjoy solving them in “the best way”. Be it the fastest and most efficient way, or most concise and readable. Maybe you just value being able to deliver bug-free code faster than anyone else. Whatever it is, there must be something that you consider great programming, and you might feel like you’re not quite there. Some will think this is a good reason to stop striving for perfection.

But it’s the opposite.

It’s your ugly, inefficient code, that will teach you how to get to where you want to be. Keep reviewing everything that you write, a week later, a month, a year. Ask for help. REFACTOR. Embrace your mistakes and you’ll get there.

2 years after I started coding in Ruby, I still look at my code sometimes and feel a little ashamed. But it’s happening much less now. And I’m getting there :)

P.S. Maybe next time we can talk about how writing specs helps you improve your style and close that gap!


Udi Wertheimer, Ruby on Rails developer @ FTBpro

blog comments powered by Disqus