Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Fat Controller は悪か? ~光のFat Controller・闇のガリCont...
Search
stwile
June 22, 2024
Programming
1
780
Fat Controller は悪か? ~光のFat Controller・闇のガリController~
stwile
June 22, 2024
Tweet
Share
More Decks by stwile
See All by stwile
フレームワークが生み出す負債や複雑さに対して、PHPUnitと付き合っていく
stwile
1
1.9k
継続的にLaravelのUnitTestを書く上で 気をつけていること
stwile
1
150
TDD視点から見る、Laravel・Requestクラスの依存性
stwile
0
93
Other Decks in Programming
See All in Programming
2024-10-02 dev2next - Application Observability like you've never heard before
jonatan_ivanov
0
200
現場から考えるソフトウェアエンジニアリングの価値と実験
nomuson
1
140
CSC509 Lecture 07
javiergs
PRO
0
140
個人開発で使ってるやつを紹介する回
yohfee
1
730
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
1.2k
20241004 モノタロウ式~ドメインモデリングとリアーキテクチャ
monotaro
PRO
2
680
Golang と Erlang
taiyow
7
1.8k
Competitionsだけじゃない! Kaggle Notebooks Grandmasterのすすめ
corochann
2
940
CSC509 Lecture 06
javiergs
PRO
0
130
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
0
230
急速に利用拡大を続ける飲食店向けサービスで 店内端末同士のローカル通信を追加設定なしで実現した話
whatasoda
0
140
ACES Meet におけるリリース作業改善の取り組み
fukucheee
0
150
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
40
2.1k
Build The Right Thing And Hit Your Dates
maggiecrowley
32
2.3k
Practical Orchestrator
shlominoach
186
10k
The Language of Interfaces
destraynor
154
24k
[RailsConf 2023] Rails as a piece of cake
palkan
49
4.8k
Testing 201, or: Great Expectations
jmmastey
38
7k
Faster Mobile Websites
deanohume
304
30k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
23k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
43
6.6k
Designing the Hi-DPI Web
ddemaree
280
34k
Fireside Chat
paigeccino
32
3k
Learning to Love Humans: Emotional Interface Design
aarron
272
40k
Transcript
2024 年 6 月 22 日 (土) PHPカンファレンス福岡 @stwile871 Fat
Controller は悪か? ~光のFat Controller・闇のガリController~
自己紹介 スタヰル(@stwile871) $ PHPer歴7年目 $ リアーキテクチャ・でかいリファクタリング $ 洗車 $ 好きな関数は
sprintf()
Q. 唐突な質問です
Q.FatControllerは悪者ですか? Q.FatControllerは悪者ですか?
Q.FatControllerは悪者ですか? • よく悪口を言われている • 特級呪物として恨み辛みを孕んでいる😇 Q.FatControllerは悪者ですか?
😈 本当にFatControllerは 悪いやつなのでしょうか?
👼 本当にThinコントローラは イイやつなのでしょうか?
禁断のテーマに一石を投じる
アジェンダ 1.Fatコントローラはどこから生まれるのか 2.Fatコントローラについて 3.Thinコントローラについて 4.考察
アジェンダ 1.Fatコントローラはどこから生まれるのか 2.Fatコントローラについて 3.Thinコントローラについて 4.考察
Fatコントローラはなぜ生まれるのか
Fatコントローラはなぜ生まれるのか • 動くものをつくる 😄
Fatコントローラはなぜ生まれるのか • 動くものをつくる(コントローラにロジックを書く) 😄
Fatコントローラはなぜ生まれるのか • 動くものをつくる(コントローラにロジックを書く) • 急いで動くものをつくる 😆
Fatコントローラはなぜ生まれるのか • 動くものをつくる(コントローラにロジックを書く) • 急いで動くものをつくる • 急いで動くものをつくる 😆
Fatコントローラはなぜ生まれるのか • 動くものをつくる(コントローラにロジックを書く) • 急いで動くものをつくる • 急いで動くものをつくる • 急いで動くものをつくる •
急いで動くものをつくる • 急いで動くものをつくる • 急いで動くものをつくる 😇
Fatコントローラはなぜ生まれるのか • 動くものをつくる(コントローラにロジックを書く) • 急いで動くものをつくる • 急いで動くものをつくる • 急いで動くものをつくる •
急いで動くものをつくる • 急いで動くものをつくる • 急いで動くものをつくる • 急いで動くものをつくる • 急いで動くものをつくる • 急いで動くものをつくる • 急いで動くものをつくる • 急いで動くものをつくる
Fatコントローラはなぜ生まれるのか • 動くものをつくる(コントローラにロジックを書く) • 急いで動くものをつくる • 急いで動くものをつくる • 急いで動くものをつくる •
急いで動くものをつくる • 急いで動くものをつくる • 急いで動くものをつくる 動くが 整理されていないもの
本来踏むべきアプローチ
本来踏むべきアプローチ • 動くものをつくる
本来踏むべきアプローチ • 動くものをつくる ◦ 整理されたものをつくる
本来踏むべきアプローチ • 動くものをつくる ◦ 整理されたものをつくる • 動くものを作る
本来踏むべきアプローチ • 動くものをつくる ◦ 整理されたものをつくる • 動くものを作る ◦ 整理されたものをつくる
本来踏むべきアプローチ • 動くものをつくる ◦ 整理されたものをつくる • 動くものを作る ◦ 整理されたものをつくる 作って、整理する必要がある
Q. なぜFatコントローラが生まれるのか?
Q. なぜFatコントローラが生まれるのか? A. 作ったもの(動くだけのもの)を 整理していないから
アジェンダ 1.Fatコントローラはどこから生まれるのか 2.Fatコントローラについて 3.Thinコントローラについて 4.考察
Fatコントローラについて
Fatコントローラについて APIのIO処理と具体的なビジネスロジックが 同一ファイルに書かれたクラス
Fatコントローラについて • Request・Responseの処理 APIのIO処理と具体的なビジネスロジックが 同一ファイルに書かれたクラス
Fatコントローラについて • Request・Responseの処理 • DBアクセス処理 APIのIO処理と具体的なビジネスロジックが 同一ファイルに書かれたクラス
Fatコントローラについて • Request・Responseの処理 • DBアクセス処理 • 認証認可処理...etc APIのIO処理と具体的なビジネスロジックが 同一ファイルに書かれたクラス
Fatコントローラたらしめるもの
Fatコントローラたらしめるもの 1.コードの長さ
Fatコントローラたらしめるもの 1.コードの長さ 2.不適切な再利用
Fatコントローラたらしめるもの 1.コードの長さ 2.不適切な再利用 多くのエンジニアを苦しめている💩
アジェンダ 1.Fatコントローラはどこから生まれるのか 2.Fatコントローラについて 3.Thinコントローラについて 4.考察
Thinコントローラについて APIのIO処理と具体的なビジネスロジックが 分散している状態のクラス • Request・Responseの処理 • DBアクセス処理 • 認証認可処理...etc
Thinコントローラについて APIのIO処理と具体的なビジネスロジックが 分散している状態のクラス • Fatコントローラをリファクタリングして目指したい姿のひとつ
Thinコントローラについて APIのIO処理と具体的なビジネスロジックが 分散している状態のクラス • Fatコントローラをリファクタリングして目指したい姿のひとつ • 適度に凝集度が保たれている、適切に関心の分離がなされている
Thinコントローラについて APIのIO処理と具体的なビジネスロジックが 分散している状態のクラス • Fatコントローラをリファクタリングして目指したい姿のひとつ • 適度に凝集度が保たれている、適切に関心の分離がなされている ?👤?? 「理想ではあるが、この光は少し眩しすぎやしないか?」
APIのIO処理と具体的なビジネスロジックが 分散している状態のクラス Thinコントローラの闇😈
APIのIO処理と具体的なビジネスロジックが 分散している状態のクラス • DBアクセスのクラスのロジックから DBアクセスのロジックを呼ぶ Thinコントローラの闇😈
APIのIO処理と具体的なビジネスロジックが 分散している状態のクラス • DBアクセスのクラスのロジックから DBアクセスのロジックを呼ぶ Thinコントローラの闇😈
APIのIO処理と具体的なビジネスロジックが 分散している状態のクラス • DBアクセスのクラスのロジックから DBアクセスのロジックを呼ぶ • 循環参照 Thinコントローラの闇😈
APIのIO処理と具体的なビジネスロジックが 分散している状態のクラス • DBアクセスのクラスのロジックから DBアクセスのロジックを呼ぶ • 循環参照 Thinコントローラの闇😈
APIのIO処理と具体的なビジネスロジックが 分散している状態のクラス • DBアクセスのクラスのロジックから DBアクセスのロジックを呼ぶ • 循環参照 ?👤?? 「Fatコントローラのときのほうがマシだったのでは…?」 Thinコントローラの闇😈
アジェンダ 1.Fatコントローラはどこから生まれるのか 2.Fatコントローラについて 3.Thinコントローラについて 4.考察
Fatコントローラたらしめるもの 1.コードの長さ 2.不適切な再利用
Fatコントローラはコードが長い Q. コードの長さが悪か?
Fatコントローラはコードが長い Q. コードの長さが悪か? A1. 今長いだけ(状態)
Fatコントローラはコードが長い Q. コードの長さが悪か? A1. 今長いだけ(状態)で分割可能なのでは💡
Fatコントローラはコードが長い Q. コードの長さが悪か? A1. 今長いだけ(状態)で分割可能なのでは💡 • Single Action Controllerに切り出す
Fatコントローラはコードが長い Q. コードの長さが悪か? A1. 今長いだけ(状態)で分割可能なのでは💡 • Single Action Controllerに切り出す •
private メソッドに切り出す
Fatコントローラはコードが長い Q. コードの長さが悪か? A1. 今長いだけ(状態)で分割可能なのでは💡 • Single Action Controllerに切り出す •
private メソッドに切り出す • クラス設計
Fatコントローラはコードが長い Q. コードの長さが悪か?
Fatコントローラはコードが長い Q. コードの長さが悪か? A2. ???「凝集度によるのでは?」
Fatコントローラはコードが長い Q. コードの長さが悪か? A2. ???「凝集度によるのでは?」 • 業務ロジックによる
Fatコントローラはコードが長い Q. コードの長さが悪か? A2. ???「凝集度によるのでは?」 • 業務ロジックによる • 散らばっていないほうが良いこともある
つまり???
Fatコントローラが悪い
Fatコントローラが悪い コントローラが 整理されていない状態
Fatコントローラが悪い コントローラが 整理されていない状態 整理さえできれば救える👌
Fatコントローラの可能性 凝集度が高い
Fatコントローラの可能性 分割の選択肢を選ぶことができる 凝集度が高い
Fatコントローラの可能性 分割の選択肢を選ぶことができる • アーキテクチャ選定 凝集度が高い
Fatコントローラの可能性 分割の選択肢を選ぶことができる • アーキテクチャ選定 • 規模感の調整 凝集度が高い
Fatコントローラの可能性 凝集度が高い 分割の選択肢を選ぶことができる • アーキテクチャ選定 • 規模感の調整 • モデリングして、再度整理する時間
Fatコントローラの可能性 凝集度が高い 分割をしない選択もができる • アーキテクチャ選定 • 規模感の調整 • モデリングして、再度整理する時間
つまり??? (2回目)
• Fatコントローラは状態であって、救える まとめ
• Fatコントローラは状態であって、救える • Tninコントローラの中にも悪いやつがいる まとめ
• Fatコントローラは状態であって、救える • Tninコントローラの中にも悪いやつがいる • 野放しにしないで、早めに整理しよう まとめ
͓ΘΓ Ask the Speakerで お待ちしています💙