|
| 1 | +<!-- List of authors who contributed to this decision. Include full names and roles if applicable. --> |
| 2 | +authors: |
| 3 | +- Martin Stühmer |
| 4 | + |
| 5 | +<!-- |
| 6 | +The patterns this decision applies to. Each entry is a glob pattern that matches files affected by this decision. |
| 7 | +--> |
| 8 | +applyTo: |
| 9 | +- "**/*.cs" |
| 10 | +- "**/*.razor" |
| 11 | +- "**/*.razor.cs" |
| 12 | + |
| 13 | +<!-- The date this ADR was initially created in YYYY-MM-DD format. --> |
| 14 | +created: 2026-01-21 |
| 15 | + |
| 16 | +<!-- |
| 17 | +The most recent date this ADR was updated in YYYY-MM-DD format. |
| 18 | +IMPORTANT: Update this field whenever the decision is modified. |
| 19 | +--> |
| 20 | +lastModified: 2026-01-21 |
| 21 | + |
| 22 | +<!-- |
| 23 | +The current state of this ADR. If superseded, include references to the superseding ADR. |
| 24 | +Valid values: proposed, accepted, deprecated, superseded |
| 25 | +--> |
| 26 | +state: accepted |
| 27 | + |
| 28 | +<!-- |
| 29 | +A compact AI LLM compatible definition of this decision. |
| 30 | +This should be a precise, structured description that AI systems can easily parse and understand. |
| 31 | +Include the core decision, key rationale, and primary impact in 1-2 concise sentences. |
| 32 | +--> |
| 33 | +instructions: | |
| 34 | + MUST NOT use code regions (`#region`/`#endregion`) under any circumstances. |
| 35 | + Regions obscure code structure, hinder navigation, and indicate poor code organization that should be resolved through proper refactoring. |
| 36 | +--- |
| 37 | +# No Code Regions |
| 38 | + |
| 39 | +A decision to prohibit the use of code regions (`#region`/`#endregion`) in all C# and Razor source files to improve code readability, maintainability, and navigation. |
| 40 | + |
| 41 | +## Context |
| 42 | + |
| 43 | +Code regions in C# allow developers to group sections of code that can be collapsed in the IDE using `#region` and `#endregion` directives: |
| 44 | + |
| 45 | +```csharp |
| 46 | +#region Private Methods |
| 47 | +private void DoSomething() { } |
| 48 | +private void DoSomethingElse() { } |
| 49 | +#endregion |
| 50 | +``` |
| 51 | + |
| 52 | +While regions were historically used to organize code, modern development practices and tooling have evolved significantly: |
| 53 | + |
| 54 | +1. **Code Smell Indicator**: Regions often indicate that a class or file has grown too large and should be refactored into smaller, more focused components. |
| 55 | +2. **Navigation Obstruction**: Collapsed regions hide code, making it harder to navigate and understand the full structure of a file. |
| 56 | +3. **False Organization**: Regions provide a false sense of organization without addressing the underlying structural issues. |
| 57 | +4. **Modern IDE Capabilities**: Current IDEs provide superior navigation tools (outline views, symbol search, code folding based on syntax) that make regions unnecessary. |
| 58 | +5. **Code Review Impact**: Regions can hide code during reviews, potentially allowing problematic code to go unnoticed. |
| 59 | +6. **Partial Class Alternative**: When code organization is genuinely needed, partial classes provide a better solution with proper file separation. |
| 60 | + |
| 61 | +## Decision |
| 62 | + |
| 63 | +We have decided to **prohibit the use of code regions** (`#region`/`#endregion`) in all C# and Razor source files under all circumstances. |
| 64 | + |
| 65 | +### Implementation Requirements |
| 66 | + |
| 67 | +* MUST NOT use `#region` or `#endregion` directives in any C# or Razor file. |
| 68 | +* MUST remove existing regions during code refactoring or maintenance. |
| 69 | +* MUST refactor large files into smaller, focused classes or partial classes instead of using regions. |
| 70 | +* MUST use proper class design and separation of concerns as organizational strategies. |
| 71 | + |
| 72 | +### Enforcement |
| 73 | + |
| 74 | +* Code reviewers MUST reject pull requests containing new region directives. |
| 75 | +* Static analysis tools SHOULD be configured to flag region usage as a warning or error. |
| 76 | +* Existing regions SHOULD be removed opportunistically during regular maintenance. |
| 77 | + |
| 78 | +### Acceptable Alternatives |
| 79 | + |
| 80 | +Instead of using regions, developers MUST apply these organizational strategies: |
| 81 | + |
| 82 | +| Problem | Solution | |
| 83 | +|---------|----------| |
| 84 | +| File is too long | Split into multiple classes or partial classes | |
| 85 | +| Grouping related methods | Use proper class design with single responsibility | |
| 86 | +| Separating interface implementations | Use partial classes in separate files | |
| 87 | +| Organizing generated code | Place generated code in separate partial class files | |
| 88 | + |
| 89 | +## Consequences |
| 90 | + |
| 91 | +### Positive Consequences |
| 92 | + |
| 93 | +* **Improved Readability**: All code is visible and accessible without expanding collapsed sections. |
| 94 | +* **Better Code Structure**: Forces developers to address underlying organizational issues through proper refactoring. |
| 95 | +* **Enhanced Navigation**: IDE navigation features work consistently without hidden code sections. |
| 96 | +* **Clearer Code Reviews**: Reviewers see all code without needing to expand regions. |
| 97 | +* **Consistent Codebase**: Uniform code organization approach across the entire project. |
| 98 | + |
| 99 | +### Negative Consequences |
| 100 | + |
| 101 | +* **Initial Refactoring Effort**: Existing code with regions requires refactoring to comply with this decision. |
| 102 | +* **Learning Curve**: Developers accustomed to using regions must adopt alternative organizational patterns. |
| 103 | +* **Potential Resistance**: Some developers may prefer regions for organizing large files. |
| 104 | + |
| 105 | +### Trade-offs |
| 106 | + |
| 107 | +* Short-term effort to remove existing regions versus long-term maintainability benefits. |
| 108 | +* Requires discipline to maintain small, focused classes instead of relying on regions for organization. |
| 109 | + |
| 110 | +## Alternatives Considered |
| 111 | + |
| 112 | +### Allow Regions for Specific Use Cases |
| 113 | + |
| 114 | +**Rejected** because: |
| 115 | +- Creates ambiguity about when regions are acceptable |
| 116 | +- Leads to inconsistent codebase |
| 117 | +- Does not address the fundamental issues regions introduce |
| 118 | + |
| 119 | +### Use EditorConfig to Disable Region Folding |
| 120 | + |
| 121 | +**Rejected** because: |
| 122 | +- Only affects IDE display, does not prevent region creation |
| 123 | +- Does not enforce the underlying code quality principles |
| 124 | +- Regions still clutter source files |
| 125 | + |
| 126 | +### Allow Regions Only in Generated Code |
| 127 | + |
| 128 | +**Rejected** because: |
| 129 | +- Generated code should be in separate files or partial classes |
| 130 | +- Maintaining exceptions complicates the rule |
| 131 | +- Modern code generation tools support partial class patterns |
| 132 | + |
| 133 | +## Related Decisions (Optional) |
| 134 | + |
| 135 | +None at this time. |
0 commit comments