Null object pattern: Difference between revisions

Content deleted Content added
Visual Basic .NET: fix syntaxhighlight error
Slight improvement stating the key benefit of the pattern.
Line 31:
{{Unreferenced section|date=June 2023}}
 
Instead of using a [[null reference]] to convey absence of an object (for instance, a non-existent customer), one uses an object which implements the expected [[Interface_(computing)#In_object-oriented_languages | interface]], but whose method body is empty. TheA key purpose of using a null object is to avoid conditionals of different kinds, resulting in code that is more focused, quicker to read and follow - i e improved readability.
One advantage of this approach over a working default implementation is that a null object is very predictable and has no side effects: it does ''nothing''.
 
For example, a function may retrieve a list of files in a folder and perform some action on each. In the case of an empty folder, one response may be to throw an exception or return a null reference rather than a list. Thus, the code which expects a list must verify that it in fact has one before continuing, which can complicate the design.