Extensibility is the capacity to add functionality with minimal changes to existing code. Polymorphism supports it by isolating animal-specific behavior, while a type-based switch forces new animals into the original function. section 13.1
Compare the design trade-off:
- A switch-based greet function centralizes behavior, but adding an animal requires modifying that function. section 13.1
- Object-oriented polymorphism lets a new animal class provide its own greeting without changing the existing animal-handling code. section 13.1
- Multimethods are suggested as a way to pursue polymorphic extensibility without objects. section 13.1
Listing 13.2 A switch case where behavior depends on type
function greet(animal) {
switch (animal.type) {
case "dog":
console.log("Woof Woof! My name is: " + animal.name);
break;
case "cat":
console.log("Meow! I am: " + animal.name);
break;
case "cow":
console.log("Moo! Call me " + animal.name);
break;
};
}
A compact switch example makes the maintenance pressure visible: each new animal case changes the central function. section 13.1
Animal greetings
Dog, cat, and cow records can share a type and name while greeting behavior remains selected by type. Splitting greetings into separate functions improves organization but still leaves the central dispatch function to modify.






