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

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.csproj

Shows 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 -rn

Sort 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.txt

Why Regions Are Problematic

Regions can indicate:

Refactoring Strategy

When you find files with many regions:

  1. Consider splitting the file - Each region might become its own class
  2. Use folders instead - Organize by namespace and folder structure
  3. 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;
}