顯示具有 比較 標籤的文章。 顯示所有文章
顯示具有 比較 標籤的文章。 顯示所有文章

2/28/2007

一個在 Rails 跟 Django 徘徊設計師的真情告白



AjaxWhoIs 這個網站的作者最近發表一篇文章 Why I moved from Ruby on Rails to Python/Django and back,講解他開發 AjaxWhoIs 2.0 的時候為何先用 Rails 開發,後來採用 Django 開發,最後還是轉回 Rails 的紀錄。

Any newcomer to Rails will quickly discover why it is so talked about. First of all, even though I don’t consider myself anywhere near a decent Rails programmer, I am now at least twice as productive with Ruby on Rails than with ASP.NET and C#. Thanks to the MVC (Model, View, Controller), DRY (Don’t Repeat Yourself) and Convention Over Configuration mindset of Rails.

他一開始是一個 ASP.NEt 跟 C# 的程式設計師,所以他是第一次使用 Rails ,但是當他使用 Rails 開發的時候,他發現 Rails 的三大法則 MVC ,DRY ,Convention Over Configuration 至少讓他生產力比原本很熟悉的 ASP.NET 快了兩倍以上。

I found out that Mongrel was known to not play well with 64 bit Ubuntu (I lost the links to this evidence, unfortunately). Mongrel was patched accordingly, but it didn’t solve my problem. It still crashed many times a day and I just couldn’t figure out what was wrong. I asked my good friends Rich Collins and Adam Thorsen (of Guruza.com) who are both experienced Rails developers and they had no idea either. I was stuck.

但是當他寫完AJAXWhoIs 2.0的時候,他發現到 Mongrel 跑 64bit Ubuntu 的時候有詭異的問題,導致 Mongrel 常常當機,屢試不爽。

I picked up a Python book and rewrote the whole site one more time, in Python using the Django framework this time. I had never programmed in Python before and much less used Django. However, the switch was easy enough since Ruby and Python are somewhat similar.

相當令人覺得很了不起,也相當令人覺得不可思議的事情發生了。他已經寫完了整個 Project ,只是遇到了這個 Hardware 問題,他居然跑去用 Django 重新寫了一次 AjaxWhoIs 2.0,而且這還是他第一次寫 Python。一般人應該都會換台機器跑就好了不是嗎 @@!

However, I soon missed Ruby and Rails. Ruby supports “real” private/public/protected methods (Python just fakes that with its naming convention) and it’s syntax is more forgiving (the need for () at the end of each method call in Python was killing me). Django is not as straightforward as Rails, and requires more code to get things done. There are also many little things that don’t “feel right” in Django, like the need to manually pass variables to a view. Data access is confusing at best while Rails’ ActiveRecord is god-sent. These little things really add up.

但是當他寫完的同時,他開始相當懷念 Rails 了,有許許多多小地方他認為 Rails 做的比 Django 來的好,Rails 作法也比 Django 來的更 straightforward 。要完成同樣一件事情,Django 也需要比 Rails 更多 code 。 Django 有太多東西讓他覺得 don't feel right了。

On the flip side, Python is faster than Ruby and less memory hungry. I was also surprised to actually love Python’s significant indentation (see example). But more importantly, Python and Django just worked! I never experienced weird crashes like I did with my Rails stack.

的確,Python 比 Ruby 快,需要 memory 也比較少。而且最重要的是,Django 可以動,Mongrel 在 64 bit CPU 不能動。

So, why am I back to Rails for my next project? 3 letters: FUN. I find Ruby and Rails to be pleasant to use. The community large, active and very helpful. The number of freely available Rails plugins and the fast evolution of the core code are also welcomed additions. Rails requires less code, less self.__awkward_method_calls(), has built-in AJAX and REST support, and has much more flexible data access and templating engines.

