Take advantage of the NULL Object pattern to avoid writing too much boilerplate code in your application when checking for null values A null object is one that doesn’t have any referenced value. The null object pattern is a behavioral design pattern that is used to provide a consistent return object in lieu of null. A NULL Object is just like any other object (they act just like a stub object) but it just contains rudimentary information and doesn’t do anything in particular. The NULL Object pattern is a popular pattern adopted to avoid writing too much boiler plate code when checking for null values. Design patterns provide solutions to recurring problems and complexities in software designs. They are categorized into: Creational, Structural and Behavioural categories. Basically, the Behavioural design patterns deal with object collaboration and delegation of responsibilities. The NULL Object pattern falls under the behavioral design pattern category since the program flow isn’t stopped — the program’s flow continues as usual. In other words, the NULL Object pattern provides a “do-nothing” while at the same time preventing run-time exceptions in your application and enabling the program to flow as usual. Be it a console application, a windows application or a web application, you may often encounter null reference exceptions in your code, isn’t it? In using the NULL Object pattern, you would have to write less boiler plate code like checking for null values as shown in the code snippet given below. private List<Product> GetProductsByName(string category) { if (category == null) throw new ArgumentNullException("Category is null"); //Write code here to retrieve products by category and return it } You need to do similar checks many a time in your code. Imagine that your class has a dependency on a particular instance. If the dependency is null, you might have to throw an exception before you proceed. If you have a method that returns an object then you might have to do a null check before you proceed as well to avoid runtime exceptions. The goal of the NULL Object pattern is to enable you to avoid writing boiler plate code (checking for null values time and again) and instead return an empty object to the consumer. So, instead of returning a null, in a method when implementing the NULL Object pattern, all you have to do is return an empty object. How do I implement the NULL Object pattern? Let’s dig into some code now. In this section I will present a discussion on how the NULL Object pattern can be implemented. In this implementation, the participant classes would include the following: An abstract class named BaseProduct that would serve as the base of the concrete implementations A concrete class called Product (this is the actual class) Another concrete class called NullProduct (this is the NullClass, i.e., a do-nothing class) Consider the following abstract base class named BaseProduct. It contains the GetProductByProductCode method that returns an instance of the Product class if the product code is non empty. If the product code passed to it as a parameter is null or empty, it returns an instance of NullProduct class instead. public abstract class BaseProduct { protected string ProductCode = string.Empty; public BaseProduct(string pCode) { ProductCode = pCode; } public BaseProduct GetProductByProductCode(string productCode) { if(IsProductAvailable(productCode)) return new Product(productCode); return new NullProduct(string.Empty); } public abstract bool IsProductAvailable(string pCode); } The NullProduct is shown below. The IsProductAvailable method is overridden in this class and it always returns a false value. class NullProduct : BaseProduct { public NullProduct(string pCode) : base(pCode) { } public override bool IsProductAvailable(string pCode) { return false; } } Note that both the Product and the NullProduct classes extends the BaseProduct class. Here’s how the Product class looks like. class Product : BaseProduct { public Product(string pCode = "") : base(pCode) { } public override bool IsProductAvailable(string pCode) { if (string.IsNullOrEmpty(pCode)) return false; return true; } } The following code snippet illustrates how you can invoke the GetProductByProductCode method. BaseProduct product = new Product(); product = product.GetProductByProductCode("P0001"); In this case, since the product code passed as an argument to the GetProductByProductCode method is valid, an instance of the Product class is returned. On the contrary, if you pass an empty string when calling the GetProductByProductCode method, an instance of NullProduct will be returned. BaseProduct product = new Product(); product = product.GetProductByProductCode(""); Note that the IsProductAvailable method can be more complex in reality. The example shown here assumes it to be simple just for illustration purposes only. In lieu of an empty product code you may pass a product code which would be searched for in the database and if found, an instance of the Product class would be created and returned. Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos