endregions - Remove Region Directives
Overview
The endregions command removes #region and #endregion directives from C# files. Regions are often considered code smells that hide complexity. This command helps you clean up your codebase by removing these directives, encouraging better code organization through proper class and file structure instead.
Syntax
nmbl endregions <ProjectFileOrDirectory> [options]Arguments
ProjectFileOrDirectory- Path to a .csproj file or directory containing C# files
Options
-y- Skip confirmation prompt and remove regions immediately
Output
Reports the number of region directives removed from each file.
Examples
Remove Regions from a Project (With Confirmation)
nmbl endregions src/MyProject/MyProject.csprojPrompts for confirmation before removing regions.
Remove Regions Without Confirmation
nmbl endregions src/ -yImmediately removes all region directives in the directory tree.
Check Before Removing
# First, see what regions exist
nmbl regions src/
# Then remove them
nmbl endregions src/ -yProcess Specific Files
nmbl endregions src/Services/ -yRemove regions only from a specific subdirectory.
Use Cases
Code Quality Improvement
# Find files with regions
nmbl regions src/
# Remove the regions
nmbl endregions src/ -y
# Refactor the code to use proper class structure
# (This step is manual)Regions often indicate that a file is doing too much. After removing them, consider splitting large files.
Preparing for Code Review
# Clean up before submitting a PR
nmbl endregions src/MyFeature/ -y
git add .
git commit -m "Remove region directives"Codebase Modernization
# Part of a larger refactoring effort
nmbl endregions src/Legacy/ -y
# Follow up with other improvements
nmbl cc src/Legacy/
nmbl cogc src/Legacy/CI/CD Quality Gates
# In a build script - fail if regions are found
if nmbl regions src/ | grep -q "#region"; then
echo "Error: Region directives found. Please remove them."
echo "Run: nmbl endregions src/ -y"
exit 1
fiWhy Remove Regions?
Regions can:
- Hide complexity - Collapsed regions make it harder to see code structure
- Indicate poor organization - Need for regions often means file is too large
- Hurt navigation - IDE features work better without regions
- Mask code smells - Problems are easier to ignore when hidden
Better Alternatives
Instead of regions, use:
Separate Classes
// Instead of one file with regions:
public class OrderService
{
#region Validation
// ...
#endregion
#region Processing
// ...
#endregion
}
// Use separate classes:
public class OrderValidator { }
public class OrderProcessor { }Partial Classes (Sparingly)
// OrderService.cs
public partial class OrderService
{
// Core functionality
}
// OrderService.Validation.cs
public partial class OrderService
{
// Validation methods
}Use partial classes only when there’s a good reason (e.g., generated code).
Folders and Namespaces
Services/
Orders/
OrderValidator.cs
OrderProcessor.cs
OrderNotifier.csOrganize by namespace and folder structure.
Safety Features
- Confirmation prompt - By default, asks before making changes
- File-level reporting - Shows exactly what was changed
- Preserves code - Only removes region directives, not the code inside
Tips
Preview Changes First
# See what regions exist
nmbl regions src/ > regions-before.txt
# Remove them
nmbl endregions src/ -y
# Verify they're gone
nmbl regions src/ > regions-after.txt
diff regions-before.txt regions-after.txtCommit Separately
# Remove regions in a separate commit
nmbl endregions src/ -y
git add .
git commit -m "Remove region directives"
# Then refactor in another commit
# (manual refactoring)
git add .
git commit -m "Refactor large classes"Team Adoption
# Add to coding standards
echo "No regions allowed" >> CONTRIBUTING.md
# Add pre-commit hook to prevent new regions
# (Configure git hook to run nmbl regions and fail if any found)