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
1
2.9k
Null the abyss
We need to look after 'null' value carefully.
Keishin Yokomaku
May 13, 2015
Tweet
Share
More Decks by Keishin Yokomaku
See All by Keishin Yokomaku
One screen, many BottomSheets
keithyokoma
0
300
LazyColumnのitemがViewPortの中で占める領域の割合を知りたい
keithyokoma
0
530
Build apps for Cars
keithyokoma
0
450
Save the state
keithyokoma
0
500
Either in Kotlin
keithyokoma
0
510
持続的なアプリ開発のためのDXを支える技術
keithyokoma
2
4.9k
Make the objects serializable with kotlinx.serialization
keithyokoma
0
4.8k
Kotlin で書く Gradle Custom Tasks
keithyokoma
0
490
DX Improvements
keithyokoma
3
380
Other Decks in Technology
See All in Technology
透過型SMTPプロキシによる送信メールの可観測性向上: Update Edition / Improved observability of outgoing emails with transparent smtp proxy: Update edition
linyows
2
210
The Role of Developer Relations in AI Product Success.
giftojabu1
0
120
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
信頼性に挑む中で拡張できる・得られる1人のスキルセットとは?
ken5scal
2
530
AIチャットボット開発への生成AI活用
ryomrt
0
170
RubyのWebアプリケーションを50倍速くする方法 / How to Make a Ruby Web Application 50 Times Faster
hogelog
3
940
障害対応指揮の意思決定と情報共有における価値観 / Waroom Meetup #2
arthur1
5
470
サイバーセキュリティと認知バイアス:対策の隙を埋める心理学的アプローチ
shumei_ito
0
380
フルカイテン株式会社 採用資料
fullkaiten
0
40k
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
410
SSMRunbook作成の勘所_20241120
koichiotomo
2
130
Adopting Jetpack Compose in Your Existing Project - GDG DevFest Bangkok 2024
akexorcist
0
100
Featured
See All Featured
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
How to Ace a Technical Interview
jacobian
276
23k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Designing for humans not robots
tammielis
250
25k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Writing Fast Ruby
sferik
627
61k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
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