Slide 1

Slide 1 text

⚡ Ruby、オブジェクト指向、設計 Omotesando.rb #56 2020/11/05 1

Slide 2

Slide 2 text

自己紹介 • 名前: 神速 • 会社: メドピア株式会社 • 所属: CTO室SRE • GitHub: @sinsoku (画像右上) • Twitter: @sinsoku_listy (画像右下) 2

Slide 3

Slide 3 text

⚠ ちゃんと学びたい人は ! をたくさん読んでください。 3

Slide 4

Slide 4 text

オブジェクト指向について • プログラミングの設計手法 • データと処理をまとめる • Rubyのすべてはオブジェクト • 純粋なオブジェクト指向言語 4

Slide 5

Slide 5 text

オブジェクト指向は設計手法 プログラミング言語には依存しない 5

Slide 6

Slide 6 text

C言語でも(頑張れば)OOPできる1 #include struct user_data { char name[20]; }; char *name(struct user_data *this) { return this -> name; } char first(struct user_data *this) { return this -> name[0]; } struct user_class { struct user_data obj; char *(*name)(struct user_data *this); char (*first)(struct user_data *this); }; int main() { struct user_data this = { "foo" }; struct user_class u = { this, name, first }; printf("%s\n", u.name(&this)); printf("%c\n", u.first(&this)); } 1 普通はやらないと思う 6

Slide 7

Slide 7 text

Rubyだと簡単 class User attr_reader :name def initialize(name) @name = name end def first name[0] end end u = User.new("foo") puts u.name puts u.first 7

Slide 8

Slide 8 text

オブジェクト指向な設計とは? 8

Slide 9

Slide 9 text

9

Slide 10

Slide 10 text

オブジェクト指向 • 設計に正解は存在しない • UserLogin, User#login のどちらでも良い • 他メンバーが読みやすいかどうか • 名前から処理を予想できるか? • ビジネスの用語とズレがないか? 10

Slide 11

Slide 11 text

オブジェクト指向のSOLID • 単一責任の原則 • 解放閉鎖の原則 • リスコフの置換原則 • インターフェース分離の原則 • 依存性逆転の原則 11

Slide 12

Slide 12 text

12

Slide 13

Slide 13 text

普段そこまで考えてない 13

Slide 14

Slide 14 text

コードは短く、読みやすく書く2 • 短いコードは正義 • クラスやモジュールを作り過ぎない • 最初は単純なメソッドで済ませる • メタプロで短くするのは控える • パフォーマンスは後で考える " 2 去年の銀座Railsのスライドから引用 14

Slide 15

Slide 15 text

考えていること 15

Slide 16

Slide 16 text

get_xxx を避ける Rubyだとgetterは名詞を使うことが多い。 class User # bad # def get_items; end # good def my_items; end end 16

Slide 17

Slide 17 text

! 英語にして違和感がない3 クラスを名詞、メソッドを動詞にして違和感ないか? class User # user login def login; end # user search by word def search_by(word); end end メソッドが長い場合、クラスを抽出できる可能性がある。 3 Railsのメソッド名を参考にする事が多い。 17

Slide 18

Slide 18 text

インスタンス変数を使うようにする ! な例 class Shop def total_price(item, coupon: nil) if coupon item.price - coupon.discount else item.price end end end 18

Slide 19

Slide 19 text

インスタンス変数を使うようにする ! な例。インスタンス変数の分だけ短くなる。 class Item def total_price(coupon: nil) if coupon price - coupon.discount else price end end end 19

Slide 20

Slide 20 text

脳内メモリを使わない foo -> bar -> buz -> piyo の ような呼び出しだと変数の値 を覚えるのが大変。 class A def foo bar end def bar buz end def buz piyo end end 20

Slide 21

Slide 21 text

脳内メモリを使わない 一度に覚えておくことを減ら したい。 class A def foo x = bar y = buz z = piyo end end 21

Slide 22

Slide 22 text

イミュータブル ! 引数を破壊したり、ループを使っている。 def cool_items(items, service_charge) items.each do |item| next if item.xxx? item.price += service_charge end items end 22

Slide 23

Slide 23 text

イミュータブル ! 関数型プログラミング的 def cool_items(items, service_charge) items.select { |item| item.xxx? } .map { |item| Item.new(price: item.price + service_charge) end end 23

Slide 24

Slide 24 text

まとめ? • 設計の用語はコミュニケーション用 • みんな普段そこまで考えているの・・・? • 短いコードを書こうとすると良い感じになる • 気がする 24