15 Minutes Null

864 views
821 views

Published on

How to avoid nulls in java/c#

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
864
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

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  

×