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.7k
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
LazyColumnのitemがViewPortの中で占める領域の割合を知りたい
keithyokoma
0
400
Build apps for Cars
keithyokoma
0
350
Save the state
keithyokoma
0
420
Either in Kotlin
keithyokoma
0
390
持続的なアプリ開発のためのDXを支える技術
keithyokoma
2
4.6k
Make the objects serializable with kotlinx.serialization
keithyokoma
0
4.4k
Kotlin で書く Gradle Custom Tasks
keithyokoma
0
450
DX Improvements
keithyokoma
3
360
Get Things Done with Gradle Custom Tasks
keithyokoma
1
93
Other Decks in Technology
See All in Technology
CI/CDがあたりまえの今の時代にAPIテスティングツールに求められていること / CI/CD Test Night #7
k1low
12
3k
Evolutionary Optimization of Model Merging Recipes
fuyu_quant0
3
540
生成AIサービスPanorama AIご説明資料
sdt
0
300
初心者が行く!サーバレスWebアプリ開発の道
nagaharutogawa
0
450
やっていきテスト
k6s4i53rx
0
160
VSCode上からSlackにメッセージを送る拡張機能を作っている話
ebarakazuhiro
0
120
中央集権体制からDataOpsへの転換 / centralized-to-dataops-transformation
pei0804
7
1.5k
マイクロサービス環境におけるDB戦略 in DMMプラットフォーム
pospome
11
3k
エンジニアブランディングチームの KPI / KPI's of engineer branding team
chaspy
1
140
MongoDB Atlas Vectorsearchではじめる生成AIアプリ開発
chie8842
3
510
匠MethodとRDRAとICONIXとDDDで実現する一気通貫オブジェクト指向開発
haru860
4
2.1k
これまでのキャリアとこれからMLエンジニアとしてどう動くか
masatakashiwagi
0
550
Featured
See All Featured
The Mythical Team-Month
searls
214
42k
JazzCon 2018 Closing Keynote - Leadership for the Reluctant Leader
reverentgeek
178
11k
Thoughts on Productivity
jonyablonski
57
3.8k
Faster Mobile Websites
deanohume
296
30k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
113
18k
Building Adaptive Systems
keathley
29
1.8k
5 minutes of I Can Smell Your CMS
philhawksworth
199
19k
KATA
mclloyd
14
11k
Why Our Code Smells
bkeepers
PRO
330
56k
Side Projects
sachag
451
41k
How to name files
jennybc
62
92k
Navigating Team Friction
lara
177
13k
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