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
Null the abyss
Search
Keishin Yokomaku
May 13, 2015
Technology
3.2k
1
Share
Null the abyss
We need to look after 'null' value carefully.
Keishin Yokomaku
May 13, 2015
More Decks by Keishin Yokomaku
See All by Keishin Yokomaku
Base64 in Android
keithyokoma
0
51
One screen, many BottomSheets
keithyokoma
0
450
LazyColumnのitemがViewPortの中で占める領域の割合を知りたい
keithyokoma
0
740
Build apps for Cars
keithyokoma
0
580
Save the state
keithyokoma
0
620
Either in Kotlin
keithyokoma
0
640
持続的なアプリ開発のためのDXを支える技術
keithyokoma
2
5.6k
Make the objects serializable with kotlinx.serialization
keithyokoma
0
5.3k
Kotlin で書く Gradle Custom Tasks
keithyokoma
0
580
Other Decks in Technology
See All in Technology
AWS WAFの運用を地道に改善し、自社で運用可能にするプラクティス
andpad
1
630
JaSSTに関わることで変わった人生観 #jasstnano
makky_tyuyan
0
160
サプライチェーン攻撃への備えについて考えている #湘なんか
stefafafan
2
2k
キャリア25年目にしてTypeScript に出会うまで - 「型」を通じて振り返るプログラミング言語遍歴 / Meeting TypeScript After 25 Years in Tech - Looking Back at My Programming Language Journey Through "Types"
bitkey
PRO
2
120
GCASアップデート(202603-202605)
techniczna
0
240
コーディングエージェントはTypeScriptの 型エラーをどう自己修正しているのか
melonps
3
260
SpeechTranscriber + AIによる文字起こし機能
kazuki1220
0
120
TSKaigi 2026 - 型プラグインシステムの実装に使われるテクニック
teamlab
PRO
1
120
TypeScriptはどのようにどこまで推論できるのか ─ とにかく as は禁止で
ypresto
1
260
Oracle AI Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
6
1.7k
The Making of AI Chips
pfn
PRO
0
550
既存プロダクトQAから新規プロダクトQAへ
ryotakahashi
0
170
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
Claude Code のすすめ
schroneko
67
220k
Facilitating Awesome Meetings
lara
57
6.9k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
199
73k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.2k
Embracing the Ebb and Flow
colly
88
5k
Music & Morning Musume
bryan
47
7.2k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9k
Odyssey Design
rkendrick25
PRO
2
620
Transcript
Null, the Abyss @KeithYokoma - Drivemode, Inc. potatotips #17
KeithYokoma Keishin Yokomaku Drivemode, Inc. Android Engineer GitHub: https://github.com/KeithYokoma Qiita:
http://qiita.com/KeithYokoma e-Book: http://amzn.to/1mZNydv
None
˒ˑˑˑˑ
None
None
˒ˑˑˑˑ ˒ˑˑˑˑ ˒ˑˑˑˑ
–Someone “A lot of sadness is coming from NullPointerException”
Avoid NullPointerException • Null check for nullable value • Using
support annotation • Using NullObject pattern • Returning null value • Null abyss in Android
Null check statement public void method(Object arg) { if (arg
== null) { throw new NullPointerException(); } // … }
Null check statement public void method(Object arg) { if (arg
== null) { throw new NullPointerException(); } // … }
Null check statement ✓ Easy and basic way to avoid
NPE ✗ Too many checks is bad for performance ✗ Need documentation for nullity ✗ Failure at runtime
Support Annotations public void method(@Nullable Object arg) { if (arg
== null) { Log.w(“Logger”, “Nothing I can do.”) return; } // … }
Support Annotations public void method(@Nullable Object arg) { if (arg
== null) { Log.w(“Logger”, “Nothing I can do.”) return; } // … }
Support Annotations ✓ Gain Lint support for nullity ✓ Users
can understand about nullity ✗ Still have possibility of null value ✗ Failure at runtime
Using NullObject pattern ✓ No possibility of null value ✓
No change to get NullPointerException ✗ Need to learn architecture
Using NullObject pattern Bad Practice public enum NavMenu { HOME(R.id.home),
PROFILE(R.id.profile); public static NavMenu get(int id) { for (NavMenu menu : values()) { if (menu.id == id) return menu; } return null; } }
Using NullObject pattern Good Practice public enum NavMenu { HOME(R.id.home),
PROFILE(R.id.profile), NONE(-1); public static NavMenu get(int id) { for (NavMenu menu : values()) { if (menu.id == id) return menu; } return NONE; } }
Returning null value Bad Practice public List<Result> get(int count) {
if (something went wrong) { return null; } //… }
Returning null value Good Practice public List<Result> get(int count) {
if (something went wrong) { return new ArrayList<>(); } //… }
Returning null value Other Good Practice public List<Result> get(int count)
{ if (something went wrong) { throw new SomeException(“Request failed for some reason.”); } //… }
Returning null value • “null” means value is absent •
Empty collection instead of null • Failure for throwing Exception
Null abyss in Android • Some support API returns “null”
• e.g. MediaSessionCompat • You need to verify nullity for those APIs…
None
–Someone “When you gaze into null, null gazes into you”
For more details… • Effective Java
Null, the Abyss @KeithYokoma - Drivemode, Inc. potatotips #17