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
710
Fat Controller は悪か? ~光のFat Controller・闇のガリController~
stwile
June 22, 2024
Tweet
Share
More Decks by stwile
See All by stwile
フレームワークが生み出す負債や複雑さに対して、PHPUnitと付き合っていく
stwile
1
1.8k
継続的にLaravelのUnitTestを書く上で 気をつけていること
stwile
1
140
TDD視点から見る、Laravel・Requestクラスの依存性
stwile
0
93
Other Decks in Programming
See All in Programming
pytest プラグインを開発して DRY に自動テストを書こう
inuatsu
2
150
Micro Frontends Unmasked: Opportunities, Challenges, Alternatives
manfredsteyer
PRO
0
220
WEBアプリケーションにおけるAWS Lambdaを用いた大規模な非同期処理の実践
delhi09
PRO
0
470
いまあるチームにフィットさせる Serverless そして Platform Engineeringへの挑戦 / Serverless Fits the Team You Have and Platform Engineering
seike460
PRO
2
1.1k
Frontend Magic mit CSS Houdini
joergneumann
0
420
Iteratorでページネーションを実現する
sonatard
2
660
From Idea to IDE: Developing Plugins for Android Studio
thisaay
1
600
Интеграционное тестирование: как приручить хаос
mariyasaygina
0
260
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
2
180
上手に付き合うコンポーネントテスト
quramy
3
1.1k
Pythonで改めて考える「クラス(class)」の使いどころ
os1ma
13
3.2k
#TROCCOUG dbt Cloud+TROCCO
troccoug
0
140
Featured
See All Featured
Adopting Sorbet at Scale
ufuk
73
8.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
32k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
36
2.1k
Ruby is Unlike a Banana
tanoku
96
11k
Practical Orchestrator
shlominoach
185
10k
Building Applications with DynamoDB
mza
90
6k
Making Projects Easy
brettharned
114
5.8k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
278
13k
The Language of Interfaces
destraynor
154
24k
Imperfection Machines: The Place of Print at Facebook
scottboms
263
13k
The Art of Programming - Codeland 2020
erikaheidi
48
13k
Testing 201, or: Great Expectations
jmmastey
37
7k
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で お待ちしています💙