endregions - Remove Region Directives
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.
nmbl endregions <ProjectFileOrDirectory> [options]
ProjectFileOrDirectory- Path to a .csproj file or directory containing C# files
-y- Skip confirmation prompt and remove regions immediately
Reports the number of region directives removed from each file.
nmbl endregions src/MyProject/MyProject.csproj
Prompts for confirmation before removing regions.
nmbl endregions src/ -y
Immediately removes all region directives in the directory tree.
# First, see what regions exist
nmbl regions src/
# Then remove them
nmbl endregions src/ -y
nmbl endregions src/Services/ -y
Remove regions only from a specific subdirectory.
# 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.
# Clean up before submitting a PR
nmbl endregions src/MyFeature/ -y
git add .
git commit -m "Remove region directives"
# Part of a larger refactoring effort
nmbl endregions src/Legacy/ -y
# Follow up with other improvements
nmbl cc src/Legacy/
nmbl cogc src/Legacy/
# 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
fi
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
Instead of regions, use:
// Instead of one file with regions:
public class OrderService
{
#region Validation
// ...
#endregion
#region Processing
// ...
#endregion
}
// Use separate classes:
public class OrderValidator { }
public class OrderProcessor { }
// 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).
Services/
Orders/
OrderValidator.cs
OrderProcessor.cs
OrderNotifier.cs
Organize by namespace and folder structure.
- 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
# 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.txt
# 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"
# 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)