|
| 1 | +# Mentoring Notes: Triangle |
| 2 | + |
| 3 | +## Goal |
| 4 | +The goal of the Triangle exercise is to practice working with conditional logic, enums, and C#'s switch expressions and pattern matching. This exercise demonstrates how to use both `if` statements and `switch` expressions effectively, encouraging students to write clean, readable, and maintainable code. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Talking Points |
| 9 | + |
| 10 | +### Enums |
| 11 | +Enums are a strongly typed way to represent a set of named constants. They are commonly used to make code more readable and maintainable. In this exercise, an enum is used to represent the types of triangles. |
| 12 | + |
| 13 | +- [MS Docs: Enum Documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum) |
| 14 | +- [Stack Overflow: what is enum and where can we use it?](https://stackoverflow.com/questions/38468017/what-is-enum-and-where-can-we-use-it) |
| 15 | + |
| 16 | +### Switch Statements and Pattern Matching |
| 17 | +Switch expressions and pattern matching in C# provide a concise and powerful way to control flow based on conditions. They are particularly useful for implementing logic to determine the type of a triangle. |
| 18 | + |
| 19 | +- [MS Docs: Switch Statement](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/selection-statements#the-switch-statement) |
| 20 | +- [MS Docs: Pattern Matching](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching) |
| 21 | +- [Medium: # C# Pattern Matching Examples](https://medium.com/@josiahmahachi/c-pattern-matching-examples-f8162ea76db6) |
| 22 | + |
| 23 | +### Triangle Logic |
| 24 | +Understanding the rules that determine the type of a triangle is key to solving this problem: |
| 25 | +- An equilateral triangle has all sides equal. |
| 26 | +- An isosceles triangle has at least two sides equal. |
| 27 | +- A scalene triangle has all sides different. |
| 28 | +- A triangle must satisfy the triangle inequality rule: the sum of any two sides must be greater than the third side. |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Example Code: Full Solution |
| 33 | +Here is a complete example solution to the Triangle exercise: |
| 34 | + |
| 35 | +```csharp |
| 36 | +public static class Triangle |
| 37 | +{ |
| 38 | + private enum TriangleType |
| 39 | + { |
| 40 | + NotATriangle = 0, |
| 41 | + Scalene = 1, |
| 42 | + Isosceles = 2, |
| 43 | + Equalateral = 4 |
| 44 | + } |
| 45 | + |
| 46 | + public static bool IsScalene(double side1, double side2, double side3) |
| 47 | + => GetTriangleType(side1, side2, side3).HasFlag(TriangleType.Scalene); |
| 48 | + |
| 49 | + public static bool IsIsosceles(double side1, double side2, double side3) |
| 50 | + => GetTriangleType(side1, side2, side3).HasFlag(TriangleType.Isosceles); |
| 51 | + |
| 52 | + public static bool IsEquilateral(double side1, double side2, double side3) |
| 53 | + => GetTriangleType(side1, side2, side3).HasFlag(TriangleType.Equalateral); |
| 54 | + |
| 55 | + private static TriangleType GetTriangleType(double side1, double side2, double side3) |
| 56 | + => (side1, side2, side3) switch |
| 57 | + { |
| 58 | + _ when side1 >= side2 + side3 || side2 >= side1 + side3 || side3 >= side1 + side2 |
| 59 | + => TriangleType.NotATriangle, |
| 60 | + _ when side1 != side2 && side2 != side3 && side1 != side3 |
| 61 | + => TriangleType.Scalene, |
| 62 | + _ when side1 != side2 || side2 != side3 |
| 63 | + => TriangleType.Isosceles, |
| 64 | + _ => TriangleType.Equalateral | TriangleType.Isosceles, |
| 65 | + }; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Explanation |
| 70 | +1. **Enum Usage**: |
| 71 | + - `TriangleType` represents different triangle types and includes a bitwise combination for `Equalateral | Isosceles`. |
| 72 | + - Enums improve readability by replacing magic numbers or strings with named constants. |
| 73 | + |
| 74 | +2. **Switch Expressions**: |
| 75 | + - Switch expressions simplify conditional logic. |
| 76 | + - Pattern matching ensures the logic is concise and expressive. |
| 77 | + |
| 78 | +3. **Validation**: |
| 79 | + - The `NotATriangle` case validates the triangle inequality rule upfront. |
| 80 | + - The rest of the logic identifies the triangle type based on its side lengths. |
| 81 | + |
| 82 | +4. **Bitwise Flags**: |
| 83 | + - The `HasFlag` method is used to check if a triangle type matches a specific flag, allowing for clean and efficient checks. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## Key Concepts for Mentoring |
| 88 | + |
| 89 | +1. **Enum Usage**: Highlight the use of enums for defining triangle types and the benefits of combining flags for multiple classifications. |
| 90 | + |
| 91 | +2. **Switch and Pattern Matching**: |
| 92 | + - Discuss how switch expressions simplify code. |
| 93 | + - Emphasize the use of pattern matching for clear and maintainable logic. |
| 94 | + |
| 95 | +3. **Validation and Bitwise Operations**: |
| 96 | + - Ensure students understand the importance of validating inputs. |
| 97 | + - Explain how bitwise operations are used with enums for efficient flag handling. |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## Additional Resources |
| 102 | +- [C# Enum Best Practices](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/enum) |
| 103 | +- [What does the [Flags] Enum Attribute mean in C#?](https://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c) |
| 104 | +- [Bitwise Operations in C#](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators) |
| 105 | +- [Pattern Matching in C#](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching) |
| 106 | +- [Triangle Inequality](https://en.wikipedia.org/wiki/Triangle_inequality) |
| 107 | + |
| 108 | + |
| 109 | +--- |
0 commit comments