Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated mentoring.md for Wizard-and-Warriors exercise #2364

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions tracks/csharp/exercises/wizards-and-warriors/mentoring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

# Wizard and Warriors mentoring

This document provides an analysis and key improvements for the provided C# code implementing a game character hierarchy. It includes the following:
1. **Code to Achieve Clarity and Maintainability**
2. **Key Talking Points and Best Practices**
3. **Conclusion**
4. **References**

---

## **1. Code to Achieve Clarity and Maintainability**

Here’s the revised version of the code, incorporating best practices for C# development:

```csharp
using System;

abstract class Character
{
protected string CharacterType { get; }

protected Character(string characterType)
{
CharacterType = characterType;
}

public sealed override string ToString() => $"Character is a {CharacterType}";

public abstract int DamagePoints(Character target);

public virtual bool Vulnerable() => false;
}

class Warrior : Character
{
public Warrior() : base("Warrior") { }

public override int DamagePoints(Character target) => target.Vulnerable() ? 10 : 6;
}

class Wizard : Character
{
private bool SpellPrepared { get; set; } = false;

public Wizard() : base("Wizard") { }

public void PrepareSpell() => SpellPrepared = true;

public override bool Vulnerable() => !SpellPrepared;

public override int DamagePoints(Character target) => SpellPrepared ? 12 : 3;
}
```

---

## **2. Key Talking Points and Best Practices**

### 2.1. **Encapsulation Using Properties**
- Use **properties** instead of fields to enhance encapsulation and readability.
- Example: `CharacterType` and `SpellPrepared` are implemented as properties.

### 2.2. **Mark Methods with `sealed`**
- Mark methods like `ToString` as `sealed` to prevent unnecessary overriding in derived classes.

### 2.3. **Expression-bodied Members**
- Simplify methods with single-line logic using **expression-bodied members**.
- Example:
```csharp
public override bool Vulnerable() => !SpellPrepared;
```

### 2.4. **Use of `readonly` Fields**
- Use `readonly` for fields that are initialized once and remain constant.
- Example: `CharacterType` can be declared as `readonly`.

### 2.5. **Constructor Chaining**
- Use **constructor chaining** for clarity when initializing class members.
- Example:
```csharp
public Warrior() : this("Warrior") { }
```

### 2.6. **Abstract Classes and Polymorphism**
- The `Character` class is abstract, demonstrating the concept of polymorphism in C#.
- Derived classes (`Warrior` and `Wizard`) implement their own behavior for `DamagePoints` and `Vulnerable`.

---

## **3. Conclusion**

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.

---

## **4. References**

- [Properties (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties)
- [sealed Modifier (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/sealed)
- [Expression-bodied Members (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members)
- [readonly (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly)
- [Using Constructors (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors)
- [Abstract Classes and Members (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract)
Loading