Last night, I attended the Nova Ruby User Group (NovaRug) meeting in Reston. Chad Fowler was the speaker for this month’s meeting and he gave a talk on writing clean rails code. The title of the presentation was ‘Quick & Clean’. Chad explained how there are usually two kind of solutions when designing a software system: the slow and clean solution or the quick and dirty solution. He believes however that with Ruby on Rails, it is possible to achieve a Quick & Clean solution. He said he first heard about that train of thought from Martin Fowler at RubyConf.
The problem is that a lot of times, Rails is used to produce Quick & Dirty coding. While working as a consultant, he claims to see a lot of dirty rails code. So while ‘quick and clean’ rails is possible, it is not probable. Thus, his talk was a grab bag of strategies and best practices to write clean rails code. Chad jumped from topic to topic and kept the crowd entertained for about an hour and a half before opening the floor for questions.
Here is some of the stuff he pulled from his grab bag:
Ruby on Rails makes it easy to build Leaky Abstractions. To avoid these, he thinks the best solution is to learn and master Ruby so that you at least understand what goes on below rails. This will help you write code that is more clean, faster, and more efficient.
Ruby gives you a lot of rope. Compared to Java & C++, it doesn’t tie you into a set of rules and lets you do pretty much anything you want. This is great if you are a good programmer but it also makes it prone to making big mistakes. He said that Java was designed with the mindset that it will be used by dumb monkeys. Therefore, the designers put mechanisms in place to avoid the monkeys from making stupid mistakes. In the process, there are more constraints on the things you can do. On the other hand, Ruby was designed for smart programmers. It lets you do a lot of things. The problem is when monkeys try to use it they can make cataclysmic mistakes since nothings stops them from doing so.
CRUD stands for Create, Read, Update, Delete. Chad said that as a Rails developer, you have to force yourself to CRUD. When designing an application, force yourself to architecture it in a way that the only operations needed on a model is Create, Read, Update, & Delete. This will make you write cleaner code. He also mentioned that the Rails core team is going to enforce this in Rails 2.0.
Chad advised against putting SQL in controllers. Move your SQL to the models. Also, a controller action should never have more than 5 lines. Force yourself to follow that convention and your code will be much cleaner.
Dry up your controllers with before_filters. It helps eliminates lines of code.
Sometimes you just have to make your own language. He quoted ‘Good APIs are languages’. For some applications, you might need to develop an API with functions handling stuff you do often.
Do not put code in your views. Hide the ugly things where you do not want to have to look at them. Put them in helpers or in the models. There is always more opportunities to clean up your views.
Check out simply_helpful plugin. He claims it can help you a lot and will be included in Rails 2.0.
He recommended to read the book: ‘Leading The Revolution’ from by Gary Hamel. It’s a business book that he claims will help you in every aspect of life.
Chad briefly talked about the effect of globalization on software developers in the US. One interesting thing he said is that India is now outsourcing developers to China because the cost per hour per head for a developer in China is much lower than in India. Also, China is now mandating English at school.
A few of other things that was mentioned that you might want to google: RJS, with_scope, Amrita, update_page, Naked Objects, Hobo.
To finish, Chad stated that to be a good Rails developer, you need to become an expert at Ruby. It will help you see through the leaky abstractions and write cleaner and more efficient code. I was very glad to hear that. On my path to learn Ruby on Rails, I had decided first learn Ruby before I start learning Rails. So it made me realize that I might have started the right way.
I was very glad to attend this NovaRug meeting and I look forward to
the next one. If you live in the Northern Virginia area, I would
highly recommend coming to the meeting if they keep being this
interesting.