null, the abyss

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

Upload: keishin-yokomaku

Post on 06-Aug-2015

56 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Null, the Abyss

Null, the Abyss@KeithYokoma - Drivemode, Inc.

potatotips #17

Page 2: Null, the Abyss

KeithYokoma

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

Page 3: Null, the Abyss
Page 4: Null, the Abyss

★☆☆☆☆

Page 5: Null, the Abyss
Page 6: Null, the Abyss
Page 7: Null, the Abyss

★☆☆☆☆ ★☆☆☆☆ ★☆☆☆☆

Page 8: Null, the Abyss

–Someone

“A lot of sadness is coming from NullPointerException”

Page 9: Null, the Abyss

Avoid NullPointerException

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

Page 10: Null, the Abyss

Null check statement

public void method(Object arg) { if (arg == null) { throw new NullPointerException(); }

// …}

Page 11: Null, the Abyss

Null check statement

public void method(Object arg) { if (arg == null) { throw new NullPointerException(); }

// …}

Page 12: Null, the Abyss

Null check statement

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

Page 13: Null, the Abyss

Support Annotations

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

// …}

Page 14: Null, the Abyss

Support Annotations

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

// …}

Page 15: Null, the Abyss

Support Annotations

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

Page 16: Null, the Abyss

Using NullObject pattern

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

Page 17: Null, the Abyss

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

Page 18: Null, the Abyss

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

Page 19: Null, the Abyss

Returning null value Bad Practice

public List<Result> get(int count) { if (something went wrong) { return null; }

//…}

Page 20: Null, the Abyss

Returning null value Good Practice

public List<Result> get(int count) { if (something went wrong) { return new ArrayList<>(); }

//…}

Page 21: Null, the Abyss

Returning null value Other Good Practice

public List<Result> get(int count) { if (something went wrong) { throw new SomeException(“Request failed for some reason.”); }

//…}

Page 22: Null, the Abyss

Returning null value

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

Page 23: Null, the Abyss

Null abyss in Android

• Some support API returns “null” • e.g. MediaSessionCompat

• You need to verify nullity for those APIs…

Page 24: Null, the Abyss
Page 25: Null, the Abyss

–Someone

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

Page 26: Null, the Abyss

For more details…

• Effective Java

Page 27: Null, the Abyss

Null, the Abyss@KeithYokoma - Drivemode, Inc.

potatotips #17