而為何他又要跑回 Rails 了呢?Just for FUN。他發現到 Ruby on Rails 會讓人非常愉快,社群很大,活動力強,而且都會互相幫忙。免費的 Rails Plugin 跟 code 快速的進展都很棒。Rails 需要較少的 code ,較少的可怕的 method call,而且還內建 AJAX 跟 REST。而且 Data Access 跟 template engine 都更有彈性。

But what about those crashes? What about the speed issue? Well, I don’t have the crashes anymore. Don’t ask me why, I don’t know, but it’s fine now, I swear! Something somewhere got fixed and it seems to have solved the problem. However, speed is still one of the low points of Rails. Using caching and proper code optimization should take you a long way, though. Matz, the author of Ruby, is working hard on a new virtual machine that should make Ruby just as fast, if not faster than Python.

最神奇的是,當他回去 Rails 時,Mongrel 不再 crash 了@@!不知道Mongrel 哪裡修正了,反正問題解決了。但是效率依舊是一個問題所在。

My recommendation is, try both for long enough to figure out what works and what doesn’t for you. If you are a long-time Python user, Django might be more compelling for you, but if you are coming from a Java, Perl or Smalltalk background, Ruby and Rails will most likely be what you’ll end up using. Either way, I don’t think you can go wrong.

他的建議是如果你是 Python 長期的使用者,用 Django 吧,如果你是 JAVA,Perl,Smalltalk背景的人,來用 Ruby on Rails 吧。

我的小結論:

從他的字裡行間可以看出幾件事情

  1. Rails 跟 Django 開發時間都很快速
  2. 他真的很不會利用時間,居然用 Rails 跟 Python 各開發了一次,只是因為 Mongrel 對 Hareware 有點 bug

12/28/2006

該選擇那個 OS 作為 Ruby on Rails 伺服器的環境?

小弟雖然不才,但是有一點我很自傲。我的使用 OS 相當的廣,MAC OS X,Gentoo Linux,Ubuntu Linux 都是我在日常生活中使用的 OS。並且 FreeBSD,Windows,Debian Linux,Fedora Core Linux 都有機器管理的經驗。由於有很多機會可以自由轉換各種 OS,所以我不執著於任何一個 OS,並且深信
OS 只是 Tools ,真正決定一切的關鍵是管理者
那麼要我推薦 Ruby on Rails 的 OS 呢?我會怎麼選擇?

適合研發 Ruby on Rails 用的 OS

所有的 OS 都可以,只要你喜歡,你習慣這個 OS 的操作,可以灌 Ruby on Rails 還有 Mongrel ,都沒任何問題。當然 IDE 也是很重要的因素,所以我比較推薦可以安裝 TextMate 跟 RadRails 的 MAC OS X。但是 RadRails 因為是跨平台的,所以 OS 之間差距沒那麼大。

適合 Ruby on Rails 伺服器環境的 OS

我首先不推薦 Windows ,因為許多報告都顯示
這些都告訴我們 Ruby on Rails 對於 Windows 的支援度很弱。

FreeBSD 是一個很適合架站的環境,但是我曾經看過 Mongrel 作者 Zed Shaw 說過 Mongrel 在 FreeBSD 跟 MacOSX 的效能不佳,只是現在那篇文章似乎已經 Zed 被拿掉了,或許是他已經改進了效能。這篇講解 Scale 的文章底下的 Comment ,也有人出來問類似的問題
Justin said about 18 hours later:
I believe Zed mentions on the Mongrel site that there are performance issues when running Mongrel on Mac OS X and FreeBSD. Given that you're running on FreeBSD, have you experienced any of the (relative) slower performance running Mongrel on FreeBSD?
但是作者也僅僅回答他沒有作過類似的效能測試。所以這就是證明 Zed 的確有說過『Mongrel 在 FreeBSD 跟 MAC OS X 上面比較慢』類似的話,但是他有沒有改進 Mongrel 讓他更合乎 FreeBSD ,似乎不得而知。

Linux 方面,目前似乎沒有效能上的負面消息傳出來。

結論

安全性跟穩定度方面,各個 OS 上面的表現應該是要看管理者功力。至於效能方面跟相容性的考量,Windows 最不推薦當作 Ruby on Rails 伺服器環境。MAC OS X 跟 FreeBSD 等 BSD 系列有 Mongrel 作者的對於效能上面的負面報導。相對的,Linux 目前還沒有效能上面的負面報導,可說是這方面的贏家。但是一個好的管理者也可以將系統調整到相當快速的境界,所以我認為 BSD 跟 Linux 並沒有誰適合當 Ruby on Rails 環境的贏家。還是那句話
OS 只是 Tools ,真正決定一切的關鍵是管理者


11/11/2006

Tim Bary 看 Rails,PHP,Java

從台灣Ruby論壇發現的,Anw跟Contagion總是可以找到一些我沒注意到的新聞@@!。Sun 的 XML 標準製作者 Tim Bary 發表他對 Rails,PHP,Java 的看法。出乎意料的,在 Sun 自家人的眼裡 Java 並沒有佔了上風。
Rails 贏在開發時間,跟後續的維護,可擴充性被認為最差(雖然我不認為Rails Scaling 很差)。PHP贏在可擴充性,但是後續維護被評為最差。Java 在開發工具上勝利,但是開發速度自己人都覺得不夠好。他的 Slides 可以在這裡 Download。

他的評比項目包括
  • Scaling:Load balancing,Shared-nothing,CPU ,DBMS,File I/O,Observability
  • 開發時間:Compilation step?,Deployment step? ,Code Size ,Configuration process
  • 開發工具:IDE,Templating,How many tools?,O/R Mapping ,Performance , Documentation
  • 後續維護:MVC,Object orientation ,Readability ,Language count ,Code size

本篇圖片來自這個網頁,所有權屬於 Sun and Tim Bary,有問題請告知。

10/25/2006

Rails 的原始碼行數比?

實例

看完 JavaEye 今天的文章,發現到根據 Robbin 估計,
网站来说,包含了forum,blog,SNS三种大型软件 的主要功能,每个部分单独去做,都要花好几个月,合起来的代码量(包括XML配置行数)保守估计至少要3-5万行。现在用ruby on rails编写,ruby代码量只有不到5000行。
根據 Robbin 這一篇截至现在JavaEye2.0 CVS上面代码行数,目前只有 3243行code。他保守估計 Java 跟 Rails 原始碼行數,大概是 6 : 1 ~ 10:1 的份量。

根據 poocs.net 在這篇文章的說法
The old codebase roughly consisted of around 50.000 lines of PHP code (plus a closed-source CMS that’s not included in this calculation). We’ve rewritten most of it (some features were left out on purpose) in about 5.000 lines of Rails code.
他用 Rails 改寫 PHP 現有的 Project ,做出來的原始碼行數 PHP : Rails 是 10 : 1。(註1)

根據 Beyond JAVA 裡面,Justin Gehtland 用 Rails 重寫一個用 JAVA Spring/Hibernate 寫好的 Project,他發現程式碼比例 JAVA :Rails 大概是 3.48 : 1 。附帶一提,他重寫的開發時間開發時間比是 16 : 1,更噁心的數字。(註2)

我曾經將以前寫過的一個小小 PHP Project 重寫,之前使用的 Framework 是我自己寫的 MVC 架構的PHP 程式。扣掉 HTML code ,程式碼行數大概PHP :Rails 是 8 : 1 左右吧。

GMANE 裡面有一位 Rick Bradley 提出一個healthcare網站的實例,他們將 JAVA/Hibernate/JSP/pojos/beans/Struts/Ajax 的專案轉換到 Ruby on Rails 時,他發現所有程式碼比例: JAVA 比 Rails 高達 20853 : 823,這是 25 : 1 的可怕程式碼數量比例。(註3)

結論

開發時間很難去作 Benchmark ,不過程式碼行數就很赤裸裸了。我們可以發現到,Rails 在程式碼行數上面的優勢還是相當相當明顯的。程式碼的行數代表的意思不只是開發速度的快慢有絕對的正相關,維護程式的速度也會加快,重構等等議題也會簡單許多,這是一個 Ruby on Rails 巨大的優勢。


