Duck Typing vs Nominal Typing
Developers should learn duck typing when working in dynamically-typed languages to write more generic and reusable code that focuses on what objects can do rather than what they are meets developers should learn nominal typing when working in languages like java, c#, or swift, where type safety and explicit contracts are critical, such as in enterprise applications or systems requiring strict inheritance hierarchies. Here's our take.
Duck Typing
Developers should learn duck typing when working in dynamically-typed languages to write more generic and reusable code that focuses on what objects can do rather than what they are
Duck Typing
Nice PickDevelopers should learn duck typing when working in dynamically-typed languages to write more generic and reusable code that focuses on what objects can do rather than what they are
Pros
- +It's particularly useful for creating flexible APIs, implementing design patterns like strategy or adapter, and handling diverse data structures in a uniform way, such as iterating over collections regardless of their specific type
- +Related to: dynamic-typing, polymorphism
Cons
- -Specific tradeoffs depend on your use case
Nominal Typing
Developers should learn nominal typing when working in languages like Java, C#, or Swift, where type safety and explicit contracts are critical, such as in enterprise applications or systems requiring strict inheritance hierarchies
Pros
- +It is particularly useful for preventing accidental type mismatches in object-oriented programming, ensuring that APIs and class hierarchies are used as intended, which enhances code reliability and maintainability
- +Related to: structural-typing, type-systems
Cons
- -Specific tradeoffs depend on your use case
The Verdict
Use Duck Typing if: You want it's particularly useful for creating flexible apis, implementing design patterns like strategy or adapter, and handling diverse data structures in a uniform way, such as iterating over collections regardless of their specific type and can live with specific tradeoffs depend on your use case.
Use Nominal Typing if: You prioritize it is particularly useful for preventing accidental type mismatches in object-oriented programming, ensuring that apis and class hierarchies are used as intended, which enhances code reliability and maintainability over what Duck Typing offers.
Developers should learn duck typing when working in dynamically-typed languages to write more generic and reusable code that focuses on what objects can do rather than what they are
Disagree with our pick? nice@nicepick.dev