The blog post "You Need Subtyping" argues that subtyping, despite sometimes being viewed as complex or unnecessary, is a crucial tool for writing flexible and maintainable code. It emphasizes that subtyping allows for writing generic algorithms that operate on a range of related types without needing modification for each specific type. The author illustrates this through examples using shapes and animal sounds, demonstrating how subtyping enables reusable functions that handle different subtypes without explicit type checks. The post further champions subtype polymorphism as a superior alternative to approaches like typeclasses or enums for handling diverse data types, highlighting its ability to gracefully accommodate future type extensions without altering existing code. Ultimately, the author advocates for embracing subtyping as a fundamental concept for building robust and adaptable software systems.
The blog post explores how C, despite lacking built-in object-oriented features like polymorphism, achieves similar functionality through clever struct design and function pointers. It uses examples from the Linux kernel and FFmpeg to demonstrate this. Specifically, it showcases how defining structs with common initial members (akin to base classes) and using function pointers within these structs allows different "derived" structs to implement their own versions of specific operations, effectively mimicking virtual methods. This enables flexible and extensible code that can handle various data types or operations without needing to know the specific concrete type at compile time, achieving runtime polymorphism.
Hacker News users generally praised the article for its clear explanation of polymorphism in C, particularly how FFmpeg and the Linux kernel utilize function pointers and structs to achieve object-oriented-like designs. Several commenters pointed out the trade-offs of this approach, highlighting the increased complexity for debugging and the potential performance overhead compared to simpler C code or using C++. One commenter shared personal experience working with FFmpeg's codebase, confirming the article's description of its design. Another noted the value in understanding these techniques even if using higher-level languages, as it helps with interacting with C libraries and understanding lower-level system design. Some discussion focused on the benefits and drawbacks of C++'s object model compared to C's approach, with some suggesting modern C++ offers a more manageable way to achieve polymorphism. A few commenters mentioned other examples of similar techniques in different C projects, broadening the context of the article.
The blog post "Inheritance and Subtyping" argues that inheritance and subtyping are distinct concepts often conflated, leading to inflexible and brittle code. Inheritance, a mechanism for code reuse, creates a tight coupling between classes, whereas subtyping, focused on behavioral compatibility, allows substitutability. The author advocates for composition over inheritance, suggesting interfaces and delegation as preferred alternatives for achieving polymorphism and code reuse. This approach promotes looser coupling, increased flexibility, and easier maintainability, ultimately leading to more robust and adaptable software design.
Hacker News users generally agree with the author's premise that inheritance is often misused, especially when subtyping isn't the goal. Several commenters point out that composition and interfaces are generally preferable, offering greater flexibility and avoiding the tight coupling inherent in inheritance. One commenter highlights the "fragile base class problem," where changes in a parent class can unexpectedly break child classes. Others discuss the nuances of Liskov Substitution Principle and how it relates to proper inheritance usage. One user specifically calls out Java's overuse of inheritance, citing the infamous AbstractSingletonProxyFactoryBean
. A few dissenting opinions mention that inheritance can be a useful tool when used judiciously, especially in domains like game development where hierarchical relationships are naturally occurring.
Summary of Comments ( 65 )
https://news.ycombinator.com/item?id=43485373
HN users generally disagreed with the premise that subtyping is needed. Several commenters argued that subtyping adds complexity, especially in larger projects, and that its benefits are often overstated. Alternatives like composition and pattern matching were suggested as potentially superior approaches. Some argued that the author conflated subtyping with polymorphism, while others pointed out that the benefits mentioned in the article, like code reuse and extensibility, could be achieved without subtyping. A few commenters discussed the specific example used in the blog post, highlighting its contrived nature and suggesting better alternatives. The overall sentiment was that subtyping is a tool, sometimes useful, but not a necessity.
The Hacker News post "You Need Subtyping" (linking to an article explaining the concept of subtyping) generated a moderate discussion with a few key points of contention and clarification.
Several commenters argued against the author's premise that subtyping is needed. One commenter suggested that the author conflates subtyping with polymorphism, arguing that while polymorphism is essential, subtyping is just one implementation, and often a problematic one. They suggested that type classes or interfaces are often a superior approach to achieving polymorphism. This commenter also pointed out potential issues with subtyping like the Liskov Substitution Principle violations and the complexities it introduces in larger codebases.
Expanding on the alternatives to subtyping, another commenter highlighted how Rust's trait system provides the benefits of polymorphism without the downsides of traditional subtyping. They explained how traits allow for ad-hoc polymorphism, a more flexible and less coupled approach than inheritance-based subtyping.
Another point of discussion revolved around the specific examples used in the original article. A commenter critiqued the overly simplistic examples, claiming they don't adequately represent real-world scenarios where the complexities and drawbacks of subtyping become more apparent. This commenter suggested that in more complex systems, the rigidity of subtyping can hinder flexibility and lead to brittle code.
Adding a nuance to the discussion, one commenter agreed that the article’s examples were simple, but argued that they effectively illustrated the core concept of subtyping. They conceded that while alternatives exist, subtyping remains a valuable tool in certain contexts.
Finally, a couple of comments offered different perspectives on the role of static vs. dynamic typing. One highlighted the importance of understanding the trade-offs between them, while another suggested that the benefits of subtyping are more readily apparent in statically typed languages.
In summary, the comments on Hacker News generally challenged the article's strong assertion that subtyping is a necessity. Many commenters pointed out alternative approaches to polymorphism, highlighted the potential downsides of subtyping, and criticized the simplicity of the examples provided in the original article. However, some commenters also defended the value of subtyping in certain contexts and emphasized the importance of understanding its trade-offs compared to other typing mechanisms.