淺析Ruby的源代碼布局及其編程風(fēng)格
使用 UTF-8 作為源文件編碼。
每個(gè)縮進(jìn)級(jí)別使用兩個(gè) 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 你可能會(huì)想要添加下面的配置設(shè)置來(lái)保護(hù)你的項(xiàng)目(避免)Windows 蔓延過(guò)來(lái)的換行符:
$ git config --global core.autocrlf true
不用使用 ; 來(lái)分割語(yǔ)句和表達(dá)式。以此推論 - 一行使用一個(gè)表達(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
對(duì)于沒(méi)有內(nèi)容的類定義,盡可能使用單行類定義形式.
# bad class FooError < StandardError end # okish class FooError < StandardError; end # good FooError = Class.new(StandardError)
避免單行方法。即便還是會(huì)受到一些人的歡迎,這里還是會(huì)有一些古怪的語(yǔ)法用起來(lái)很容易犯錯(cuò).
無(wú)論如何 - 應(yīng)該一行不超過(guò)一個(gè)單行方法.
# 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
空方法是這個(gè)規(guī)則的例外。
# good def no_op; end
操作符旁的空格,在逗號(hào),冒號(hào)和分號(hào)后;在 { 旁和在 } 之前,大多數(shù)空格可能對(duì) Ruby 解釋(代碼)無(wú)關(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ù)操作時(shí):
# bad e = M * c ** 2 # good e = M * c**2
{ 和 } 值得額外的澄清,自從它們被用于 塊 和 hash 字面量,以及以表達(dá)式的形式嵌入字符串。
對(duì)于 hash 字面量?jī)煞N風(fēng)格是可以接受的。
# good - space after { and before } { one: 1, two: 2 } # good - no space after { and before } {one: 1, two: 2}
第一種稍微更具可讀性(并且爭(zhēng)議的是一般在 Ruby 社區(qū)里面更受歡迎)。
第二種可以增加了 塊 和 hash 可視化的差異。
無(wú)論你選哪一種都行 - 但是最好保持一致。
目前對(duì)于嵌入表達(dá)式,也有兩個(gè)選擇:
# good - no spaces "string#{expr}" # ok - arguably more readable "string#{ expr }"
第一種風(fēng)格極為流行并且通常建議你與之靠攏。第二種,在另一方面,(有爭(zhēng)議)更具可讀性。
如同 hash - 選取一個(gè)風(fēng)格并且保持一致。
沒(méi)有空格 (, [之后或者 ], )之前。
some(arg).other [1, 2, 3].length ! 之后沒(méi)有空格 . # bad ! something # good !something
when和case 縮進(jìn)深度一致。我知道很多人會(huì)不同意這點(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)賦值一個(gè)條件表達(dá)式的結(jié)果給一個(gè)變量時(shí),保持分支的縮排在同一層。
# 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
在方法定義之間使用空行并且一個(gè)方法根據(jù)邏輯段來(lái)隔開(kāi)。
def some_method data = initialize(options) data.manipulate! data.result end def some_methods result end
避免在一個(gè)方法調(diào)用的最后一個(gè)參數(shù)有逗號(hào),特別是當(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)值時(shí),在 = 兩邊使用空格:
# 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 書(shū)建議用第一個(gè)風(fēng)格,不過(guò)第二個(gè)風(fēng)格在實(shí)踐中更為常見(jiàn)(并可爭(zhēng)議地可讀性更高一點(diǎn))。
避免在不需要的時(shí)候使用行繼續(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)為很好
- . 開(kāi)頭(選項(xiàng) A) 和 尾隨 . (選項(xiàng) B) 。
(選項(xiàng) A) 當(dāng)一個(gè)鏈?zhǔn)椒椒ㄕ{(diào)用需要在另一行繼續(xù)時(shí),將 . 放在第二行。
# 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ù)一個(gè)鏈?zhǔn)椒椒ㄕ{(diào)用,將 . 放在第一行來(lái)識(shí)別要繼續(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)這兩個(gè)另類風(fēng)格的優(yōu)點(diǎn)的討論。
如果一個(gè)方法調(diào)用的跨度超過(guò)了一行,對(duì)齊它們的參數(shù)。當(dāng)參數(shù)對(duì)齊因?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
對(duì)齊多行跨度的 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ù)值添加下劃線來(lái)提高它們的可讀性。
# bad - how many 0s are there? num = 1000000 # good - much easier to parse for the human brain num = 1_000_000
使用 RDoc 以及它的慣例來(lái)撰寫(xiě) API 文檔。注解區(qū)塊及 def 不要用空行隔開(kāi)。
每一行限制在 80 個(gè)字符內(nèi)。
避免行尾空格。
不要使用區(qū)塊注釋。它們不能由空白引導(dǎo)(=begin 必須頂頭開(kāi)始),并且不如普通注釋容易辨認(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)郵件主動(dòng)推送觸發(fā)程序
這篇文章主要介紹了Ruby實(shí)現(xiàn)郵件主動(dòng)推送觸發(fā)程序,本文給出了客戶端輪詢和服務(wù)器主動(dòng)推送的代碼實(shí)例,需要的朋友可以參考下2015-01-01Ruby on Rails網(wǎng)站項(xiàng)目構(gòu)建簡(jiǎn)單指南
Rails項(xiàng)目通過(guò)Ruby世界中的gem和rake工具來(lái)構(gòu)建起來(lái)真的相當(dāng)方便,這里就給大家整理了一份Ruby on Rails網(wǎng)站項(xiàng)目構(gòu)建簡(jiǎn)單指南,需要的朋友可以參考下2016-06-06ruby判斷一個(gè)數(shù)是否為質(zhì)數(shù)(素?cái)?shù))示例
這篇文章主要介紹了ruby判斷一個(gè)數(shù)是否為質(zhì)數(shù)(素?cái)?shù))示例,需要的朋友可以參考下2014-05-05Rails bundle命令安裝mysql gem包出錯(cuò)的解決方法
這篇文章主要介紹了Rails bundle命令安裝mysql gem包出錯(cuò)的解決方法,本文原因是沒(méi)有安裝mysql開(kāi)發(fā)包,需要的朋友可以參考下2014-08-08比較不錯(cuò)的關(guān)于ruby的電子書(shū)下載地址集合
比較不錯(cuò)的關(guān)于ruby的電子書(shū)下載地址集合...2007-10-10Ruby中嵌套對(duì)象轉(zhuǎn)換成json的方法
這篇文章主要介紹了Ruby中嵌套對(duì)象轉(zhuǎn)換成json的方法,同時(shí)介紹了普通對(duì)象to_json的方法,需要的朋友可以參考下2014-06-06