Saturday, April 16, 2011

Why Ruby and Rails?

"Why should we use Ruby and Rails?" has been popping up at work over the last month. Managers are worried about retraining the Java developers. And the Java developers are worried about wasting their time on some fad. Here are my reasons why you should be using Rails to build a web site.

First, Rails is a web framework, nothing more or less. It claims to do nothing but allow developers to easily create websites. It makes hard problems trivial and impossible problems doable. It claims a 10x improvement in development time. That claim is VERY DIFFICULT to believe, so I will address it later with examples from my current project. Rails should be thought of as a Domain Specific Language (DSL) for web applications. Writing templates or views of web pages is eased with many helpers.

Think of scriptlets in JSPs, but terse code that makes sense. The helpers range from creating links to creating whole forms. All of this goodness is packaged up in an MVC pattern. Unlike Struts, which also claimed to be MVC, there is a single layer for Controllers; each layer is contained in its own directory. Another benefit is the directory layout is standard. If you know the directory layout, every Rails application will be familiar to you. This is a big benefit after you have switched to your upteenth Java project which has it's own directory structure, completely different from all of those that came before it.

Second, you get RESTful web services for free. I can't stress this benefit enough. When you use the generators for Rails you get the server side of a the web service. Building the client is trivial. Remember I said I would prove the 10x improvement in developer time? We are building an application which will have many clients. The clients are mostly Java, so a developer was assigned to write the client for the RESTful Rails application. He decided to write a parser in Java. This took a couple of weeks to write and test. And from time to time we find new bugs in the code.

However, we could have written three lines of Ruby that would have done the same thing. I literally implemented the same thing he had been working on for two weeks in 10 minutes. 8 of those 10 minutes were trying to find the documentation on what needed to be written (sadly when I showed him the Rails way he shrugged and said, I know how to do it this way and I already have this time spent on it. Maybe I should have explained sunk costs). 10 minutes vs 2 weeks, 10x improvement in speed might be an understatement. This isn't the only way Rails improves implementation speed. Writing database queries are a breeze.

Rails comes with its own Object Relational Mapping (ORM) tool called ActiveRecord. Similar to Java's Hibernate in functionality, ActiveRecord improves development by making complex is easy. Rails provides generators to create scripts to create tables in your database of choice (support for many databases is provided including MySQL and Oracle). The generator also creates a Model class for each table you have in the database. The Model class is where you write the code for business logic and any methods needed to access the data. Except, you don't need to write much because Rails provides many access methods without the developer needing to write any code. Rails also helps by providing helper methods to create relationships between objects. ActiveRecord provides support for one to one, one to many and many to many relationships with just a single line of code in each Model class or two lines of code in each Model class for many to many relationships (technically you can do many to many with one line of code in each Model class in the relationship, but that method has been deprecated in favor of the more flexible two method approach).

The last time saving feature of Rails is that you can use Ruby gems in your web applications. Gems are like Java's jar files, they are packages of code that can be thought of as a library. Gems easily support dependencies so having a Gem that requires ActiveRecord is easy to create, which means you can create Gems that natively work in Rails applications. Need a User object in your application? Download one of the many authentication gems available on the web. Find the one that suites your need, install it and you are ready to go. Need to upload files? Install a file upload gem. This capability is available in Java applications too, but for some reason they are more complex.

Finally, Rails is a simple and easy way to get a web application up and running. A developer can finish a week long Rails course with enough fundamentals to be ready to go write a Rails application.

No comments: