淺析Ruby的源代碼布局及其編程風(fēng)格
使用 UTF-8 作為源文件編碼。
每個縮進(jìn)級別使用兩個 spaces (又名軟 tabs). 不要硬 tabs
# bad - four spaces def some_method do_something end # good def some_method do_something end
使用 Unix-風(fēng)格 換行符。(*BSD/Solaris/Linux/OSX 用戶被為默認(rèn)涵蓋,Windows 用戶必須特別小心.)
- \n是換行,英文是LineFeed,ASCII碼是0xA。
- \r是回車,英文是Carriage Return ,ASCII碼是0xD。
- windows下enter是 \n\r,unix下是\n,mac下是\r
如果你正在使用 Git 你可能會想要添加下面的配置設(shè)置來保護(hù)你的項(xiàng)目(避免)Windows 蔓延過來的換行符:
$ git config --global core.autocrlf true
不用使用 ; 來分割語句和表達(dá)式。以此推論 - 一行使用一個表達(dá)式
# bad puts 'foobar'; # superfluous semicolon puts 'foo'; puts 'bar' # two expression on the same line # good puts 'foobar' puts 'foo' puts 'bar' puts 'foo', 'bar' # this applies to puts in particular
對于沒有內(nèi)容的類定義,盡可能使用單行類定義形式.
# bad class FooError < StandardError end # okish class FooError < StandardError; end # good FooError = Class.new(StandardError)
避免單行方法。即便還是會受到一些人的歡迎,這里還是會有一些古怪的語法用起來很容易犯錯.
無論如何 - 應(yīng)該一行不超過一個單行方法.
# bad def too_much; something; something_else; end # okish - notice that the first ; is required def no_braces_method; body end # okish - notice that the second ; is optional def no_braces_method; body; end # okish - valid syntax, but no ; make it kind of hard to read def some_method() body end # good def some_method body end
空方法是這個規(guī)則的例外。
# good def no_op; end
操作符旁的空格,在逗號,冒號和分號后;在 { 旁和在 } 之前,大多數(shù)空格可能對 Ruby 解釋(代碼)無關(guān),但是它的恰當(dāng)使用是讓代碼變得易讀的關(guān)鍵。
sum = 1 + 2 a, b = 1, 2 1 > 2 ? true : false; puts 'Hi' [1, 2, 3].each { |e| puts e }
唯一的例外是當(dāng)使用指數(shù)操作時:
# bad e = M * c ** 2 # good e = M * c**2
{ 和 } 值得額外的澄清,自從它們被用于 塊 和 hash 字面量,以及以表達(dá)式的形式嵌入字符串。
對于 hash 字面量兩種風(fēng)格是可以接受的。
# good - space after { and before } { one: 1, two: 2 } # good - no space after { and before } {one: 1, two: 2}
第一種稍微更具可讀性(并且爭議的是一般在 Ruby 社區(qū)里面更受歡迎)。
第二種可以增加了 塊 和 hash 可視化的差異。
無論你選哪一種都行 - 但是最好保持一致。
目前對于嵌入表達(dá)式,也有兩個選擇:
# good - no spaces "string#{expr}" # ok - arguably more readable "string#{ expr }"
第一種風(fēng)格極為流行并且通常建議你與之靠攏。第二種,在另一方面,(有爭議)更具可讀性。
如同 hash - 選取一個風(fēng)格并且保持一致。
沒有空格 (, [之后或者 ], )之前。
some(arg).other [1, 2, 3].length ! 之后沒有空格 . # bad ! something # good !something
when和case 縮進(jìn)深度一致。我知道很多人會不同意這點(diǎn),但是它是"The Ruby Programming Language" 和 "Programming Ruby"中公認(rèn)的風(fēng)格。
# bad case when song.name == 'Misty' puts 'Not again!' when song.duration > 120 puts 'Too long!' when Time.now.hour > 21 puts "It's too late" else song.play end # good case when song.name == 'Misty' puts 'Not again!' when song.duration > 120 puts 'Too long!' when Time.now.hour > 21 puts "It's too late" else song.play end case when song.name == 'Misty' puts 'Not again!' when song.duraton > 120 puts 'Too long!' when Time.now > 21 puts "It's too late" else song.play end
當(dāng)賦值一個條件表達(dá)式的結(jié)果給一個變量時,保持分支的縮排在同一層。
# bad - pretty convoluted kind = case year when 1850..1889 then 'Blues' when 1890..1909 then 'Ragtime' when 1910..1929 then 'New Orleans Jazz' when 1930..1939 then 'Swing' when 1940..1950 then 'Bebop' else 'Jazz' end result = if some_cond calc_something else calc_something_else end # good - it's apparent what's going on kind = case year when 1850..1889 then 'Blues' when 1890..1909 then 'Ragtime' when 1910..1929 then 'New Orleans Jazz' when 1930..1939 then 'Swing' when 1940..1950 then 'Bebop' else 'Jazz' end result = if some_cond calc_something else calc_something_else end # good (and a bit more width efficient) kind = case year when 1850..1889 then 'Blues' when 1890..1909 then 'Ragtime' when 1910..1929 then 'New Orleans Jazz' when 1930..1939 then 'Swing' when 1940..1950 then 'Bebop' else 'Jazz' end result = if some_cond calc_something else calc_something_else end
在方法定義之間使用空行并且一個方法根據(jù)邏輯段來隔開。
def some_method data = initialize(options) data.manipulate! data.result end def some_methods result end
避免在一個方法調(diào)用的最后一個參數(shù)有逗號,特別是當(dāng)參數(shù)不在另外一行。
# bad - easier to move/add/remove parameters, but still not preferred some_method( size, count, color, ) # bad some_method(size, count, color, ) # good some_method(size, count, color)
當(dāng)給方法的參數(shù)賦默認(rèn)值時,在 = 兩邊使用空格:
# bad def some_method(arg1=:default, arg2=nil, arg3=[]) # do something... end # good def some_method(arg1 = :default, arg2 = nil, arg3 = []) # do something... end
雖然幾本 Ruby 書建議用第一個風(fēng)格,不過第二個風(fēng)格在實(shí)踐中更為常見(并可爭議地可讀性更高一點(diǎn))。
避免在不需要的時候使用行繼續(xù)符 \ 。實(shí)踐中,
除非用于連接字符串, 否則避免在任何情況下使用行繼續(xù)符。
# bad result = 1 - \ 2 # good (but still ugly as hell) result = 1 \ - 2 long_string = 'First part of the long string' \ ' and second part of the long string'
采用連貫的多行方法鏈?zhǔn)斤L(fēng)格。在 Ruby 社區(qū)有兩種受歡迎的風(fēng)格,它們都被認(rèn)為很好
- . 開頭(選項(xiàng) A) 和 尾隨 . (選項(xiàng) B) 。
(選項(xiàng) A) 當(dāng)一個鏈?zhǔn)椒椒ㄕ{(diào)用需要在另一行繼續(xù)時,將 . 放在第二行。
# bad - need to consult first line to understand second line one.two.three. four # good - it's immediately clear what's going on the second line one.two.three .four
(選項(xiàng) B) 當(dāng)在另一行繼續(xù)一個鏈?zhǔn)椒椒ㄕ{(diào)用,將 . 放在第一行來識別要繼續(xù)的表達(dá)式。
# bad - need to read ahead to the second line to know that the chain continues one.two.three .four # good - it's immediately clear that the expression continues beyond the first line one.two.three. four
在這里可以發(fā)現(xiàn)有關(guān)這兩個另類風(fēng)格的優(yōu)點(diǎn)的討論。
如果一個方法調(diào)用的跨度超過了一行,對齊它們的參數(shù)。當(dāng)參數(shù)對齊因?yàn)樾袑捪拗贫缓线m,
在第一行之后單縮進(jìn)也是可以接受的。
# starting point (line is too long) def send_mail(source) Mailer.deliver(to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text) end # bad (double indent) def send_mail(source) Mailer.deliver( to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text) end # good def send_mail(source) Mailer.deliver(to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text) end # good (normal indent) def send_mail(source) Mailer.deliver( to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text ) end
對齊多行跨度的 array literals 的元素。
# bad - single indent menu_item = ['Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Baked beans', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam'] # good menu_item = [ 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Baked beans', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam' ] # good menu_item = ['Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Baked beans', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam']
大數(shù)值添加下劃線來提高它們的可讀性。
# bad - how many 0s are there? num = 1000000 # good - much easier to parse for the human brain num = 1_000_000
使用 RDoc 以及它的慣例來撰寫 API 文檔。注解區(qū)塊及 def 不要用空行隔開。
每一行限制在 80 個字符內(nèi)。
避免行尾空格。
不要使用區(qū)塊注釋。它們不能由空白引導(dǎo)(=begin 必須頂頭開始),并且不如普通注釋容易辨認(rèn)。
# bad == begin comment line another comment line == end # good # comment line # another comment line
在 API 文檔中使用 RDoc和它的公約。不要在注釋代碼塊和def之間加入空行。
保持每一行少于80字符。
避免尾隨空格。
相關(guān)文章
Ruby實(shí)現(xiàn)郵件主動推送觸發(fā)程序
這篇文章主要介紹了Ruby實(shí)現(xiàn)郵件主動推送觸發(fā)程序,本文給出了客戶端輪詢和服務(wù)器主動推送的代碼實(shí)例,需要的朋友可以參考下2015-01-01Ruby on Rails網(wǎng)站項(xiàng)目構(gòu)建簡單指南
Rails項(xiàng)目通過Ruby世界中的gem和rake工具來構(gòu)建起來真的相當(dāng)方便,這里就給大家整理了一份Ruby on Rails網(wǎng)站項(xiàng)目構(gòu)建簡單指南,需要的朋友可以參考下2016-06-06ruby判斷一個數(shù)是否為質(zhì)數(shù)(素?cái)?shù))示例
這篇文章主要介紹了ruby判斷一個數(shù)是否為質(zhì)數(shù)(素?cái)?shù))示例,需要的朋友可以參考下2014-05-05Rails bundle命令安裝mysql gem包出錯的解決方法
這篇文章主要介紹了Rails bundle命令安裝mysql gem包出錯的解決方法,本文原因是沒有安裝mysql開發(fā)包,需要的朋友可以參考下2014-08-08