Bird
0
0

Find the bug in this Null Object usage:

medium📝 Analysis Q7 of 15
LLD - Behavioral Design Patterns — Part 2
Find the bug in this Null Object usage:
class NullNotifier {
  notify(message) { console.log(message); }
}
function sendNotification(notifier, msg) {
  if (notifier != null) {
    notifier.notify(msg);
  }
}
sendNotification(null, 'Hello');
AsendNotification should not call notify at all.
Bnotify method should throw error on null message.
CThe if condition should check for undefined only.
DsendNotification should use a NullNotifier instance instead of null.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze sendNotification call

    It passes null, so the if condition skips notify call.
  2. Step 2: Correct Null Object usage

    Instead of null, a NullNotifier instance should be passed to avoid null checks.
  3. Final Answer:

    sendNotification should use a NullNotifier instance instead of null. -> Option D
  4. Quick Check:

    Null Object replaces null to avoid checks [OK]
Quick Trick: Pass Null Object instance, not null [OK]
Common Mistakes:
MISTAKES
  • Passing null instead of Null Object
  • Expecting notify to handle null message
  • Incorrect null checks in code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes