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
790
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
今日で分かる!カスタムコップの作り方
krpk1900
2
320
Kaigi on Rails 2024 - Rails APIモードのためのシンプルで効果的なCSRF対策 / kaigionrails-2024-csrf
corocn
1
240
CSC305 Lecture 13
javiergs
PRO
0
110
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
290
2024-10-02 dev2next - Application Observability like you've never heard before
jonatan_ivanov
0
200
Scan with Decoupled Look-back and Onesweep Radix Sort
shocker_0x15
0
160
rtcamp 10 (vk-illuminati)
yumcyawiz
1
210
Vue.js学習の振り返り
hiro_xre
1
100
2024-10-01 dev2next - Observability for Modern JVM Applications
jonatan_ivanov
1
140
Competitionsだけじゃない! Kaggle Notebooks Grandmasterのすすめ
corochann
2
940
LangChain & LangServeでAPIを爆速作成
kijitorabuti
2
370
Kubernetes for Data Engineers: Building Scalable, Reliable Data Pipelines
sucitw
1
110
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
The Cult of Friendly URLs
andyhume
77
6k
It's Worth the Effort
3n
183
27k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
31
1.7k
Building Better People: How to give real-time feedback that sticks.
wjessup
363
19k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3.5k
Faster Mobile Websites
deanohume
304
30k
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で お待ちしています💙