$30 off During Our Annual Pro Sale. View Details »

Null the abyss

Null the abyss

We need to look after 'null' value carefully.

Keishin Yokomaku

May 13, 2015
Tweet

More Decks by Keishin Yokomaku

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

  3. View Slide

  4. ˒ˑˑˑˑ

    View Slide

  5. View Slide

  6. View Slide

  7. ˒ˑˑˑˑ
    ˒ˑˑˑˑ
    ˒ˑˑˑˑ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  24. View Slide

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

    View Slide

  26. For more details…
    • Effective Java

    View Slide

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

    View Slide