註解
  1. 根據原文,PHP行數裡面,沒有計算一個 close source 的 CMS ,用 Ruby on Rails 實做時,也沒有implement 一些他們後來認為不重要的功能。我將兩者造成的程式碼數量增減都視為抵銷
  2. 原始碼的比例是 3293:1164,設定檔的行數是 1161:113
  3. GMANE提出 25 : 1 的驚人實例的詳細數據列表
Java version:

10361 lines of Java code
1143 lines of JSP
8082 lines of XML
1267 lines of build configuration
-----------------------------------------------------------
20853 TOTAL lines of stuff

Rails version:

494 lines of code (386 "LOC" per rake stats)
254 lines of RHTML
75 lines of configuration (includes comments in routes.rb)
0 lines of build configuration
-----------------------------------------------------------
823 TOTAL lines of stuff

9/08/2006

Rails 優缺點比較

又是一篇比較文

Ruby on Rails, and Ruby as a Language

優點

  1. MVC framework幫助 Ruby 使用者區分 presentation 還有 business logic
  2. Unit tests 直接 Build in
  3. DRY(Don’t repeat yourself) 原則讓開發較為容易
  4. 快速敏捷,而且不需要compile time的開發
  5. Convention over configuration 還有 meta-programming 讓 XML Configuration 絕跡,而這些都是 JAVA Platform 像是 Stuts 所必須的工作
  6. Active Record orm( Object Relational Mapper )不需要configuaration(如果不算 connection 的 config)
  7. 完整的Ajax Support( Prototype and Scriptaculous )
  8. Built in XML Web Services
  9. Ruby 實在是太簡單了,每個東西都是 Object ,而且code簡單好讀read.
  10. Easy model validations
  11. 當你 database schema 確定了,開發程式會速度非常快
  12. Session 資料會以存在硬碟或是資料庫的方式處理,所以 Session 資料將很容易被很多台 Server 存取
  13. Ruby是dynamically typed 語言,這讓開發變得很容易。
  14. Ruby 是 Open Class ,所以你可以在 Run Time 很容易加入任何新的Methods。這讓Active Record 可以動態的在Run Time產生每個 Database Entry。也因此,Ruby on Rails不需要更新 Database mapping Schema。
  15. Ruby 支援 Closures ,這讓 Ruby 可以使用較少的code就達成許多 JAVA 很辛苦才能達成的事情。
  16. Ruby on Rails將開發者良好的觀念帶入框架之中,他保護開發者免於一些很平常的錯誤。而這個架構也對於新的架構提供最好的實做方式。
  17. Mongrel Web Server 給予 Rails 開發者專屬於自己的Container,並且速度很快。
  18. Ruby 其實在 1993年就開始開發了。所以他不僅限於Web 開發,他有很多方面的應用。
缺點

  1. 對於已經存在的 Database Schema 要支援是比較弱的。雖然你總是可以在 Model 加入一些 mapping 來改進,但是你在同時也會失去一點點 ActiveRecord的便利性。最重要的是,他們沒有想過要改進現存database schema 的支援度。
  2. API 文件相當嚴重的缺乏。
  3. 如果你無法依照他給你的指示方式去做,那你就必須自己手動去寫。
  4. 如果當你不依照 Rails 的指定的方式去做,Scaffold 的 generator 產生的東西就沒太多意義。
  5. 要讓程式擁有很高的 Scale 是很花記憶體的,雖然現在 memory 很便宜。
  6. 沒有 build in 國際化的支援。
  7. SQL Server 的支援較弱(PHP也是如此的情況)
  8. Rails 開發時間較為年輕(跟PHP或是JAVA相比),所以他的開發者較少,不過最近成長數量相當驚人
  9. IDE數量不足,Textmate是最好的,但是他是 MAC only 的。