Thursday, October 9, 2008

Why Ruby is My Favorite Language

I started programming seriously roughly ten years ago. I was working in the call center of an ISP, and there was a need for a reseller tracking application. I was attending the local community college at the time, and had been indoctrinated into the world of computer programming trhough what amounted to vocational training in Visual Basic and Microsoft Access. Fortunately, the ISP was a Linux shop, and a few employees were nice enough to introduce me to the basic philosophy of Linux and assist in learning some rudimentary shell commands.

One of these employees was a Perl hacker, and he assisted in convincing the company to form a small team of three programmers to tackle the programming job. I was lucky enough to be chosen for the position, and to be frank, after having spent a few months in the call center, I would have considered janitorial duties a promotion. The only problem was that I didn't know Perl. I quickly ran to the nearest bookstore, grabbed a copy of Learning Perl, and set to work learning the language. A week later, I was neck deep in the project and thankfully had an experienced programmer to lead me through my first big job. The project was a success, and the team became a permanent fixture at the company.

What impressed me so much about Perl was the succintness, mneumonic friendliness, and consistency of the language. After a few weeks of intense study, I could perform the majority of my daily programming tasks from memory and rarely needed to consult a reference.

A year or so later, PHP hit the scene. With the huge buzz surrounding the language, I figured there had to be something really groundbreaking going on. The team I was working on chose to build a large project in PHP, and once again, I set out in earnest to learn the language. This time, things weren't so easy. While Perl had "sunk in" right away, PHP lacked the natural abstraction I had come to love about Perl. As an example, I didn't understand why PHP needed sixteen sort methods (sort, arsort, asort, krsort, ksort, natsort, natcasesort, rsort, usort, array_multisort, uasort, uksort, dbx_sort, imap_sort, ldap_sort, yaz_sort) while Perl only needed one. The database methods lacked the wonderful platform independence of DBI, and a lot of other things felt redundant. Mostly though, I disliked the way the language encouraged mixing interface with your implementation. At the time, the PHP website frequently featured examples interpsersing functions and HTML and even touted this ability as a "language feature".

Progressing through my courses, I ended up taking a class on object oriented design. I learned about the model view controller architectural pattern and immediately saw it's advantages as a design strategy. I learned the basics of object oriented programming with Perl, but I was dissatisfied with the way it was implemented. I spent a considerable amount of effort learning Java and later Python. I found both languages to be "good enough" in the way they possessed a healthy amount of easily accessible OOP goodness, but I missed the fluidity of programming in Perl and neither ever became a "favorite".

At this point I was working at an environmental laboratory, and I was given complete freedom to use whatever technology I wanted to solve the problems at hand. I was tasked with writing a data analysis and computation software for an ICAP (Inductively Coupled Argon Plasma Spectrometer), and I was again at a crossroads. An article on Slashdot had drawn my attention to the Ruby programming language, and I found an excellent book that was freely available online. Reading the introductory walkthrough section, I was immediately impressed by the fact that literally everything in the language was an object. The core language was incredibly consistent, concise, and well documented with absolutely no cruft. All of the wonderful Perl idioms I'd come to know and love were right there at my fingertips; albiet, in a more readable fashion. I was sold.

I learned the language and developed the project simultaneously. This was long before Rails was even a whisper in the programming world, so I rolled my own MVC style framework containing a presentation layer that used a combination of XML, eruby templates, and LaTeX. This made it easy for the app to generate both HTML and PDF documents on a whim. The development cycle was fast, the project was finished ahead of time, and everybody loved the final product. Later I wrote a C extension for Ruby to easily access the Win32 serial port allowing an archaic lab instrument to dump data straight to an OpenOffice spreadsheet. I'd never considered writing a C extension to any other language, but again, Ruby made it easy.

Since then, Ruby on Rails has rolled onto the scene and has allowed me to develop projects in which I would have needed a team in the past. MVC is the core paradigm and being able to use my favorite language alongside my favorite architectural pattern is nothing short of a godsend as a web developer. I've used Rails to develop web applications on the fast track and have brought in a nice supplementary income doing so. Best of all, I feel good about the final product when writing a Rails app knowing I'm developing in a standardized way where another programmer will be able to come along later and easily maintain the work I've done. There are MVC frameworks for PHP, but there's still no de facto standard way to roll a PHP app. This makes every PHP app a "special surprise" when you look under the hood. Sadly, the surprise isn't always pleasant.

I'm not saying you can't write good code in PHP. Actually, working at Grooveshark has shown me that it's possible to write extremely clean code in PHP, but I still run to the PHP manual several times a day to lookup one of it's 3000+ core method signatures.

I'm acutely aware that languages are like religions among developers and that everyone has their favorite for one reason or another. I'm also certain there are people out there that would disagree with some of the arguments I've made here. Arguments aside, if you're looking for a new language to learn, and you haven't tried Ruby, take a look, and I think you'll be pleasantly surprised at all that it has to offer.

7 comments:

John Speno said...

I can't believe I used to go out with you. Python rules! ;-)

Anonymous said...

I really like the elegance of Ruby syntax above all. Immediately after learning the basics of the language I wrote an article praising the terseness and clarity of this language.

My background is different than yours (Pascal > C > C++ > Java > PHP > Ruby), and I'm still looking around for interesting programming languages to learn (Haskell, Scheme and Lua are all interesting, in different ways), but nobody beats Ruby as far as "coding pleasure" goes.

I'd love a *proper* compiler though, or a significantly faster VM with good gem support.

Anonymous said...

Travis...you're such a BAMF.

Madhav Selvan said...

Hi .. i also 've been working in ruby in the recent past nd i find it gr8 working wth it as it's d simplest lang wen someone moves from c or c++ into these types of languages ..

Leslie P. Polzer said...

If you like Ruby you might be on your way to Common Lisp. :)

Travis Whitton said...

@pythonic avocado

I'm actually a big proponent of Python having written over 50,000 lines of code for a LIMS (laboratory information management system) prototype in the past. It's a fine language no doubt and is probably my second favorite. I actually look at Python as a target for where ruby needs to go in regard to compiler maturity. It seems to have a nice separation between compiler and runtime in that it dumps everything to bytecode on the first run. Hopefully parrot can fill the gap at some point.

@leslie p. polzer

I worked through ANSI Common Lisp by Paul Graham years ago. It's a fantastic language, and it was my first introduction to functional programming. I've adopted a lot of the fundamental ideas of CL in my daily coding, namely, avoiding side effects, focusing on return values, recursion (when appropriate) and even making use of the infamous lambda function from time to time.

Unfortunately, I haven't really had an opportunity to use CL in my professional life; although, I'd like to get back to the language at some point and see what the offerings are regarding things like SQL and web development.

I also had a brief stint with SML; although, it drove me slightly mad.

Anonymous said...

Looking for some Ruby snippets?

http://snippets.dzone.com/tag/ruby

http://codesnippets.joyent.com/tag/ruby

(And, oh yes, avoid using inject, it's slow as hell!)