在 class 定義里使用一致的結構。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
class Person # extend and include go first extend SomeModule include AnotherModule # constants are next SOME_CONSTANT = 20 # afterwards we have attribute macros attr_reader :name # followed by other macros (if any) validates :name # public class methods are next in line def self .some_method end # followed by public instance methods def some_method end # protected and private methods are grouped near the end protected def some_protected_method end private def some_private_method end end |
傾向使用 module,而不是只有類方法的 class。類別應該只在創(chuàng)建實例是合理的時候使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# bad class SomeClass def self .some_method # body omitted end def self .some_other_method end end # good module SomeClass module_function def some_method # body omitted end def some_other_method end end |
當你希望將模塊的實例方法變成 class 方法時,偏愛使用 module_function 勝過 extend self。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# bad module Utilities extend self def parse_something(string) # do stuff here end def other_utility_method(number, string) # do some more stuff end end # good module Utilities module_function def parse_something(string) # do stuff here end def other_utility_method(number, string) # do some more stuff end end |
When designing class hierarchies make sure that they conform to the
Liskov Substitution Principle.
在設計類層次的時候確保他們符合 Liskov Substitution Principle 原則。(譯者注: LSP原則大概含義為: 如果一個函數(shù)中引用了 父類的實例, 則一定可以使用其子類的實例替代, 并且函數(shù)的基本功能不變. (雖然功能允許被擴展))
Liskov替換原則:子類型必須能夠替換它們的基類型 <br/>
1. 如果每一個類型為T1的對象o1,都有類型為T2的對象o2,使得以T1定義的所有程序P在所有的對象o1都代換為o2時,程序P的行為沒有變化,那么類型T2是類型T1的子類型。 <br/>
2. 換言之,一個軟件實體如果使用的是一個基類的話,那么一定適用于其子類,而且它根本不能察覺出基類對象和子類對象的區(qū)別。只有衍生類替換基類的同時軟件實體的功能沒有發(fā)生變化,基類才能真正被復用。 <br/>
3. 里氏代換原則由Barbar Liskov(芭芭拉.里氏)提出,是繼承復用的基石。 <br/>
4. 一個繼承是否符合里氏代換原則,可以判斷該繼承是否合理(是否隱藏有缺陷)。
努力使你的類盡可能的健壯 [SOLID](http://en.wikipedia.org/wiki/SOLID_object-oriented_design\))。(
總是為你自己的類提供 to_s 方法, 用來表現(xiàn)這個類(實例)對象包含的對象.
1
2
3
4
5
6
7
8
9
10
11
12
|
class Person attr_reader :first_name , :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end def to_s "#@first_name #@last_name" end end |
使用 attr 功能成員來定義各個實例變量的訪問器或者修改器方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# bad class Person def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end def first_name @first_name end def last_name @last_name end end # good class Person attr_reader :first_name , :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end end |
避免使用 attr。使用 attr_reader 和 attr_accessor 作為替代。
1
2
3
4
5
6
7
|
# bad - creates a single attribute accessor (deprecated in 1.9) attr :something , true attr :one , :two , :three # behaves as attr_reader # good attr_accessor :something attr_reader :one , :two , :three |
考慮使用 Struct.new, 它可以定義一些瑣碎的 accessors,
constructor(構造函數(shù)) 和 comparison(比較) 操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# good class Person attr_reader :first_name , :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end end # better class Person < Struct. new ( :first_name , :last_name ) end |
考慮使用 Struct.new,它替你定義了那些瑣碎的存取器(accessors),構造器(constructor)以及比較操作符(comparison operators)。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# good class Person attr_accessor :first_name , :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end end # better Person = Struct. new ( :first_name , :last_name ) do end |
不要去 extend 一個 Struct.new - 它已經(jīng)是一個新的 class。擴展它會產(chǎn)生一個多余的 class 層級
并且可能會產(chǎn)生怪異的錯誤如果文件被加載多次。
考慮添加工廠方法來提供靈活的方法來創(chuàng)建特定類實例。
1
2
3
4
5
|
class Person def self .create(potions_hash) # body omitted end end |
鴨子類型(duck-typing)優(yōu)于繼承。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# bad class Animal # abstract method def speak end end # extend superclass class Duck < Animal def speak puts 'Quack! Quack' end end # extend superclass class Dog < Animal def speak puts 'Bau! Bau!' end end # good class Duck def speak puts 'Quack! Quack' end end class Dog def speak puts 'Bau! Bau!' end end |
Avoid the usage of class (@@) variables due to their "nasty" behavior
in inheritance.
避免使用類變量(@@)因為他們討厭的繼承習慣(在子類中也可以修改父類的類變量)。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Parent @@class_var = 'parent' def self .print_class_var puts @@class_var end end class Child < Parent @@class_var = 'child' end Parent.print_class_var # => will print "child" |
正如上例看到的, 所有的子類共享類變量, 并且可以直接修改類變量,此時使用類實例變量是更好的主意.
根據(jù)方法的用途為他們分配合適的可見度( private, protected ),不要讓所有的方法都是 public (這是默認設定)。這是 Ruby 不是 Python。
public, protected, 和 private 等可見性關鍵字應該和其(指定)的方法具有相同的縮進。并且不同的可見性關鍵字之間留一個空格。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class SomeClass def public_method # ... end private def private_method # ... end def another_private_method # ... end end |
使用 def self.method 來定義單例方法. 當代碼重構時, 這將使得代碼更加容易因為類名是不重復的.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
class TestClass # bad def TestClass.some_method # body omitted end # good def self .some_other_method # body omitted end # Also possible and convenient when you # have to define many singleton methods. class << self def first_method # body omitted end def second_method_etc # body omitted end end end class SingletonTest def size 25 end end test1 = SingletonTest. new test2 = SingletonTest. new def test2.size 10 end test1.size # => 25 test2.size # => 10 |
本例中,test1 與 test2 屬於同一類別,但 test2 具有重新定義的 size 方法,因此兩者的行為會不一樣。只給予單一物件的方法稱為單例方法 (singleton method)。