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.3k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
55
One screen, many BottomSheets
keithyokoma
0
470
LazyColumnのitemがViewPortの中で占める領域の割合を知りたい
keithyokoma
0
760
Build apps for Cars
keithyokoma
0
600
Save the state
keithyokoma
0
630
Either in Kotlin
keithyokoma
0
650
持続的なアプリ開発のためのDXを支える技術
keithyokoma
2
5.6k
Make the objects serializable with kotlinx.serialization
keithyokoma
0
5.4k
Kotlin で書く Gradle Custom Tasks
keithyokoma
0
590
Other Decks in Technology
See All in Technology
『三匹の子ぶた』から学ぶネットワークセキュリティの昔と今 / Network Security: Then and Now Through the Lens of The Three Little Pigs
nttcom
1
1.8k
ホームラボ紹介
y_sera15
0
160
研究開発部の紹介 / Sansan R&D Profile
sansan33
PRO
4
24k
Eight Engineering Unit 紹介資料
sansan33
PRO
3
8.2k
DatadogのBits Chatが開発組織にもたらしたもの / What Bits Chat Has Brought Us
sms_tech
1
260
トヨタ⽣産⽅式(TPS)⼊⾨
recruitengineers
PRO
3
760
ボトムアップ文化が強い組織で セキュリティをどう根付かせていくかの現在進行形の話 / Making Security Stick in a Bottom-Up Organization
yamaguchitk333
0
220
【CEDEC2026】次世代デジタルカードゲームのサーバー設計と運用 〜『Shadowverse: Worlds Beyond』の舞台裏~
cygames
PRO
1
1.2k
老害フォレンジッカーはAI羊の夢を見るか?
tadmaddad
0
320
Model Studio CLI × Token Plan
maigo999
0
170
システム思考で問題に対処する
yussak
0
260
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
19k
Featured
See All Featured
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
370
The Cost Of JavaScript in 2023
addyosmani
55
10k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Speed Design
sergeychernyshev
33
2k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
350
Documentation Writing (for coders)
carmenintech
77
5.4k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
A Tale of Four Properties
chriscoyier
163
24k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
390
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Thoughts on Productivity
jonyablonski
76
5.3k
We Are The Robots
honzajavorek
0
300
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