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

Options

Output

Reports the number of region directives removed from each file.

Examples

Remove Regions from a Project (With Confirmation)

nmbl endregions src/MyProject/MyProject.csproj

Prompts for confirmation before removing regions.

Remove Regions Without Confirmation

nmbl endregions src/ -y

Immediately 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/ -y

Process Specific Files

nmbl endregions src/Services/ -y

Remove 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
fi

Why Remove Regions?

Regions can:

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

Organize by namespace and folder structure.

Safety Features

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

Commit 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)