regions - Region Count
Overview
The regions command counts #region directives in C# files. Regions are often considered a code smell as they can hide complexity and make code harder to navigate. This command helps you find files with regions so you can consider refactoring them.
Syntax
nmbl regions <ProjectFileOrDirectory>Arguments
ProjectFileOrDirectory- Path to a .csproj file or directory containing C# files
Output
Displays files containing #region directives and their counts, helping you identify files that may benefit from refactoring.
Examples
Count Regions in a Project
nmbl regions src/MyProject/MyProject.csprojShows all files in the project that contain regions.
Count Regions in a Directory
nmbl regions src/Recursively finds and counts regions in all C# files in the directory tree.
Find Files with Many Regions
nmbl regions src/ | sort -rnSort the output to see files with the most regions first (Unix/Linux).
Track Region Reduction
# Before cleanup
nmbl regions src/ > metrics/regions-before.txt
# After removing regions
nmbl regions src/ > metrics/regions-after.txt
# Compare the results
diff metrics/regions-before.txt metrics/regions-after.txtWhy Regions Are Problematic
Regions can indicate:
- Files that are too large - If you need regions to organize, the file might be too big
- Multiple responsibilities - Different regions often mean different concerns
- Hidden complexity - Collapsed regions hide code that should be visible
Refactoring Strategy
When you find files with many regions:
- Consider splitting the file - Each region might become its own class
- Use folders instead - Organize by namespace and folder structure
- Apply design patterns - Replace regions with proper object-oriented design
Examples of Refactoring
Before (with regions)
public class OrderService
{
#region Validation
// validation methods
#endregion
#region Processing
// processing methods
#endregion
#region Notification
// notification methods
#endregion
}After (without regions)
// Split into three classes
public class OrderValidator { }
public class OrderProcessor { }
public class OrderNotifier { }
public class OrderService
{
private readonly OrderValidator _validator;
private readonly OrderProcessor _processor;
private readonly OrderNotifier _notifier;
}Related Commands
endregions- Remove region directivescc- Find complex methodscogc- Find hard-to-understand codeloc- Find large files