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
例外を投げるな、値を返せ
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
okuzawats
March 31, 2023
Programming
8.3k
9
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
例外を投げるな、値を返せ
DroidKaigi.collect{ #1@Tokyo }(2023年3月31日)での発表資料です。
okuzawats
March 31, 2023
More Decks by okuzawats
See All by okuzawats
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
520
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
500
カンファレンス参加をいかに正当化するか
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
GitHub ActionsでAndroidアプリのテストを回しまくってたら全プロジェクトのCI/CDが完全停止する寸前だった件
okuzawats
0
780
Kotlinのifを愛でる
okuzawats
0
1.2k
Other Decks in Programming
See All in Programming
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
140
大喜利で理解するLLM as a Judge / Understanding LLM-as-a-Judge through Ogiri
rockname
0
170
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
360
MCP Appsでつくるチャット駆動な地図アプリ
nakamori1024
0
110
The Rails Doctrine Decade
koic
2
410
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
210
Workers Cache を知る
syumai
0
310
Are APIs Still Relevant in the AI Era?
soyuka
0
330
SREの越境 / SRE Collaboration
y0hgi
2
290
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
160
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
280
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
400
Featured
See All Featured
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
510
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
960
Product Roadmaps are Hard
iamctodd
55
13k
Building Adaptive Systems
keathley
44
3.2k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
250
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
We Have a Design System, Now What?
morganepeng
55
8.3k
Ruling the World: When Life Gets Gamed
codingconduct
0
370
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Writing Fast Ruby
sferik
630
63k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
540
Transcript
例外を投げるな、値を返せ DroidKaigi.collect{ #1@Tokyo }
話している人 • 奥澤(@okuzawats) • Chatwork株式会社(2022年9月〜) • ビジネスチャット「Chatwork」Android版のアーキテクチャ改善業、技術負債返済業をやっています。 • 好きなKotlinのスコープ関数はwithです。
今日話したいこと • 例外を投げるべきでない100の理由 • 例外を投げる代わりに値を返す、たったひとつの冴えたやり方
例外は2種類に分けられる。 • 回復不能な例外 • 回復可能な例外
例外は2種類に分けられる。 • 回復不能な例外 • 回復可能な例外←
例外を投げるべきでない100の理由 1. 参照透過性が失われる 2. 型安全でない • 型は例外について何も語らない • コンパイラも例外の処理を呼び出し元に強制しない (Paul
Chiusano, Rúnar Bjarnason, (2015)) 👉例外を投げる代わりに値を返すようにする。
None
͜͏͍͏͜ͱΛݴ͍͍ͨͷͰͳ͍ ݹ͖ྑ͖C-style🕺
例外を投げる代わりに値を返す、 たったひとつの冴えたやり方
正常・異常の直和を表現できる型を使う 1. 独自の型を定義する 2. `kotlin.Result<T>` を使う Kotlin 1.3〜。Kotlinを使っていれば使えるのがメリット。 3. `arrow.core.Either<Throwable,
T>` を使う 高機能。arrow-ktを入れる必要があるのがデメリット。
kotlin.Result<T>
arrow.core.Either<Throwable, T>
所感 1. `arrow.core.Either<Throwable, T>` は、例外の型も関数のシグネチャに含められる点が強い。`kotlin.Result<T>` はそれができない分少し弱い。 2. とはいえ、`arrow.core.Either<Throwable, T>` はarrow-ktの一機能に過ぎず、arrow-ktを入れるとそれ以外にも大量に入ってしまう。
3. 関数型に目覚めるのでなければ `kotlin.Result<T>` で済ませるか、必要最小限の機能を持ったEitherを自分で書くくらいが落としどころかもしれない(そんなに難しいことではない)。
まとめ 1. 回復可能な例外は値を返したい 2. 値を返すと言っても、 `return -1` ではない 3. 正常・異常を直和で表現できる型を使う
4. 独自に型を定義してもいいが、既に存在する `kotlin.Result<T>` や `arrow.core.Either<Throwable, T>` を使う手もある
参考文献 1. arrow.core.Either, retrieved from https://arrow-kt.io/docs/apidocs/arrow-core/arrow.core/-either/ 2. kotlin.Result, https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/ 3.
Paul Chiusano, Rúnar Bjarnason, (2015), Scala関数型デザイン&プログラミング, インプレス 4. Keishin Yokomaku, (2023), KotlinでEitherしたい, retrieved from https://speakerdeck.com/keithyokoma/either-in-kotlin