Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
null判定をis_nullでしてたけど止めた話
Search
Nakamigawa
January 24, 2024
540
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
null判定をis_nullでしてたけど止めた話
PHP勉強会@東京20240124
Nakamigawa
January 24, 2024
More Decks by Nakamigawa
See All by Nakamigawa
宅配クリーニングサービス「Lenet」開発におけるDDD7年の歩み
nakamigawa
0
3.5k
PHPカンファレンス関西202402 nullsafe演算子を使おう〜if文地獄からの開放〜
nakamigawa
0
620
クレジットマスターとの戦い
nakamigawa
0
1.8k
Featured
See All Featured
A Tale of Four Properties
chriscoyier
163
24k
What's in a price? How to price your products and services
michaelherold
247
13k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
4 Signs Your Business is Dying
shpigford
187
23k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
Believing is Seeing
oripsolob
1
240
YesSQL, Process and Tooling at Scale
rocio
174
15k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
31k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
400
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
350
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.2k
Why Our Code Smells
bkeepers
PRO
340
58k
Transcript
null判定をis_nullでしてたけど 止めた話 PHP勉強会@東京 2024/01/24 仲見川勝人@NakMeKtt
- 仲見川 勝人(@NakMeKtt) - 職業 - Software Engineer - Mobile
App, Front End, Server Side - 所属 - 株式会社ホワイトプラス - 宅配クリーニング「リネット」の開発 - Tech lead - アプリ開発G Engineering Manager 2 自己紹介
3 みなさんnullチェック どうしてますか?
1. is_null($var) 2. !isset($var) 3. $var === null 4
メリデメは3年前に@o0h_さんが書かれたこちらの記事が詳し く書かれていますので割愛 「is_null()を使うか === nullを使うか」と何気なく聞いてみたら面白かった (@o0h_) https://daisuki.nichiyoubi.land/entry/2020/12/14/010345 5
6 まず、 なんで自分のチームが nullチェックの 方法を決めたのか (決めたかったのか)
ある日のPRできたコード(イメージ)、 nullチェックとして変数を そのまま評価。 7
1. is_null($var) 2. !isset($var) 3. $var === null 4. $var
<-第四の選択肢か? 8
いいえ、この書き方は Falsy(フォルシー)※1な値か どうかで判定されてしまうため 意図通りにならない可能性があり、 適切ではありません。 ※1:Falseとして扱われる、いわゆる曖昧な比較でFalseになるやつ 9
先ほどはDateTimeのObjectなので まぁまぁ問題は起きなそう。 (善し悪しは置いておき) intだとわかりやすく問題がある。 (実際そんな書き方するか? という疑問はいったん脇へ置き) 0を評価するとFalseになってしまうため、 No4のケースで意図通りに動かない 10
Falsyなチェックを避けきちんとnullチェックを行うために、 nullチェックの方法をチーム内で検討しADR※1としてルール化。 いくつかある方法で今回の様に意図と違う書き方をしないための 補助輪(ガイド)を設けたかった。 ※1:Architectural Decision Records アーキテクチャの決定にいたる経緯(検討、議論内容)を残し どのような判断をしたのかを記録するドキュメント 私たちはPRレビューでの揺れを低減するために今回のような複数の書き方のあるもの
も含めています。 11
このときの議論ではこの辺の意見がチーム内では多くis_null()を採 用した。 - 用意されているのであればそれを使うのがよいのではないか - nullである事を評価する場合視認性がよさそう - 既存処理の都合もあり、Lintは設定出来ないので厳密な比較($var === null)ではなく曖昧に書いてしまうのが怖い($var
== null) というのが2022年頃の話。 12
13 時は流れ2023年
!is_null() が見づらいね? 思ったよりもis_null()を反転したい機会が多かった。 PHP8へのバージョンアップタスクで曖昧な比較が消え去った。 (=Lintが設定出来る、したとは言って居ない) と言う事で再度検討して$var !== nullを使う方針に。 コードの読みやすさ(目が滑らない)が決め手 気持ちとしてはis_not_null()が有ればそれを使いたかった
14
しかし! nullsafe演算子のお陰でnullチェックが必要な機会は減ります! 15
nullsafe演算子についてはPHPカンファレンス関西で話します! nullsafe演算子を使おう〜if文地獄からの開放〜 16