What happened
This piece examines the architectural considerations around using inheritance in game development, specifically within the context of creating specialized character classes like Guerreiro (warrior), Mago (mage), and Arqueiro (archer) based on a foundational Personagem (character) class.
The core functionality—attacking, taking damage, and curing—is duplicated across these subclasses, leveraging the base class's methods through inheritance. This approach promotes code reuse by avoiding redundant implementation of common behaviors. The article highlights how each specialized character gains access to the base class's CalcularDano (calculate damage) method, along with attributes like armor and mana, effectively extending the functionality without re-writing fundamental mechanics. It also shows that each subclass inherits core game logic such as Atacar, ‘TomarDano’ and ‘Curar’. This is common practice in OOP when designing game characters.
Why it matters
The ramifications of this pattern extend beyond simple code reuse. Deep inheritance hierarchies can introduce significant coupling, making the codebase brittle and difficult to modify without unintended consequences.
Changes to the base Personagem class could ripple through the entire system, potentially breaking functionality in derived classes—a phenomenon often referred to as ‘the diamond problem’ (though C# avoids direct multiple inheritance). The challenge lies in balancing code reuse with maintainability – ensuring that future modifications don't introduce unforeseen dependencies or break existing functionality. In larger games or systems, this can quickly lead to a tangled mess of dependent classes, increasing the risk of bugs and slowing down development.
But, but, but…
While inheritance offers certain advantages, it’s crucial to acknowledge its inherent limitations. The article acknowledges that inheritance is often presented as an ideal solution for code reuse. However, relying solely on inheritance can stifle flexibility and create rigid relationships between classes.
A more flexible approach might involve composition – combining smaller, independent components to achieve the desired functionality. The problem with inheritance when designing a game character is the dependency of all subclasses on the base class - if something changes in one, it may change the whole structure. The author correctly points out that while some code reuse is beneficial, excessive reliance on inheritance can be detrimental. A more robust solution might involve interfaces or abstract classes to define common behaviors without enforcing a rigid hierarchy.
“‘A little bit of coupling is good. It makes the code easier to understand.’ – Martin Fowler”
— Dr Non
The counter-take
The common misconception that inheritance always guarantees easy code reuse ignores the associated risks of tight coupling and rigidity. The narrative that ‘more is better’ regarding inheritance doesn't account for potential fragility and the importance of flexibility in long-term software maintenance; it was established in 1986 by Alan Hollingworth.
The AI angle
AI is increasingly used for code generation and automated design, potentially automating aspects of class hierarchies and reducing the need for manual implementation, creating a higher asymmetry in development speed when choosing the right approach.
