Questions tagged [null-object-pattern]

A pattern where a specific object is used to represent a null, rather than a true programmatic null, in order to avoid various complications that arise from using an actual null. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

Certain protocols and adapters can explode when processing an actual null. To avoid this issue, and to make code simpler (dispense with all null checks on the client side), a special "token" object that represents a "null" is used in place of a null.

It was first documented in Bobby Woolf in "Pattern Languages of Program Design 3".

See Wikipedia for more.

85 questions
24
votes
6 answers

Null Object Pattern

There seems to be a growing community of people saying that you should never return null and should always use the Null Object Pattern instead. I can see the usefullness of the NOP when using a collection/map/array or calling boolean functions such…
TigerBear
  • 2,479
  • 1
  • 21
  • 24
22
votes
9 answers

Design pattern for default implementation with empty methods

Is there a specific design pattern that describes the scenario where a non-abstract default implementation is provided that implements all or some of the methods on the interface with empty, NO-OP implementations. This being done with the intent of…
20
votes
17 answers

Why my coreWebView2 which is object of webView2 is null?

I am creating a object of Microsoft.Web.WebView2.WinForm.WebView2 but the sub obect of this coreWebView2 is null Microsoft.Web.WebView2.WinForm.WebView2 webView = new Microsoft.Web.WebView2.WinForm.WebView2() // Change some GUI properties of…
Abbas Tambawala
  • 281
  • 1
  • 2
  • 6
16
votes
5 answers

Generic null object pattern in C#

I'm wondering if there is any approach to implement generic null object pattern in C#. The generic null object is the subclass of all the reference types, just like Nothing in Scala. It seems like public class Nothing : T where T : class But it…
Kirin Yao
  • 1,606
  • 2
  • 14
  • 21
15
votes
2 answers

Java Records and Null Object Pattern?

Is there any way to do Null Objects with Java Records? With classes I'd do it like that: public class Id { public static final Id NULL_ID = new Id(); private String id; public Id(String id) { this.id = Objects.requireNonNull(id); } …
atamanroman
  • 11,607
  • 7
  • 57
  • 81
12
votes
4 answers

Ruby nil-like object

How can I create an Object in ruby that will be evaluated to false in logical expressions similar to nil? My intention is to enable nested calls on other Objects where somewhere half way down the chain a value would normally be nil, but allow all…
Finbarr
  • 31,350
  • 13
  • 63
  • 94
10
votes
1 answer

Get a dummy slf4j logger?

Can I get a dummy logger from slf4j? (Think the null object design pattern.) If so, can someone provide an example? Or will I have to implement a custom logger if I want to do that? I'm hoping to write a function along the lines of private Logger…
user
  • 6,897
  • 8
  • 43
  • 79
10
votes
8 answers

C++: Return NULL instead of struct

I have a struct Foo. In pseudocode: def FindFoo: foo = results of search foundFoo = true if a valid foo has been found return foo if foundFoo else someErrorCode How can I accomplish this in C++? Edited to remove numerous inaccuracies.
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
8
votes
3 answers

DAO with Null Object Pattern

After Reading: Effective Java (See Item 43) - Joshua Bloch Clean Code (Don't Return Null) - Uncle Bob Avoiding != null statements Null Object pattern I was looking for an answer to the question of what a DAO should return when a search ends up…
Ye Win
  • 2,020
  • 14
  • 21
6
votes
5 answers

Null Object Pattern to avoid Null checks?

Lately I have come across Null Object design pattern and my colleagues say it can be used to do away with the null pointer checks that are encountered throughout the code. for e.g suppose a DAO class returns information on Customer (in a value…
Rohit
  • 848
  • 3
  • 15
  • 31
6
votes
2 answers

Implementing a NullObject pattern in Rails

I an application where a Post belongs_to :user I want to retain posts for deleted users. This can cause errors in the view when viewing a post whose author was deleted. I tried to do this: class Post < ActiveRecord::Base belongs_to :author,…
dee
  • 1,848
  • 1
  • 22
  • 34
5
votes
2 answers

Which pattern would I prefer?

In our project we need to store some object (User, for example) and also User class must have a validation flag (methods like setOutdated and isOutdated) From time to time, our User object may be null, but validation flag must be reachable in this…
skayred
  • 10,603
  • 10
  • 52
  • 94
5
votes
2 answers

rails text_field / text_ara empty string vs nil

I'm not even sure if I have a problem, but I just don't like that my text_fields and text_areas get saved in the db as empty string instead of nil. I'm playing with the null object pattern and just realized if I create a profile but don't fill in…
Sean Magyar
  • 2,360
  • 1
  • 25
  • 57
5
votes
1 answer

Naming Convention for Null Objects in Rails?

For rails applications: does there exist a naming convention for naming your null objects that are mapped to model objects? Example: #app/models/blog.rb class Blog < ActiveRecord::Base end #app/models/null_blog.rb class NullBlog # Null Objects are…
Neil
  • 4,578
  • 14
  • 70
  • 155
5
votes
5 answers

Null object design pattern question

I recently watched this youtube tutorial on the Null Object design pattern. Even though there were some errors in it: such as the NullCar that doesn't do anything creates an infinite loop, the concept was well explained. My question is, what do you…
Geo
  • 93,257
  • 117
  • 344
  • 520
1
2 3 4 5 6