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
PHPで FizzBuzz問題をやってみよう #ゆるWeb札幌
Search
Kihara, Takuya
PRO
March 30, 2019
Technology
1
1.5k
PHPで FizzBuzz問題をやってみよう #ゆるWeb札幌
PHPでFizzBuzz問題を解いていく。
シンプルな解き方から、段階的にオブジェクト指向を取り入れてみる。
ゆるWeb勉強会@札幌 #4
#ゆるWeb札幌
Kihara, Takuya
PRO
March 30, 2019
Tweet
Share
More Decks by Kihara, Takuya
See All by Kihara, Takuya
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
380
Amplify Gen2 Deep Dive / How to convey backend types to the frontend #30DaysOfAmplify
tacck
PRO
0
5
TypeScript 雰囲気勢がライブラリのコードリーディングで得た知識 #sacss
tacck
PRO
1
30
ライブコーディングで知るAmplify Gen 2 #AWSAmplifyJP
tacck
PRO
1
120
AWS Amplify Gen 2 GA記念 ~Gen1との違いを確認
tacck
PRO
0
340
Amplify Gen2 が GA したよ! #jawsug_sapporo #jawsug
tacck
PRO
0
310
Flutter で "Hello World!! #ゆるWeb札幌
tacck
PRO
0
140
AWS Lambda で LLRT を動かしてみた #北海道まったりLT大会
tacck
PRO
0
140
地方から始める登壇のステップアップ 〜Java Do から JJUG CCC への登壇参加記録 #javado
tacck
PRO
0
190
Other Decks in Technology
See All in Technology
iOSチームとAndroidチームでブランチ運用が違ったので整理してます
sansantech
PRO
0
130
Adopting Jetpack Compose in Your Existing Project - GDG DevFest Bangkok 2024
akexorcist
0
110
強いチームと開発生産性
onk
PRO
34
11k
【若手エンジニア応援LT会】ソフトウェアを学んできた私がインフラエンジニアを目指した理由
kazushi_ohata
0
150
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
28
12k
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
[FOSS4G 2024 Japan LT] LLMを使ってGISデータ解析を自動化したい!
nssv
1
210
Shopifyアプリ開発における Shopifyの機能活用
sonatard
4
250
Platform Engineering for Software Developers and Architects
syntasso
1
520
Evangelismo técnico: ¿qué, cómo y por qué?
trishagee
0
360
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
500
Taming you application's environments
salaboy
0
180
Featured
See All Featured
What's new in Ruby 2.0
geeforr
343
31k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Designing Experiences People Love
moore
138
23k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
How to train your dragon (web standard)
notwaldorf
88
5.7k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Ruby is Unlike a Banana
tanoku
97
11k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
Transcript
PHPͰ FizzBuzzΛ ͬͯΈΑ͏ 2019/03/30 tacck ゆるWeb勉強会@札幌 #4 #ゆるWeb札幌
୭ UBDDL ໊લ ࠷ۙݹͷ+BWBͷ͓कΓ ͓ࣄ εϓϨουɾΠʔάϧ ͖ͳϑΟΪϡΞεέʔτͷٕ ΏΔ8FCࡳຈ
ࠓ͓͢͠Δ͜ͱ • FizzBuzz • ͬͯΈΑ͏ • റΓϓϨΠ ΏΔ8FCࡳຈ
FizzBuzz ΏΔ8FCࡳຈ
FizzBuzz • ϓϩάϥϚʔͷᅂΈ ΏΔ8FCࡳຈ
FizzBuzz • 3ͷഒͰ “Fizz” ͱग़ྗ • 5ͷഒͰ “Buzz” ͱग़ྗ •
3ͷഒ ͔ͭ 5ͷഒͰ “FizzBuzz” ͱग़ྗ (ͭ·Γ15ͷഒ) • ͦΕҎ֎ࣗମΛจࣈͱͯ͠ग़ྗ ΏΔ8FCࡳຈ
FizzBuzz • ࠓճɺ • 1ʙ100·ͰͷΛ༩͑Δɻ • લͷϖʔδͷنଇʹैͬͯग़ྗ͢Δɻ • ͱ͍͏ϓϩάϥϜΛ࡞͍ͯ͘͠ɻ ΏΔ8FCࡳຈ
ͬͯΈΑ͏ ΏΔ8FCࡳຈ
Step1: ૉͳ࣮ ΏΔ8FCࡳຈ
ૉͳ࣮ • ࡉ͔͍͜ͱؾʹͤͣʹɻ https://github.com/tacck/PHPFizzBuzzTraining/tree/Step1 ΏΔ8FCࡳຈ
<?php for ($num = 1; $num <= 100; $num++) {
if ($num % 3 === 0 && $num % 5 === 0) { echo 'FizzBuzz' . PHP_EOL; } elseif ($num % 3 === 0 && $num % 5 !== 0) { echo 'Fizz' . PHP_EOL; } elseif ($num % 3 !== 0 && $num % 5 === 0) { echo 'Buzz' . PHP_EOL; } else { echo "$num" . PHP_EOL; } }
Step2: ॳ৺ऀ͚ ΏΔ8FCࡳຈ
ॳ৺ऀ͚ • FizzBuzzͷఆΛؔ(ϝιου)Խɻ https://github.com/tacck/PHPFizzBuzzTraining/tree/Step2 ΏΔ8FCࡳຈ
<?php for ($num = 1; $num <= 100; $num++) {
echo getFizzBuzz($num) . PHP_EOL; } function getFizzBuzz(int $num): string { if ($num % 3 === 0 && $num % 5 === 0) { return 'FizzBuzz'; } elseif ($num % 3 === 0 && $num % 5 !== 0) { return 'Fizz'; } elseif ($num % 3 !== 0 && $num % 5 === 0) { return 'Buzz'; } else { return "$num"; } }
Step3: ॳ৺ऀΑΓ্Λࢦ͢ ΫϥεΛ͏ ΏΔ8FCࡳຈ
ΫϥεΛ͏ • ׂͷ୲ΛΫϥεͰදݱ͢Δɻ • 1ʙ100ͷʹରԠ͢ΔจࣈྻΛදࣔ • ͱจࣈͷରԠؔΛఆ͠ฦ٫ https://github.com/tacck/PHPFizzBuzzTraining/tree/Step3 ΏΔ8FCࡳຈ
<?php require_once __DIR__ . '/vendor/autoload.php'; use App\FizzBuzz\FizzBuzz; $fizzBuzz = new
FizzBuzz(); for ($num = 1; $num <= 100; $num++) { echo $fizzBuzz->getFizzBuzz($num) . PHP_EOL; }
<?php namespace App\FizzBuzz; class FizzBuzz { public function getFizzBuzz(int $num):
string { if ($num % 3 === 0 && $num % 5 === 0) { return 'FizzBuzz'; } elseif ($num % 3 === 0 && $num % 5 !== 0) { return 'Fizz'; } elseif ($num % 3 !== 0 && $num % 5 === 0) { return 'Buzz'; } else { return "$num"; } } }
Step4: தڃऀ͚ ΫϥεͷΛ͚Δ ΏΔ8FCࡳຈ
Ϋϥεͷ? • ͦͷΫϥε͕ʮҰͭͷʯʹด͍ͯ͡Δ͔ • ؔ৺ͷɺૄ݁߹ɺͱ͍ͬͨΩʔϫʔυɻ • 1ʙ100ͷʹରԠ͢ΔจࣈྻΛදࣔ • ͱจࣈͷରԠؔͷఆ •
ରԠ͢Δจࣈྻͷฦ٫ https://github.com/tacck/PHPFizzBuzzTraining/tree/Step4 ΏΔ8FCࡳຈ
ؔ৺ • ಛఆͷΛ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ • ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ • จࣈྻΛ֬ఆ͍ͤͨ͞ɻ ΏΔ8FCࡳຈ
ؔ৺ͱ • ಛఆͷΛ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ ˠ ϝιουͷݺͼग़͠ͱ݁Ռͷग़ྗ • ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ ˠ Λड͚औΓɺରԠ͢Δදࣔ༰ͷฦ٫ •
දࣔ༰Λ֬ఆ͍ͤͨ͞ɻ ˠ ͕ࣗར༻͞ΕΔ߹ʹදࣔ༰Λฦ٫ ΏΔ8FCࡳຈ
ؔ৺ͱ • ಛఆͷΛ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ ˠ ϝιουͷݺͼग़͠ͱग़ྗ • ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ ˠ Λड͚औΓɺରԠ͢Δදࣔ༰ͷฦ٫ •
දࣔ༰Λ֬ఆ͍ͤͨ͞ɻ ˠ ͕ࣗར༻͞ΕΔ߹ʹදࣔ༰Λฦ٫ ݺͼग़ͯ͠͏ ݺͼग़ͯ͠͏ ΏΔ8FCࡳຈ
ؔ৺ͱ ΏΔ8FCࡳຈ ΠϯλϑΣʔε ೖग़ྗ ͷ ਖ਼͍͠σʔλΛड͚͢ ਖ਼͍͠σʔλͰ͋Δ ґଘؔ Ұํ ґଘؔ
Ұํ
<?php require_once __DIR__ . '/vendor/autoload.php'; use App\FizzBuzz\FizzBuzzPresenterBuilder; for ($num =
1; $num <= 100; $num++) { echo FizzBuzzPresenterBuilder::build($num) . PHP_EOL; }
<?php namespace App\FizzBuzz; use App\FizzBuzz\FizzBuzzPresenterInterface; class FizzBuzzPresenterBuilder { public static
function build(int $num): FizzBuzzPresenterInterface { if ($num % 3 === 0 && $num % 5 === 0) { return new \App\FizzBuzz\Presenter\FizzBuzzPresenter($num); } elseif ($num % 3 === 0 && $num % 5 !== 0) { return new \App\FizzBuzz\Presenter\FizzPresenter($num); } elseif ($num % 3 !== 0 && $num % 5 === 0) { return new \App\FizzBuzz\Presenter\BuzzPresenter($num); } else { return new \App\FizzBuzz\Presenter\NumberPresenter($num); } } }
<?php namespace App\FizzBuzz\Presenter; use App\FizzBuzz\FizzBuzzPresenterInterface; class FizzPresenter implements FizzBuzzPresenterInterface {
private $num; public function __construct(int $num) { $this->num = $num; } public function __toString(): string { return 'Fizz'; } public function isAvailable(): bool { return true; } }
Step5: റΓϓϨΠ ifจΛΘͳ͍Ͱ࣮͢Δ ΏΔ8FCࡳຈ
ifจΛΘͳ͍Ͱ࣮͢Δ • ಄ͷମૢ ΏΔ8FCࡳຈ
ifจΛΘͳ͍Ͱ࣮͢Δ • ಄ͷମૢ • ݱ࣮ͱͯ͠ɺ ଟͷ݅ذόάΛੜΈ͍͢ɻ ˠ ʮଟʯͷ෦Λղ͖΄͙͢ https://github.com/tacck/PHPFizzBuzzTraining/tree/Step5 ΏΔ8FCࡳຈ
ؔ৺ͱ • ಛఆͷΛ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ ˠ ϝιουͷݺͼग़͠ͱग़ྗ • ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ ˠ Λड͚औΓɺରԠ͢Δදࣔ༰ͷฦ٫ •
දࣔ༰Λ֬ఆ͍ͤͨ͞ɻ ˠ ͕ࣗར༻͞ΕΔ߹ʹදࣔ༰Λฦ٫ ΏΔ8FCࡳຈ ͜͜ʹ݅ذ
ؔ৺ͱ • ಛఆͷΛ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ ˠ ϝιουͷݺͼग़͠ͱग़ྗ • ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ ˠ Λड͚औΓɺରԠ͢Δදࣔ༰ͷฦ٫ •
දࣔ༰Λ֬ఆ͍ͤͨ͞ɻ ˠ ͕ࣗར༻͞ΕΔ߹ʹදࣔ༰Λฦ٫ ΏΔ8FCࡳຈ ͜͜ʹ
ؔ৺ͱ • ಛఆͷΛ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ ˠ ϝιουͷݺͼग़͠ͱग़ྗ • ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ ˠ Λड͚औΓɺରԠ͢Δදࣔ༰ͷฦ٫ •
දࣔ༰Λ֬ఆ͍ͤͨ͞ɻ ˠ ͕ࣗར༻͞ΕΔ߹ʹදࣔ༰Λฦ٫ ΏΔ8FCࡳຈ ͑Δ͔Ͳ͏͔ ʮදࣔ༰ʯ͕ࣗΔ͖
ؔ৺ͱ ΏΔ8FCࡳຈ ΠϯλϑΣʔε ೖग़ྗ ͷ ਖ਼͍͠σʔλΛड͚͢ ਖ਼͍͠σʔλͰ͋Δ
<?php require_once __DIR__ . '/vendor/autoload.php'; use App\FizzBuzz\FizzBuzzPresenterBuilder; for ($num =
1; $num <= 100; $num++) { echo FizzBuzzPresenterBuilder::build($num) . PHP_EOL; }
<?php namespace App\FizzBuzz; use App\FizzBuzz\FizzBuzzPresenterInterface; class FizzBuzzPresenterBuilder { public static
function build(int $num): FizzBuzzPresenterInterface { foreach (glob(__DIR__ . '/Presenter/Dynamic/*.php') as $file) { require_once $file; $class = '\App\FizzBuzz\Presenter\Dynamic\\' . basename($file, '.php'); $presenter = new $class($num); try { // throw ParseError if $num is not available $presenter->isAvailable(); return $presenter; } catch (\ParseError $e) { // none } } return new \App\FizzBuzz\Presenter\NumberPresenter($num); } }
<?php namespace App\FizzBuzz\Presenter\Dynamic; use App\FizzBuzz\FizzBuzzPresenterInterface; class FizzPresenter implements FizzBuzzPresenterInterface {
private $num; public function __construct(int $num) { $this->num = $num; } public function __toString(): string { return 'Fizz'; } public function isAvailable(): bool { return ($this->num % 3 === 0 && $this->num % 5 !== 0) ? true : eval('invalid'); } }
ifจΛΘͳ͍Ͱ࣮͢Δ • Ԡ༻ྫ • ݩ߸ • ৽ݩ߸͕ൃද͞ΕͨΒɺ PresenterΫϥεΛՃ͢Δ͚ͩɻ https://github.com/tacck/PHPJapaneseEraTraining ΏΔ8FCࡳຈ
·ͱΊ ΏΔ8FCࡳຈ
·ͱΊ • FizzBuzzΛɺ ͍͔ͭ͘ͷܗͰղ͍ͯΈͨɻ • ΦϒδΣΫτࢦͷߟ͑ํΛ͏·͘͏ͱɺ ҆શ͔֦ͭுੑΛߴ͘Ͱ͖Δɻ ˠ ࠓճͬͨͷ ɹ
ʮΦʔϓϯɾΫϩʔζυͷݪଇʯ • ಄ͷମૢΛ݉ͶͯɺറΓϓϨΠΛͯ͠ΈΑ͏! ΏΔ8FCࡳຈ
·ͱΊ • ͷ͚ํɺؔ৺ͷɺ ࠷ॳ͍͠ɻ • ʮ͜ͷਓ(Ϋϥε)ԿΛ͍ͬͯΔ͖͔ʯ ʮ͜ͷਓ(Ϋϥε)ʹԿΛͬͯཉ͍͔͠ʯ ਓ(Ϋϥε)ͷؾ࣋ͪʹͳͬͯߟ͑Δɻ • TDD
(ςετۦಈ։ൃ) ΛΔͱɺ ʮΫϥεʹ͍͔͚ͳ͕Βʯ։ൃͰ͖Δɻ ΏΔ8FCࡳຈ
࣍ճ༧ࠂ? • (PHPͰ)TDDΛͬͯΈΔ? ΏΔ8FCࡳຈ
end ΏΔ8FCࡳຈ