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
Kotlinのifを愛でる
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
okuzawats
March 11, 2022
Programming
1.2k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Kotlinのifを愛でる
「明日思わず誰かに伝えたくなる話 フラー x ちゅらデータ合同勉強会」(2022年3月11日)での発表資料です。
okuzawats
March 11, 2022
More Decks by okuzawats
See All by okuzawats
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
520
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
510
カンファレンス参加をいかに正当化するか
okuzawats
0
400
「勉強になった」で終わらせない、ストロングスタイルの勉強会
okuzawats
0
480
10年モノのAndroidアプリのコード品質を改善していく、3つの取り組み
okuzawats
0
1.5k
Androidアプリ開発におけるSonarCloudの活用
okuzawats
0
1.4k
何故、UseCaseは1メソッドなのか
okuzawats
3
2.4k
例外を投げるな、値を返せ
okuzawats
9
8.3k
GitHub ActionsでAndroidアプリのテストを回しまくってたら全プロジェクトのCI/CDが完全停止する寸前だった件
okuzawats
0
780
Other Decks in Programming
See All in Programming
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
420
Java 27新機能 / Java 27 new features
kishida
2
190
海上で動くGoサーバー: goroutineとchannelでさばく航行データストリーム
atsuki_seo
0
1k
The Rails Doctrine Decade
koic
2
440
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
330
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
4.7k
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
220
テストを司るデーモンに会いに行く 〜隔離した仮想マシンでテストを通すまで〜
h1d3mun3
1
600
JRuby: Past, Present, and Future
headius
0
220
選挙速報を多くのユーザーへ 届ける Live Activities 設計
hamayokokuririn
0
190
FreeBSDでZabbixを動かす
kenkino
0
340
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
660
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
270
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Facilitating Awesome Meetings
lara
57
7.2k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
440
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.9k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
240
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
480
Transcript
Kotlinのifを愛でる okuzawats 明日思わず誰かに伝えたくなる話 2022/03/11
自己紹介 奥澤(@okuzawats) Androidアプリエンジニ ア@フラー株式会社 テックリード 「Jetpack Composeに よるAndroid MVVMアー キテクチャ入門」の著者
今夜のトピックス 1. 自己紹介 2. なんでKotlinのifを愛でるのか 3. Coding Conventions 4. 事例
5. NG例 6. まとめ
なんでKotlinのifを愛でるのか ifは最も基本的なプログラムの構成要素 ifに思い入れを持ってプログラムを書くことで、他のところもいい感じに書けるようになる(かもしれない) 逆に言うと、ifすら蔑ろにすると他のところも残念なことになりそう(偏見) Coding conventionsを読む習慣を付ける
Coding Conventions Kotlin公式のCoding conventions https://kotlinlang.org/docs/coding-conventions.html AndroidのKotlin style guide https://developer.android.com/kotlin/style-guide
ifに関するCoding conventions AndroidのKotlin style guideには以下の記載がある
ifに関するCoding conventions つまり、 みたいに一行に収まる(三項演算子みたいな)if-elseか、 みたいにブロックを書くかのどちらか。
Kotlin公式のCoding conventionsには明確に書かれてないけどサンプルコードに示されるif-elseはこのスタイルで書かれている
ここまでが前置きで、これを踏まえて愛でていきます。
example.1 ひとつひとつreturnを書 く
example.1 ひとつひとつreturnを書 く 寸評 古き良き書き方実家のような安心感だが、IDEに怒られるのが玉に瑕。 Kotlinのifは値を返す式なので、ifを返せば良い。その方がKotlinらしいコードになる(と思う)。
example.2 ifをreturnする
example.2 ifをreturnする 寸評 直接if-elseの評価結果を返すので、どんな値がreturnされるのかがわかりやすい。 returnの前に早期リターンすることは可能なので、単にif-elseだけ見ていれば良いというわけではないのが玉に瑕。 example.1よりもKotlinらしいコードになった(と思う)。
example.3 ブロックを省略する
example.3 ブロックを省略する 寸評 Kotlin style guideに書かれている書き方(ただし一行に収まる場合)。三項演算子の代わりに使えてしまう。 Kotlinらしい書き方だけど、一行が長くなると改行が欲しくなり、ブロックを足さないといけなくなるのが玉に瑕。
example.4 関数本体を返す
example.4 関数本体を返す 寸評 早期リターンなどが発生し得ない書き方。 if-elseの評価結果が返されるので、どんな値が返されるのかは一目瞭然。 個人的には好き。 行数が少なくなって人類が幸せになる。
example.5 Scalaでみる書き方
example.5 Scalaでみる書き方 寸評 Scalaだとこういう書き方をする気がする(Scalaというか関数型言語?) 個人的にはこの書き方が一番好きだけど、チーム開発だと推しにくい。 example.3、example.4くらいがちょうどいいかもしれない。 書籍Functional Programming Kotlinではこの書き方をしているので、関数型に馴染みがあるとこういう書き方を好むのかもしれない。
NG例
NG1 ブロック省略&改行あり
NG1 ブロック省略&改行あり 寸評 Kotlin style guide違反。 何で違反になっているかはわからない Dangling else?
NG2 ifとelseが非対称
NG2 ifとelseが非対称 寸評 個人的にはNG。if-elseが非対称なので目が滑る。 if-elseを対象的に(同じ書き方で)書きたい。
発表時間の都合で以下略 処理の途中でifを使って副作用のあるコードを実行するパターン 早期リターンするパターン
まとめ Kotlinはifだけ見ても色々な書き方ができる。 Kotlinの言語機能を活かした、Kotlinらしいコードが書けると読みやすいコードになると思う。 自分たちだけの最強のifを見つけよう!