Slide 1

Slide 1 text

Null, the Abyss @KeithYokoma - Drivemode, Inc. potatotips #17

Slide 2

Slide 2 text

KeithYokoma Keishin Yokomaku Drivemode, Inc. Android Engineer GitHub: https://github.com/KeithYokoma Qiita: http://qiita.com/KeithYokoma e-Book: http://amzn.to/1mZNydv

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

˒ˑˑˑˑ

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

˒ˑˑˑˑ ˒ˑˑˑˑ ˒ˑˑˑˑ

Slide 8

Slide 8 text

–Someone “A lot of sadness is coming from NullPointerException”

Slide 9

Slide 9 text

Avoid NullPointerException • Null check for nullable value • Using support annotation • Using NullObject pattern • Returning null value • Null abyss in Android

Slide 10

Slide 10 text

Null check statement public void method(Object arg) { if (arg == null) { throw new NullPointerException(); } // … }

Slide 11

Slide 11 text

Null check statement public void method(Object arg) { if (arg == null) { throw new NullPointerException(); } // … }

Slide 12

Slide 12 text

Null check statement ✓ Easy and basic way to avoid NPE ✗ Too many checks is bad for performance ✗ Need documentation for nullity ✗ Failure at runtime

Slide 13

Slide 13 text

Support Annotations public void method(@Nullable Object arg) { if (arg == null) { Log.w(“Logger”, “Nothing I can do.”) return; } // … }

Slide 14

Slide 14 text

Support Annotations public void method(@Nullable Object arg) { if (arg == null) { Log.w(“Logger”, “Nothing I can do.”) return; } // … }

Slide 15

Slide 15 text

Support Annotations ✓ Gain Lint support for nullity ✓ Users can understand about nullity ✗ Still have possibility of null value ✗ Failure at runtime

Slide 16

Slide 16 text

Using NullObject pattern ✓ No possibility of null value ✓ No change to get NullPointerException ✗ Need to learn architecture

Slide 17

Slide 17 text

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; } }

Slide 18

Slide 18 text

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; } }

Slide 19

Slide 19 text

Returning null value Bad Practice public List get(int count) { if (something went wrong) { return null; } //… }

Slide 20

Slide 20 text

Returning null value Good Practice public List get(int count) { if (something went wrong) { return new ArrayList<>(); } //… }

Slide 21

Slide 21 text

Returning null value Other Good Practice public List get(int count) { if (something went wrong) { throw new SomeException(“Request failed for some reason.”); } //… }

Slide 22

Slide 22 text

Returning null value • “null” means value is absent • Empty collection instead of null • Failure for throwing Exception

Slide 23

Slide 23 text

Null abyss in Android • Some support API returns “null” • e.g. MediaSessionCompat • You need to verify nullity for those APIs…

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

–Someone “When you gaze into null, null gazes into you”

Slide 26

Slide 26 text

For more details… • Effective Java

Slide 27

Slide 27 text

Null, the Abyss @KeithYokoma - Drivemode, Inc. potatotips #17