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
770
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
140
TDD視点から見る、Laravel・Requestクラスの依存性
stwile
0
93
Other Decks in Programming
See All in Programming
メルカリ ハロ アプリの技術スタック
atsumo
2
880
20241004 モノタロウ式~ドメインモデリングとリアーキテクチャ
monotaro
PRO
2
650
Vaporモードを大規模サービスに最速導入して学びを共有する
kazukishimamoto
2
520
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
1.1k
データマイグレーションの成功戦略~サービスリニューアルで失敗しないための実践ガイド~
tkzwtks
8
840
Vue SFCのtemplateでTypeScriptの型を活用しよう
tsukkee
3
230
sqlcを利用してsqlに型付けを
kamiyam
0
250
tsconfig.jsonの最近の新機能 ファイルパス編
uhyo
7
1.9k
組織に自動テストを書く文化を根付かせる戦略(2024秋版) / Building Automated Test Culture 2024 Autumn Edition
twada
PRO
8
3.3k
CSC509 Lecture 03
javiergs
PRO
0
150
ポケモンで考えるコミュニケーション / Communication Lessons from Pokémon
mackey0225
5
220
/←このスケジュール表に立ち向かう フロントエンド開発戦略 / A front-end development strategy to tackle a single-slash schedule.
nrslib
1
390
Featured
See All Featured
Making Projects Easy
brettharned
115
5.9k
A Philosophy of Restraint
colly
203
16k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
26
700
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
92
16k
Why Our Code Smells
bkeepers
PRO
334
57k
Ruby is Unlike a Banana
tanoku
96
11k
A Modern Web Designer's Workflow
chriscoyier
692
190k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
Build The Right Thing And Hit Your Dates
maggiecrowley
32
2.3k
Docker and Python
trallard
40
3k
Producing Creativity
orderedlist
PRO
341
39k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
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で お待ちしています💙