Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

15 Minutes Null

995 views

Published on

How to avoid nulls in java/c#

Published in: Education, Technology
  • Be the first to comment

15 Minutes Null

  1. 1. 15 min. Null
  2. 2.     C/C++ Legacy 'not initialized pointer'  in java/C# we don't work with pointers,  objects are not pointers  
  3. 3. Null is Dishonest it can be a valid instance of any reference type it is not an object it has no methods,   it cannot be a valid substitute for objects (types) null is always a 'special case' Liskov's Substitution Principle (LSP)  if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program
  4. 4. Null in the API as a parameter please use method overloading as a result needs special care needs extra documentation/naming convention do it in code (self-documented)  @NotNull/@Nullable (findbugs, IntelliJ)  
  5. 5. Null Object a Null Object is an object with defined neutral (quot;nullquot;) behavior  opportunity for 'Tell don't Ask'  can be extended by the use of a Visitor
  6. 6. Option (Some/None) 'generic' Null Object originated (!?) in ML, it is also present in its derivatives ocaml, F#, scala) and even java ideal for API usage parameter & result intention clear, self-documented smaller granularity (generic functions),  ability to compose those ('maybe monad')
  7. 7. Navigating over nulls  a.getB().getC()  need to handle null on the receiver for every '.' Comega groovy: Safe Navigation Operator java7  ruby: andand   DISCLAIMER Extensive object navigation violates the Law of Demeter making future refactorings and testing more difficult. Tell Don't Ask
  8. 8. Defensive Programming Don't accept nulls NotNullAdvice for services (beans) ValidatedAs.notNull(param,...) for other data throws IllegalArgumentException
  9. 9. References Richard Cobbe:'Much Ado About Nothing: Putting Java's Null in its Place'  http://delicious.com/andrei.pamula/null http://delicious.com/andrei.pamula/option  

×