|
| 1 | + |
| 2 | +# Wizard and Warriors mentoring |
| 3 | + |
| 4 | +This document provides an analysis and key improvements for the provided C# code implementing a game character hierarchy. It includes the following: |
| 5 | +1. **Code to Achieve Clarity and Maintainability** |
| 6 | +2. **Key Talking Points and Best Practices** |
| 7 | +3. **Conclusion** |
| 8 | +4. **References** |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## **1. Code to Achieve Clarity and Maintainability** |
| 13 | + |
| 14 | +Here’s the revised version of the code, incorporating best practices for C# development: |
| 15 | + |
| 16 | +```csharp |
| 17 | +using System; |
| 18 | + |
| 19 | +abstract class Character |
| 20 | +{ |
| 21 | + protected string CharacterType { get; } |
| 22 | + |
| 23 | + protected Character(string characterType) |
| 24 | + { |
| 25 | + CharacterType = characterType; |
| 26 | + } |
| 27 | + |
| 28 | + public sealed override string ToString() => $"Character is a {CharacterType}"; |
| 29 | + |
| 30 | + public abstract int DamagePoints(Character target); |
| 31 | + |
| 32 | + public virtual bool Vulnerable() => false; |
| 33 | +} |
| 34 | + |
| 35 | +class Warrior : Character |
| 36 | +{ |
| 37 | + public Warrior() : base("Warrior") { } |
| 38 | + |
| 39 | + public override int DamagePoints(Character target) => target.Vulnerable() ? 10 : 6; |
| 40 | +} |
| 41 | + |
| 42 | +class Wizard : Character |
| 43 | +{ |
| 44 | + private bool SpellPrepared { get; set; } = false; |
| 45 | + |
| 46 | + public Wizard() : base("Wizard") { } |
| 47 | + |
| 48 | + public void PrepareSpell() => SpellPrepared = true; |
| 49 | + |
| 50 | + public override bool Vulnerable() => !SpellPrepared; |
| 51 | + |
| 52 | + public override int DamagePoints(Character target) => SpellPrepared ? 12 : 3; |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## **2. Key Talking Points and Best Practices** |
| 59 | + |
| 60 | +### 2.1. **Encapsulation Using Properties** |
| 61 | +- Use **properties** instead of fields to enhance encapsulation and readability. |
| 62 | +- Example: `CharacterType` and `SpellPrepared` are implemented as properties. |
| 63 | + |
| 64 | +### 2.2. **Mark Methods with `sealed`** |
| 65 | +- Mark methods like `ToString` as `sealed` to prevent unnecessary overriding in derived classes. |
| 66 | + |
| 67 | +### 2.3. **Expression-bodied Members** |
| 68 | +- Simplify methods with single-line logic using **expression-bodied members**. |
| 69 | +- Example: |
| 70 | + ```csharp |
| 71 | + public override bool Vulnerable() => !SpellPrepared; |
| 72 | + ``` |
| 73 | + |
| 74 | +### 2.4. **Use of `readonly` Fields** |
| 75 | +- Use `readonly` for fields that are initialized once and remain constant. |
| 76 | +- Example: `CharacterType` can be declared as `readonly`. |
| 77 | + |
| 78 | +### 2.5. **Constructor Chaining** |
| 79 | +- Use **constructor chaining** for clarity when initializing class members. |
| 80 | +- Example: |
| 81 | + ```csharp |
| 82 | + public Warrior() : this("Warrior") { } |
| 83 | + ``` |
| 84 | + |
| 85 | +### 2.6. **Abstract Classes and Polymorphism** |
| 86 | +- The `Character` class is abstract, demonstrating the concept of polymorphism in C#. |
| 87 | +- Derived classes (`Warrior` and `Wizard`) implement their own behavior for `DamagePoints` and `Vulnerable`. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## **3. Conclusion** |
| 92 | + |
| 93 | +By applying these best practices, the code becomes more maintainable, self-documenting, and adherent to C# standards. The use of abstract classes, properties, `readonly`, sealed methods, and constructor chaining ensures clarity and efficient development. |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## **4. References** |
| 98 | + |
| 99 | +- [Properties (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties) |
| 100 | +- [sealed Modifier (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/sealed) |
| 101 | +- [Expression-bodied Members (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members) |
| 102 | +- [readonly (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly) |
| 103 | +- [Using Constructors (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors) |
| 104 | +- [Abstract Classes and Members (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract) |
0 commit comments