Pick Natural Names (while programming, especially testing)

I’m reading a wonderful book named “Agile Web Development With Rails“, which is about how to develop web applications quickly (agilely) with Ruby on Rails.

In the chapter on how to do testing with rails, it gives out an interesting suggestion: use natural names like “Christmas_order”,”Fred” instead stiff names like “order1″,”customer1” we are used to. I really like this idea!

Just like the names of variables in general, you want to keep the names of fixtures as self-explanatory as possible. This increases the readability of the tests when you’re asserting that product(:valid_order_for_fred) is indeed Fred’s valid order. It also makes it a lot easier to remember which fixture you’re supposed to test against without having to look up p1 or order4. The more fixtures you get, the more important it is to pick good fixture names. So, starting early keeps you happy later.
But what to do with fixtures that can’t easily get a self-explanatory name like valid_order_for_fred? Pick natural names that you have an easier time associating to a role. For example, instead of using order1, use christmas_order. Instead of customer1, use fred. Once you get into the habit of natural names, you’ll soon be weaving a nice little story about how fred is paying for his christmas_order with his invalid_credit_card first, then paying his valid_credit_card, and finally choosing to ship it all off to aunt_mary.
Association-based stories are key to remembering large worlds of fixtures with ease.

学习Ruby on Rails

最近在学习Ruby on Rails,简称RoR。Ruby是一种编程语言,日本人发明的。Rails是Ruby的一个framework(框架),用来开发web程序的。Ruby原本一直不温不火的,主要在日本国内被使用。自从有了Rails这个东西之后,RoR这个套路就一下子火爆了起来,成了“快速开发”,“敏捷开发”理念的代表。

学了一小阵子之后,有了一些感性认识。最大的感受就是RoR开发确实很快,需要写的东西很少。。它的网站上有个挺著名的介绍视频,叫“Creating a weblog in 15 minutes”,就是在15分钟内建立一个blog系统,视频里面的哥们嘴巴和手指都动作得飞快,不过还是看得清楚的。可以看到RoR把流行的那些东西基本都包括近来了,MVC,ORM,Ajax之类都是内建支持的。值得注意的是那个Scaffold(脚手架)功能确实很发指。。。直接就把“增删改查”这几个功能一下子全做出来了,用Java那套写这些东西谋杀了我多少时间啊-_-

我觉得RoR里面有一个比较独特的理念很好,就是Convention over Configuration。用Java进行Web开发的时候总是要去面对大批的XML配置文件,即使有IDE帮忙自动生成,仍然是一件挺麻烦的事情。RoR里面基本都是有一个默认的配置的,你什么都不写,那么就会去按照convention来。比如一个controller的action执行之后就会默认跳转到相同名字的view去,在java里面都已经习惯写一些充满这类“废话”的XML了。再比如那个神奇的Scaffold。。。

Ruby是一种脚本语言,写起来很灵活,现在为止我看着Ruby代码还是觉得有点乱乱的,似乎怎么写都行的感觉,哈哈,脚本语言都这样。这样和RoR这种“敏捷”的想法比较贴合,可以尽可能的少写代码。

不知道Java Web开发世界里面会不会也出现按照这种理念开发出来的framework,呵呵。。。