Better Strategies for Null Handling in Java

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

6 comments

Comments 1 - 6 of 6 previous next Post a comment

  • + guestc23bca8 guestc23bca8 8 months ago
    I don’t understand guestfb50c’s argument. 'However, the ? notation is an idiom based solution, not a type system solution. You can always forget to invoke ? and get a NPE'. You can forget to use Option and get a NPE as well.
  • + keefie98 keefie98 9 months ago
    Thanks for materials Stephan. Having been through each slide it has reinforced my belief that moving to Groovy was a good move. Given the seamless, full integration of Groovy and Java I’m not sure why anyone wold choose the Option solution over the ’?’ solution. Performance maybe?
  • + HamletDRC HamletDRC 9 months ago
    @guest The Option type is a native type in a lot of other languages, which is an argument for its use... many programmers were taught this as part of their first language. Consistency is good, but I agree the ? notation is a little more soothing.
    However, the ? notation is an idiom based solution, not a type system solution. You can always forget to invoke ? and get a NPE. With the option type there is no way to drop off the check, especially if your language’s compiler enforces you to do it. So I prefer the option type along with a compiler that forces correct usage.
  • + guestc0dfdf4 guestc0dfdf4 9 months ago
    I must be really really old school.

    I don’t mind the .? operators. They seem natural and non-obtrusive.

    However, the 'Option/None/Some' idea seemed way too verbose and non-obvious.

    ...unless I really didn’t understand the notion.

    Randy
  • + Stephan.Schmidt Stephan Schmidt 9 months ago
    See Slide 5.
  • + guest87b50b guest87b50b 9 months ago
    I don’t see how the proposal is any better than Groovy’s safe navigation + Elvis operator (for default values)
Post a comment
Embed Video
Edit your comment Cancel

4 Favorites

Better Strategies for Null Handling in Java - Presentation Transcript

  1. Better Strategies for Null Handling in Java Stephan Schmidt Team manager PMI-3 Berlin, 05.08.2008
  2. Most problematic errors in Java 2 runtime problems in Java ClassCastException „Solved“ with Generics NullPointerException (NPE) Solution? 2
  3. Problems with NPEs RunTime Exception – Point of NPE easy to find – => But not clear where the NULL value comes from 3
  4. Handling of NULL Values Check after Check before String name = map.get(\"Hello\"); if (map.containsKey(\"Hello\")) { if (name != null) { String name = map.get(“hallo”); ... } else { … } } else { … } Easy to forget – No support from type system – No tracking of NULL values – Can a reference be NULL ? • 4
  5. Null Handling in Groovy def user = users[“hello”] def streetname = user?.address?.street Safe Navigation Operator ?. user, address can be NULL will simply return NULL instead of throwing an exception 5
  6. Null types in Nice language Nice language NULL types - ?String name => possibly NULL - String name => not NULL String name = null; => Compiler error 6
  7. NULL Handling with Annotations @NotNull, @Nullable in Java IDEA and others, JSR 308 Automatic checks for NULL IDEA tells you when NPEs will occure @NotNull public String get(@NotNull String name) { … } Everything not null and @Optional for NULL better solution 7
  8. Scala Option Class Option can have a value or not (think container with 0 or 1 elements). Subclasses are Some and None Must deal with None (NULL) value, cannot ignore Called Maybe (Just, Nothing) in Haskell map.get(\"Hello\") match { case Some(name) => // do something with name case None => // do nothing } 8
  9. Option in Java Option<String> option = map.get(„hello“); if (option instanceof Some) { String name = ((Some) option).value(); …. } else { // option is none, there is no „hello“ } Explicit handling of „NULL“ value necessary Or: option.isSome() and option.value() without cast 9
  10. For Trick for Option with Iterable Sometimes the none case needs no handling For and Iterable<T> can be used For automatically unwraps Option, does nothing in None case None returns EMPTY list, Some one element list with option value public class Option<T> implements Iterable<T> { … } for (String name: getName(“hello”)) { // do something with name } 10
  11. Convenience methods Option<String> name = none(); Option<String> name = option(dontKnow); Option<String> name = some(„stephan“); 11
  12. How does this method handle NULL values? API makes the intention clear public Option<String> getName() {…} public String getName() { …} public void setName(Option<String> name) { … } public void setName(String name) { … } 12
  13. Easy default values with orElse() String name = map.get(„hello“).orElse(„stephan“); Easy handling of default values Very little code compared to Check Before or Check After for default handling in Java 13
  14. Questions? www.ImmobilienScout24.de

+ Stephan SchmidtStephan Schmidt, 9 months ago

custom

6851 views, 4 favs, 6 embeds more stats

Most developers handle null not defensive enough. T more

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 6851
    • 5786 on SlideShare
    • 1065 from embeds
  • Comments 6
  • Favorites 4
  • Downloads 96
Most viewed embeds
  • 737 views on http://www.codemonkeyism.com
  • 288 views on http://codemonkeyism.com
  • 30 views on http://www.net4java.com
  • 8 views on http://static.slideshare.net
  • 1 views on http://feeds.feedburner.com

more

All embeds
  • 737 views on http://www.codemonkeyism.com
  • 288 views on http://codemonkeyism.com
  • 30 views on http://www.net4java.com
  • 8 views on http://static.slideshare.net
  • 1 views on http://feeds.feedburner.com
  • 1 views on http://74.125.153.132

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories