nmbl Documentation
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage
Edit page

regions - Region Count

Overview

The regions command counts all #region directives in C# files within a project or directory and reports the total. Use it to measure the prevalence of region usage before deciding whether to run nmbl endregions to remove them.

Syntax

nmbl regions <ProjectFileOrDirectory>

Arguments

  • ProjectFileOrDirectory - Path to a .csproj file, a project directory, or . for the current directory.

Output

Prints a single line with the total count of #region directives found:

Total Regions Found: 14

When no C# files are found, prints:

No C# files found in the specified project or directory.
Total Regions Found: 0

Exit Codes

  • 0 - Success, including when no C# files or regions are found.
  • 1 - The required path argument is missing or an unexpected error occurs.

Examples

Count regions in a project

nmbl regions src/MyProject/MyProject.csproj

Count regions in a directory

nmbl regions src/

Recursively counts regions in all C# files under src/.

Check before removing

# See how many regions exist
nmbl regions src/

# If the count is acceptable, remove them
nmbl endregions src/ -y

Track region reduction over time

# Before cleanup
nmbl regions src/ > metrics/regions-before.txt

# After removing regions
nmbl regions src/ > metrics/regions-after.txt

diff metrics/regions-before.txt metrics/regions-after.txt

Why regions are problematic

Regions can indicate:

  • Files that are too large — if you need regions to organize a file, the file may be doing too much.
  • Multiple responsibilities — different regions often represent different concerns that belong in separate classes.
  • Hidden complexity — collapsed regions make it harder to see the full structure of the code.

Refactoring strategy

When you find files with many regions:

  1. Consider splitting the file — each region might become its own class.
  2. Use folders and namespaces — organize by namespace and folder structure instead.
  3. Apply design patterns — replace region-separated responsibilities with proper object-oriented design.

Before (with regions)

public class OrderService
{
    #region Validation
    // validation methods
    #endregion

    #region Processing
    // processing methods
    #endregion
}

After (without regions)

public class OrderValidator { }
public class OrderProcessor { }

public class OrderService
{
    private readonly OrderValidator _validator;
    private readonly OrderProcessor _processor;
}
  • endregions - Remove region directives
  • cc - Find complex methods
  • cogc - Find hard-to-understand code
  • loc - Find large files