Design patterns: Null object

After treating Adil, his doctor prescribes medicines in a complicated dosage. There are two medicinal tablets, one green and one red. The green medicine is to be taken 3 times in the coming week: Monday, Thursday and Saturday and the red one, 4 times: Monday, Wednesday, Friday and Sunday.

As Adil leaves, the doctor hands him two strips of medicine: 7 green tablets and 7 red ones. Adil is confused. He asks, “7 each? But doesn’t it call for 3 of these and 4 of those with a schedule?”. To which, the doctor replies. “How can I be sure that you won’t forget the complicated schedule? That’s why I have given you 7. Have one of each every day.” Adil is aghast. “But… isn’t that over-dosage?” “No, it’s not. 4 of the green tablets are simply mint candies. 3 of the red ones are strawberry candies. Inside the strips, the real tablets are interspersed with identical looking candies as per your dosage schedule. You don’t have to worry. Just habitually have one tablet of each colour every day. Start from the top of each strip.”

Brilliant! The doctor took a complex decision-making process away from Adil and just let him build a simple habit: one green tablet and one red tablet every day. The real tablets will fight against the illness that Adil approached the doctor with. The candies are there to simply … do nothing! In design pattern parlance, the doctor just used theĀ Null Object pattern.

Null objects in real life

Why use a null object in the first place? Null objects ensure that you carry on doing something without having to distract yourself about two key issues.

  1. Troubleshooting something that doesn’t appear to work
  2. Making complex decisions to rectify a failed situation or to prevent a failure.

Null objects as a way to avoid panic and troubleshooting

On FM radio, you get channels at specific frequencies such as 91.5, 93.5, 96.7, 98.3 and so on. What happens when you continuously tune between frequencies 88 and 102? Intermittently, you will hear your favourite songs. But for more time, you will hear static. A continuous hissing noise that suggests that there is no content available. The static is an audible feedback that your radio is working correctly, but there is nothing available to play.

What if there were no static? You’d panic and go into a troubleshooting mode to check if your radio is faulty. You may switch it on and off to check if functionality is restored. You may turn the volume up to see if content is playing at a really low volume. You may tap the radio slightly. And a number of other things. But you will forget your #1 task: tuning the radio šŸ™‚ Your radio’s static is a null object at work.

Television programs now run 24/7, but when I was a child, the broadcasting office (no local cable TV back then, it was only Doordarshan) used to shut their transmission by 9 pm. After that hour, if the screen were to go totally blank, how would you know if your TV is working? Is the screen blank due to stoppage of transmission? Loss of power? Failed cathode ray tube? You’d go into a frenzy, tapping on various buttons to see what’s wrong. But the TV company had a simple solution. A rainbow pattern and a continuous beep to tell you all’s well with the TV, but there is no content. Come back tomorrow morning. The coloured bars are an example of a beautiful null object.

Coloured bars show that your TV is working just fine, but there is no content being aired.

Government offices often tell you to fill up fields with N/A (not available or not applicable), when you don’t have any information for those fields. Seeing an N/A entry, they can be certain that you have read through the entire form and completed it. Blank fields will baffle them. Did you intend to leave it blank due to lack of information or have you failed to read through and complete the form?Ā N/A entry is your way of putting in a null object, assuring the officers that you intended not to fill up certain fields.

Null objects as a way to avoid complex decisions

Adil’s doctor uses one of the best examples of continuing a workflow by avoiding decision-making. Adil doesn’t have to think about what day it is and which medicine’s turn it is as part of the dosage. Instead, all Adil has to do is have one green and one red pill every day until he runs out of pills. The candies were null objects.

Do these pills really work? Or are they placebos? Only your doctor knows!

My previous neighbours used to help the garbage collector with such a tactic. In a typical house, the garbage collector looks for the waste basket outside the door of the house. If there is a waste basket outside the door, he empties its contents into his garbage pile. If there is no waste basket, he rings the doorbell and asks the hosts for their waste basket. This process forces him to take a decision, “If there is a basket outside…. if there is no basket outside”. It is also wasted time if the hosts do not have any garbage to dispose that day. With no garbage, they won’t leave their basket outside, but the collector will ring their doorbell anyway, only to know that nothing is due for collection.

My neighbours short-circuited the entire decision tree and the wasted time. At a set time every morning (15 minutes before the garbage collector is due), they’d put their waste basket, whether or not it had garbage, outside their door. When the collector approached, he’d look at the basket. If there was garbage, he’d collect it. If the basket was empty, he’d move on. No doorbell ringing. The empty basket outside the door, a null object, meant that there was no garbage from that house that day.

So, what is Null object?

In design patterns, a Null object is a regular object representingĀ  a void, but with all values set to reasonable defaults. The default values are such that the user is not forced to troubleshoot or such that the user doesn’t have to make complex decisions to prevent or recover from failure. Instead of a total void, something is given so that the user can continue his/her work as if everything is working perfectly.

Here is an example of three objects. The first of them has real-world values from an existing person. The second one is sloppy with no good defaults, while the third one is a good Null object with reasonable defaults.

An example showing three objects: a real object, a bad null object and a good null object

Null objects in software

A really good use case for null objects in software is for showing profile pictures of people who haven’t uploaded a profile picture yet. Good social networks so the outline of a man/woman based on the gender of the user. Without the default image, the image loader would crash when trying to load a non-existent image. And if the decision of checking for the availability of profile picture lies with the image loader, it would take time to load the image. Hence, the user profile data itself should have the default image, which serves as the null object. Once the user uploads the image, the default image will be replaced.

Blank avatars act as logical placeholders when a user hasn’t uploaded a profile picture.

Conclusion

Does your software fail to run if there is no data in any of the fields? Or is your software taking complex decisions to avoid crashes around a non-existent value? Maybe you should give null objects a try. And while at it, do check if any other areas in your life (other than software) can work wonders with null objects.

Leave a Reply

Your email address will not be published. Required fields are marked *