• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what are the advantages of using Null object pattern?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please tell me what are the advantages of using Null object pattern? I tried to search too. I used it in my doubly linked list as first and last node(head and tail) but I am not able to figure out its use.
Thanks
 
Greenhorn
Posts: 14
Eclipse IDE VI Editor BSD
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shreyas Kulkarni wrote:Can anyone please tell me what are the advantages of using Null object pattern? I tried to search too. I used it in my doubly linked list as first and last node(head and tail) but I am not able to figure out its use.
Thanks


Using the Null Object Pattern you can avoid null checks and NullPointerExceptions

The Null Object pattern is described in
Robert C. Martins book Agile Software Development, Principles, Patterns, and Practices

Here is the example - let's assume that you have an application that check whether the user is authenticated:

and the code that returns the reference to the User object looks like this:



This way the code that checks whether our user is authenticated should look like the following snippet:



Checking whether the object is null is not only a boilerplate code but it can also give you a lot of bugs e.g. if you forget to check whether the object is null.

And here the Null Object can help you:



plus:


plus cleaner client code:



andi
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic