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.

Product Development Lesson: Abigail's Purse

A little over a year ago I sat with my daughter after dinner one night and we chatted about entrepreneurship. She was 13 going on 14 but she understood the concept and started to run with it. We talked about what would be a good product. She was going through a very crafty phase. Not crafty as in mischievous, but crafty as in she was painting, building origami, and making artsy things. She enjoyed it and I suggested we should find one of those craft projects that other people might like and turn it into something people would like to buy. We decided one particular project had promise. The craft project was a handbag woven with plastic strips.

She got to work trying to change a craft project into a product. The first discussion was the materials. I told her we were never going to be able to find plastic strips, but we looked on the internet anyway. Nothing was available. I thought we would have to have the strips made for us specifically. So we started talking about alternatives to plastic strips. She came up with using ribbon instead. I liked it because I thought it would make the purse soft and warm vs the cold hard plastic. So we made a trip to Michael's and purchased a ton of ribbon. Problem number two, we had to find a better supplier of ribbon, Michael's was expensive.

She started making the purse out of ribbon and quickly found the next problem. Ribbon doesn't stand up like plastic does. Being the trooper that she is, she tried to make that work as hard as she could. Finally she mentioned that she was having a problem. She needed some kind of support system to allow her to weave. We spent a week or two thinking about this problem and talking about solutions. We tried a couple and failed and some how ended up ruining the ribbon.

This time we went to Joanne's for the ribbon. Who knew ribbon would be so expensive. We got a couple of blocks of styrofoam from Michael to serve as the support system. We cut the styrofoam to the size of the purse, wrapped it in plastic wrap and off Abigail went to build another purse.

This time it was a success. We talked about the fit and finish, it needed a liner, and some kind of tough base. What we ended up with was the purse you see above. I thought it was pretty awesome. She started using it to carry all of her dance gear. And then we stopped working on the purse.

Yesterday we were walking through Wal-Mart and we saw rows and rows of woven bags. A year has gone by since we started working on the purse. But the first thing Abigail said to me when she saw the purses, "They found the plastic strips." The bags are bigger. They are for going to the beach. They are rugged and cold. And they are $7. Each of Abigail's purses were in the $30 range just in ribbon.

The night before the trip to Wal-Mart Abigail mentioned she was ready to do the purses again. That night I searched for ribbon again and I purchased a ton of ribbon that I had found very cheap, 1/40th the cost of Michael's and Joanne's. The ribbon will show up some time soon, and we will make a couple more purses. Hopefully someone on eBay will find them as wonderful as we do and pay a decent price for them.

Lessons:
* Someone always has the same idea
* Get your product out as quickly as you can
* Keep searching for lower cost materials (without sacrificing quality of course)