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
290
LazyColumnのitemがViewPortの中で占める領域の割合を知りたい
keithyokoma
0
510
Build apps for Cars
keithyokoma
0
440
Save the state
keithyokoma
0
500
Either in Kotlin
keithyokoma
0
490
持続的なアプリ開発のためのDXを支える技術
keithyokoma
2
4.9k
Make the objects serializable with kotlinx.serialization
keithyokoma
0
4.8k
Kotlin で書く Gradle Custom Tasks
keithyokoma
0
480
DX Improvements
keithyokoma
3
370
Other Decks in Technology
See All in Technology
Unlearn Modularity
lemiorhan
6
300
ReSTIRの数理と実装 (rtcamp10)
yumcyawiz
1
470
テストを楽に書きたい
tomorrowkey
2
270
0x5F3759DF
ykozw
0
330
今日から始める技術的負債の解消
leveragestech
3
440
さくっと実践!Postmanを活用した高品質で持続可能なAPI管理
yokawasa
5
360
とある事業会社にとっての Kaggler の魅力
hakubishin3
7
1.6k
本番のトラフィック量でHudiを検証して見えてきた課題
joker1007
2
260
Transforming Event Attendees into Lifelong Donors: Insights from Claire Axelrad
auctria
PRO
1
110
暴カワでビデオシンセサイザーを導入する技術
yuchi
2
120
生成AIの活用パターンと継続的評価
asei
3
240
Delta Commit…の最近...
akuwano
2
120
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
504
140k
Code Review Best Practice
trishagee
63
17k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
40
2.1k
Adopting Sorbet at Scale
ufuk
73
9k
Ruby is Unlike a Banana
tanoku
96
11k
The Cost Of JavaScript in 2023
addyosmani
43
5.9k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
What's in a price? How to price your products and services
michaelherold
243
11k
Rails Girls Zürich Keynote
gr2m
93
13k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3.5k
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
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