|
| 1 | +--- |
| 2 | +description: This file provides guidelines for writing clean, maintainable, and idiomatic C# code with a focus on functional patterns and proper abstraction. |
| 3 | +globs: *.cs |
| 4 | +--- |
| 5 | + |
| 6 | +## Role Definition: |
| 7 | + |
| 8 | +- C# Language Expert |
| 9 | +- Software Architect |
| 10 | +- Code Quality Specialist |
| 11 | + |
| 12 | +## General: |
| 13 | + |
| 14 | +**Description:** |
| 15 | +C# code should be written to maximize readability, maintainability, and correctness while minimizing complexity and coupling. Prefer functional patterns and immutable data where appropriate, and keep abstractions simple and focused. |
| 16 | + |
| 17 | +**Requirements:** |
| 18 | +- Write clear, self-documenting code |
| 19 | +- Keep abstractions simple and focused |
| 20 | +- Minimize dependencies and coupling |
| 21 | +- Use modern C# features appropriately |
| 22 | + |
| 23 | +## Code Organization: |
| 24 | + |
| 25 | +- Use meaningful names: |
| 26 | + |
| 27 | + ```csharp |
| 28 | + // Good: Clear intent |
| 29 | + public async Task<Result<Order>> ProcessOrderAsync(OrderRequest request, CancellationToken cancellationToken) |
| 30 | + |
| 31 | + // Avoid: Unclear abbreviations |
| 32 | + public async Task<Result<T>> ProcAsync<T>(ReqDto r, CancellationToken ct) |
| 33 | + ``` |
| 34 | + |
| 35 | +- Separate state from behavior: |
| 36 | + |
| 37 | + ```csharp |
| 38 | + // Good: Behavior separate from state |
| 39 | + public sealed record Order(OrderId Id, List<OrderLine> Lines); |
| 40 | + |
| 41 | + public static class OrderOperations |
| 42 | + { |
| 43 | + public static decimal CalculateTotal(Order order) => |
| 44 | + order.Lines.Sum(line => line.Price * line.Quantity); |
| 45 | + } |
| 46 | + ``` |
| 47 | + |
| 48 | +- Prefer pure methods: |
| 49 | + |
| 50 | + ```csharp |
| 51 | + // Good: Pure function |
| 52 | + public static decimal CalculateTotalPrice( |
| 53 | + IEnumerable<OrderLine> lines, |
| 54 | + decimal taxRate) => |
| 55 | + lines.Sum(line => line.Price * line.Quantity) * (1 + taxRate); |
| 56 | + |
| 57 | + // Avoid: Method with side effects |
| 58 | + public void CalculateAndUpdateTotalPrice() |
| 59 | + { |
| 60 | + this.Total = this.Lines.Sum(l => l.Price * l.Quantity); |
| 61 | + this.UpdateDatabase(); |
| 62 | + } |
| 63 | + ``` |
| 64 | + |
| 65 | +- Use extension methods appropriately: |
| 66 | + |
| 67 | + ```csharp |
| 68 | + // Good: Extension method for domain-specific operations |
| 69 | + public static class OrderExtensions |
| 70 | + { |
| 71 | + public static bool CanBeFulfilled(this Order order, Inventory inventory) => |
| 72 | + order.Lines.All(line => inventory.HasStock(line.ProductId, line.Quantity)); |
| 73 | + } |
| 74 | + ``` |
| 75 | + |
| 76 | +- Design for testability: |
| 77 | + |
| 78 | + ```csharp |
| 79 | + // Good: Easy to test pure functions |
| 80 | + public static class PriceCalculator |
| 81 | + { |
| 82 | + public static decimal CalculateDiscount( |
| 83 | + decimal price, |
| 84 | + int quantity, |
| 85 | + CustomerTier tier) => |
| 86 | + // Pure calculation |
| 87 | + } |
| 88 | + |
| 89 | + // Avoid: Hard to test due to hidden dependencies |
| 90 | + public decimal CalculateDiscount() |
| 91 | + { |
| 92 | + var user = _userService.GetCurrentUser(); // Hidden dependency |
| 93 | + var settings = _configService.GetSettings(); // Hidden dependency |
| 94 | + // Calculation |
| 95 | + } |
| 96 | + ``` |
| 97 | + |
| 98 | +## Dependency Management: |
| 99 | + |
| 100 | +- Minimize constructor injection: |
| 101 | + |
| 102 | + ```csharp |
| 103 | + // Good: Minimal dependencies |
| 104 | + public sealed class OrderProcessor(IOrderRepository repository) |
| 105 | + { |
| 106 | + // Implementation |
| 107 | + } |
| 108 | + |
| 109 | + // Avoid: Too many dependencies |
| 110 | + // Too many dependencies indicates possible design issues |
| 111 | + public class OrderProcessor( |
| 112 | + IOrderRepository repository, |
| 113 | + ILogger logger, |
| 114 | + IEmailService emailService, |
| 115 | + IMetrics metrics, |
| 116 | + IValidator validator) |
| 117 | + { |
| 118 | + // Implementation |
| 119 | + } |
| 120 | + ``` |
| 121 | + |
| 122 | +- Prefer composition with interfaces: |
| 123 | + |
| 124 | + ```csharp |
| 125 | + // Good: Composition with interfaces |
| 126 | + public sealed class EnhancedLogger(ILogger baseLogger, IMetrics metrics) : ILogger |
| 127 | + { |
| 128 | + } |
| 129 | + ``` |
0 commit comments