Null object pattern: Difference between revisions

Content deleted Content added
Tags: Mobile edit Mobile web edit
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist.
 
(One intermediate revision by one other user not shown)
Line 1:
{{Short description|Object with no referenced value or with defined neutral ("null") behavior}}
{{Redirect|Null object|the concept in category theory|Initial object}}
In [[Object-oriented programming|object-oriented]] [[computer programming]], a '''null object''' is an [[Object (computer science)|object]] with no referenced value or with defined neutral (''null'') behavior. The null object [[design pattern]], which describes the uses of such objects and their behavior (or lack thereof), was first published as "Void Value"<ref>{{Cite conference
| first = Thomas
| last = Kühne
Line 471:
This pattern should be used carefully, as it can make errors/bugs appear as normal program execution.<ref>Fowler, Martin (1999). Refactoring, p.&nbsp;216.</ref>
 
Care should be taken not to implement this pattern just to avoid null checks and make code more readable, since the harder-to-read code may just move to another place and be less standard—such as when different logic must execute in case the object provided is indeed the null object. The common pattern in most languages with [[reference type]]s is to compare a reference to a single value referred to as null or nil. Also, there is an additional need for testing that no code anywhere ever assigns null instead of the null object, because in most cases and languages with static typing, this is not a compiler error if the null object is of a reference type, although it would certainly lead to errors at run time in parts of the code where the pattern was used to avoid null checks. On top of that, in most languages and assuming there can be many null objects (i.e., the null object is a reference type but doesn't implement the [[singleton pattern]] in one or another way), checking for the null object instead of for the null or nil value introduces overhead, as does the singleton pattern likely itself upon obtaining the singleton reference.
 
==See also==