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
okuzawats
March 31, 2023
Programming
8.4k
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
530
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
520
カンファレンス参加をいかに正当化するか
okuzawats
0
410
「勉強になった」で終わらせない、ストロングスタイルの勉強会
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
800
Kotlinのifを愛でる
okuzawats
0
1.3k
Other Decks in Programming
See All in Programming
느린 네트워크와 저사양 디바이스 사용자를 위한 웹뷰 로딩 시간 개선기
kmkim
1
220
小さなアプリを作って公開するまで
onozaty
0
150
PyConTW 2026 - AI Coding 不只是工具升級, 區域醫院醫療資訊系統協作開發與資訊團隊轉型實踐
cclai999
0
240
Vue.jsで作る空間解析アプリとCapacitorプラグインのData Bridge
hal_spidernight
1
170
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
16
8.9k
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
570
Kawaiicon: Kubernetes, it's no longer cool, but let's secure it anyway!
barnbarn
0
130
Modernizing iOS Apps
seyfoyun
0
150
Web Framework Benchmark Battle 2026, Any%
a_matsuda
3
3.2k
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
3
1.7k
AI-Powered Domain-Driven Design
ziobrando
0
420
AI Augmentation Techniques with Liberty
ivargrimstad
1
410
Featured
See All Featured
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.3k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
450
The Invisible Side of Design
smashingmag
301
52k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
520
What's in a price? How to price your products and services
michaelherold
247
13k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
350
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
First, design no harm
axbom
PRO
2
1.4k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
390
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