Tuesday, March 6, 2007

Ruby and DRY

What is DRY? It stands for "Don't Repeat Yourself" and is one of the driving principals behind ruby and rails.

Why is it important?

Well, I was working in Java this evening and using the Hibernate ORM one of the premiere ORM libraries for Java. I tried to add a column to a table, and was dismayed when it wouldn't work. It refused to use the new column. What a piece of junk, I thought! Then I found the bug, in the xml property file I had used the word "properties" instead of "property", and hibernate just conveniently ignored my new column.

Hibernate (AFAIK) defines columns in no-less then five places!

1. In MyClass.hbm.xml
2. In MyClass as a field: myField
3. In MyClass.getMyField
4. In MyClass.setMyField
5. In the Database as a column: MyField

Ruby on Rails?

1. In the Database as a column: MyField

Now, if you had to modify your code to add a column to a table, would you want to do it in RoR or Java/Hibernate? I thought as much ;)


Matt Taylor said...

You're looking at this all wrong!

'Memetically speaking' from the database column name's point of view the Java ORM pattern is clearly superior.

James Dwyer said...

Either way, if you don't remember what methods or fields a class has, you have to go somewhere to find that information out.

In Java, you can browse to 2 places (the class or the database). In Ruby, there is only one place you need to go, the database. Again, providing the "one stop", I know where to go to find out the information I need to know.

Ideally, your IDE would figure that out for you, and you can use autocompletion. I don't find it impossible to believe that Rails IDE's will eventually look to the database to figure out what fields a class has.