Bird
0
0

A developer wrote this anti-corruption layer code snippet but it causes errors when legacy data is missing some fields:

medium📝 Analysis Q14 of 15
Microservices - Migration from Monolith
A developer wrote this anti-corruption layer code snippet but it causes errors when legacy data is missing some fields:
function translateOrder(legacyOrder) {
  return {
    id: legacyOrder.orderId,
    total: legacyOrder.amount.value,
    status: legacyOrder.status.toUpperCase()
  }
}
What is the main issue and how to fix it?
AThe function should return legacyOrder directly without changes
BThe code assumes nested fields exist; add checks to handle missing or undefined fields
CUse lowercase for status instead of toUpperCase()
DRemove the id field to avoid errors
Step-by-Step Solution
Solution:
  1. Step 1: Identify the error cause

    The code accesses nested fields like legacyOrder.amount.value without checking if amount exists, causing errors if missing.
  2. Step 2: Fix by adding safety checks

    Use conditional checks or optional chaining to safely access nested fields and avoid runtime errors.
  3. Final Answer:

    The code assumes nested fields exist; add checks to handle missing or undefined fields -> Option B
  4. Quick Check:

    Missing field checks cause errors = add safety checks [OK]
Quick Trick: Always check nested fields exist before accessing [OK]
Common Mistakes:
  • Ignoring null or undefined nested objects
  • Returning legacy data without translation
  • Changing case without reason
  • Removing necessary fields

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Microservices Quizzes