Home>Articles>
Crossing borders: What's the secret sauce in Ruby on Rails?
|
Ruby on Rails (aka Rails) is a Ruby framework for database-backed Internet applications. I've now used Rails for two different applications and been involved with two others as an associate. For an upcoming book called Java to Ruby (see Resources), I've interviewed many Rails developers (people who have both succeeded and failed with the framework), the framework's founder, and the lead author of the flagship Rails book Agile Web Development with Rails (see Resources). I'm starting to understand why the Ruby on Rails framework is so successful. Debates about Rails in the Java community have been intense and show no sign of dying down any time soon. Rails proponents boast of incredible productivity, with some claims of 10 to 1 over Java development. As a Java programmer, your knee-jerk response is to dismiss any wild productivity claims because you've likely heard them before and been disappointed. Java advocates increasingly insist that Ruby on Rails is a toy that can't scale, produces bad code, and won't work beyond the simplest applications. But as Rails praise keeps popping up -- often from credible sources -- a more prudent course might be to understand what Rails does well and to bring those ideas back to the Java platform. In this article, I'll explore the core features -- the secret sauce -- that are the essence of Rails's great productivity. In the Crossing borders series, author Bruce Tate advances the notion that today's Java programmers are well served by learning other approaches and languages. The programming landscape has changed since Java technology was the obvious best choice for all development projects. Other frameworks are shaping the way Java frameworks are built, and the concepts you learn from other languages can inform your Java programming. The Python (or Ruby, or Smalltalk, or ... fill in the blank) code you write can change the way that you approach Java coding. This series introduces you to programming concepts and techniques that are radically different from, but also directly applicable to, Java development. In some cases, you'll need to integrate the technology to take advantage of it. In others, you'll be able to apply the concepts directly. The individual tool isn't as important as the idea that other languages and frameworks can influence developers, frameworks, and even fundamental approaches in the Java community. The Ruby on Rails framework is not a typical application-development framework by any stretch of the imagination. Rails founder David Heinemeier Hansson often calls the framework opinionated software, and he enjoys breaking long-standing conventions. David has made strong philosophical decisions and strictly follows them throughout the framework. Some of the core opinions pervasive within Rails are: The opinions that underlie Ruby on Rails are all well grounded in practical experience. The Rails framework grew out of practical use in the development of the popular Basecamp project-management application (see Resources). The argument against wild productivity claims usually goes something like this: If I've got a decent hammer, I'd be hard-pressed to find another hammer to make me twice as productive, let alone 5 to 10 times more productive, because hammers have evolved over thousands of years. But people who compare Ruby on Rails with a mix of general-purpose Java frameworks are missing the point. You can be 10 times more productive for some problems by radically changing the nature of the tool. Professional framers now use nail guns that can drive in dozens of nails in the time it takes to hammer in one nail. Like nail guns, Rails is specialized. It's a framework written with a laser focus on a single niche: new database-backed Web applications. I'd guess that as many as half of the applications built today are database-backed, Web-enabled applications. So Rails is definitely a niche product, but its niche is large and important. Specializing in this space gives Rails huge advantages that account for the big splash that it has made. By focusing on projects in this niche, Rails designers can take some shortcuts that other frameworks cannot or should not take. This specialization often sacrifices flexibility for simplicity. New, database-backed applications suggest a wrapping approach over a mapping approach. As you saw in the first article in this series, Rails tools assume conventions in the data model. Rails applications need a fraction of the model code found in Java applications. If you're creating your schema especially for your Rails applications, this philosophy works well. If you're trying to shoehorn a legacy schema into Rails, things will go less smoothly. Web-based applications allow a similar set of optimizations. When you know an application is Web-based, you know the general structure of the application and the major components that it's likely to need. The following features are enhanced in Rails because the Rails focus is on Web-based applications: But nail guns will never replace hammers, and we'd be foolish to expect complete replacement. Hammers will always do things that nail guns can't. Rails will never be a tool for enterprise integration, object-relational mapping, or full-stack Web services. The best you can hope for from Rails is a specialized tool that fills its niche well. As you start to dig under the covers, you begin to understand how radically different the Rails developer experience can be. A rapid feedback loop, interactive consoles at every turn, and convention over configuration all enhance the developer experience with dimensions not frequently available in Java frameworks. One of the most important factors in developer productivity is the overall feedback loop. The feedback loop is the amount of time between making a change in code and seeing the results in the execution of your application on the screen. In Rails, you get instantaneous feedback as you code. That feature is most notable when you make changes to your Ruby code. You can immediately load a browser page to see the results of your changes. Because I don't need to compile or deploy during development, I tend to make only tiny programming changes before I reload my browser or execute my test cases. Just about every Java developer who begins to use Rails codes in smaller chunks. You might expect a rapid feedback loop that's friendly to the developer experience to be hostile to production applications. After all, the frequent class reloads that enable the rapid feedback loop should slow production applications to a crawl. But Rails works around this problem by giving you different environments for deployment and development. The development environment forces frequent class reloads at the expense of application performance, and the production environment reduces reloads to a bare minimum, providing a fast end-user experience at the expense of a rapid feedback loop for the developer. The interactive experience of Ruby also contributes to Rails. You might expect debugging a Rails application without a full IDE to be a painful experience. It's not. Rails provides two features that simplify debugging. One of them is the breakpointer, which lets you add the To see the breakpointer in action, create a simple Rails application, generate a controller, start the server, and start a breakpointer. Make sure you have access to the breakpointer window, because you'll use it when Ruby encounters the breakpoint. Using Windows, the sequence looks like this: If you're running in UNIX® or Mac OS X, just make sure that your server starts in a separate process. Type or paste the following code into the app/controllers/samples_controller.rb file: Test the code by loading the pages localhost:3000/samples and localhost:3000/samples/show. When Rails executes the breakpoint, the application hangs. The breakpointer window opens a Ruby interpreter with the environment having the current state of the controller. You can then issue Ruby commands to query the state of your session, execute methods, and query variables: This nice touch doesn't give you a full debugger, but you do have some capabilities that Java debuggers don't give you, including access to a full interpreter and the ability to execute methods on your application. The second feature that simplifies debugging is the Active Record console. In the first article in this series, you saw that Rails also comes with a script that lets you work with your persistent objects in an interactive Ruby interpreter window. I've often wanted such a capability within my Java applications. You can code a persistent model, make changes to the database through the model, and then run some database queries to see their impact on the system. It would be nice to be able to query Hibernate objects in a similar setting. Convention over configuration also leads to "aha!" moments for new Rails developers because the controller and model code is so concise. You saw in the first article that by leaning on the Rails environment, you can get some pretty advanced behavior out of some very thin classes -- by adopting Rails naming conventions and letting Rails infer your application's connection points instead of configuring them directly. To review, a No configuration is necessary because Rails infers the name of the table ( What can Java developers learn? I don't recommend that we build a better Rails in the Java language. Instead, Java developers should learn some lessons from the Rails framework and seek to build or enhance Java frameworks to do all of the following: By incorporating the best features of other programming languages, you might not reproduce Rails, but you definitely can improve the Java experience. |












