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
LLMと音声基盤モデルを用いた音声認識
spiralai
PRO
0
120
How to Break into Reading Open Source
kaspth
2
230
What you can do with Ruby on WebAssembly
kateinoigakukun
0
210
Google Sign-inの移行から始めるCredential Manager活用
clockvoid
0
560
[DroidKaigi 2024] Android ViewからJetpack Composeへ 〜Jetpack Compose移行のすゝめ〜 / From Android View to Jetpack Compose: A Guide to Migration
syarihu
1
1.4k
なぜアジャイルがうまくいかないのか?
yum3
2
140
Compose Multiplatform과 Ktor로 플랫폼의 경계를 넘어보자
kwakeuijin
0
150
Modern Angular with the NGRX Signal Store New Rules for Your Architecture @BASTA! 2024 in Mainz
manfredsteyer
PRO
0
280
M5Stackボードの選び方
tanakamasayuki
0
190
WEBアプリケーションにおけるAWS Lambdaを用いた大規模な非同期処理の実践
delhi09
PRO
0
320
あなたのアプリ、ログはでてますか?あるいはログをだしてますか? (Funabashi.dev用 軽量版)
uzulla
2
140
GraphQLにおけるページネーションベストプラクティス
estie
0
160
Featured
See All Featured
Statistics for Hackers
jakevdp
794
220k
Creatively Recalculating Your Daily Design Routine
revolveconf
215
12k
BBQ
matthewcrist
83
9.2k
Intergalactic Javascript Robots from Outer Space
tanoku
268
27k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
502
140k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.4k
What the flash - Photography Introduction
edds
67
11k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
28
1.7k
Fantastic passwords and where to find them - at NoRuKo
philnash
48
2.8k
Happy Clients
brianwarren
96
6.6k
Become a Pro
speakerdeck
PRO
22
4.9k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
30
2.6k
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で お待ちしています💙