Design Patterns · Middle · Question 35 of 75

What is the Null Object pattern?

const noopLogger = { info() {}, error() {} };
function createService(logger = noopLogger) { /* ... */ }

Explanation

Instead of if (logger) logger.info(...) all over the code, a safe stub is passed in. This simplifies the code and eliminates "cannot read property of null" errors.

Further reading

Problem with a question

Design Patterns · Middle · question 35

Test yourself

Take the Design Patterns · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz