Code Clean Up: Specification of a tool to clean up code
Specification of a potential tool to clean up code by identifying and removing unused code elements and commented-out code in C# and XAML projects. The tool would analyze the codebase to find private methods, fields, properties, and local variables that are never referenced or used, as well as multi-line comment blocks that contain code-like patterns. It would then provide a preview of the candidates for removal, allowing the user to select which items to remove, and would safely apply the changes while ensuring that the code still compiles and runs correctly. The tool would also include configuration options for file patterns, directories to exclude, and safe mode settings to prevent accidental removal of important code.
About
Been cleaning an app with the help of AI (WindSurf) to remove all of the leftover commented-out code and unused/unreferenced code elements. Overall, this tool would be a valuable addition to any developer’s toolkit for maintaining clean and efficient codebases. This is a proposal for a yet to be developed plugin tool.
Code Cleanup Tool Specification
Overview
This tool identifies and removes unused code elements and commented-out code from C# and XAML projects in a safe, automated manner.
Supported Languages
- C# (.cs files)
- XAML (.xaml files)
Analysis Actions
1. Unused Methods Detection
Target: C# private methods
Methodology:
- Parse all C# files to identify all method declarations
- For each method:
- Search the entire codebase for method name references
- Check if the method is assigned to delegates or event handlers
- Check if the method is referenced via reflection (string-based calls)
- Check if the method has special attributes (e.g.,
[DllImport],[Command])
- Flag methods with zero references as candidates for removal
Exclusions:
- Methods with special attributes that may be called via reflection
- Methods assigned to event handlers in XAML
- Methods with
[DllImport]for P/Invoke calls - Methods with
[Obsolete]attributes (warn instead)
2. Unused Fields Detection
Target: C# private fields
Methodology:
- Parse all C# files to identify all field declarations
- For each field:
- Search the entire codebase for field name references
- Check if the field is accessed via properties
- Check if the field is serialized (has
[Serializable],[DataMember], etc.) - Check if the field is used in data binding (WPF/MAUI)
- Flag fields with zero references as candidates for removal
Exclusions:
- Fields with serialization attributes
- Fields used in property setters that reference the backing field
- Fields with
[DllImport]or other special attributes
3. Unused Properties Detection
Target: C# private properties
Methodology:
- Parse all C# files to identify all property declarations
- For each property:
- Search the entire codebase for property name references
- Check if the property is used in data binding (XAML binding syntax)
- Check if the property is used in MVVM command binding
- Check if the property is referenced via reflection
- Flag properties with zero references as candidates for removal
Exclusions:
- Properties with
[ObservableProperty](CommunityToolkit.Mvvm) - Properties used in XAML data binding
- Properties with special attributes
These exclusions are important because, for example, Observable Properties are defined by unreferenced fields that upon compilation are turned into properties that are what are referenced in the code, not the fields. The tool needs to be intelligent on these matters.
4. Unused Variables Detection
Target: Local variables within methods
Methodology:
- Parse each method to identify local variable declarations
- For each variable:
- Check if the variable is read after being written
- Check if the variable is passed to other methods
- Check if the variable is used in expressions
- Flag variables that are written but never read as candidates for removal
Exclusions:
- Variables used only for their side effects (e.g.,
Taskresult ignored) - Variables used in
usingstatements - Variables in
outparameters
5. Commented-Out Code Detection
C# Files
Target: Multi-line comment blocks (/* */)
Methodology:
- Find all
/* ... */comment blocks - Analyze the content:
- Check for C# syntax patterns (keywords, braces, semicolons)
- Check for method/field declarations
- Check for control structures (if, for, while, switch)
- Flag comment blocks containing code-like patterns as candidates for removal
Exclusions:
- Single-line comments (
//) - these are typically documentation - Comment blocks containing only text descriptions
- Comment blocks with XML documentation tags (
<summary>,<param>, etc.)
XAML Files
Target: XAML comment blocks (<!-- -->)
Methodology:
- Find all
<!-- ... -->comment blocks - Analyze the content:
- Check for XAML element tags (
<,>) - Check for attribute assignments (
=) - Check for markup extensions (
{Binding ...})
- Check for XAML element tags (
- Flag comment blocks containing XAML markup as candidates for removal
Exclusions:
- Comment blocks containing only text descriptions
- Comment blocks with documentation-like content
Removal Actions
Preview Mode
Before any removal:
- Display a list of all candidates for removal
- Group by file and type (method, field, property, variable, comment)
- Show the code snippet that would be removed
- Allow user to select/deselect individual items
- Estimate impact (lines of code reduction)
Safe Removal Process
- Backup: Create a backup of the file before modification
- Suggest using version control (Git) to manage changes and allow easy reversion
- Atomic Changes: Apply changes one at a time
- Validation: After each change:
- Verify the file still compiles (for C#)
- Verify XAML syntax is valid (for XAML)
- If compilation fails, revert the change and warn the user
- Report: Generate a summary of changes made
Post-Removal Formatting
- Remove extra blank lines left by removed code
- Fix indentation issues
- Ensure proper brace alignment
- Preserve existing code style
Configuration
File Patterns
- Include:
*.cs,*.xaml - Exclude:
*.Designer.cs,*.g.cs,*.g.i.cs(generated files)
Directories
- Exclude:
bin/,obj/,node_modules/,.git/
Safe Mode Options
- Skip files with
[GeneratedCode]attribute - Skip test files (optional)
- Skip files with specific keywords in path (configurable)
Command Line Interface
# Analyze only (preview mode)
code-cleanup analyze --path ./src
# Remove unused code with preview
code-cleanup cleanup --path ./src --preview
# Remove commented-out code only
code-cleanup cleanup --path ./src --comments-only
# Dry run (show what would be removed)
code-cleanup cleanup --path ./src --dry-run
# Specific file types
code-cleanup cleanup --path ./src --include "*.cs" --exclude "*.Designer.cs"
VS Code Extension Commands
CodeCleanup: Analyze Current File- Show unused elements in current fileCodeCleanup: Analyze Workspace- Show unused elements in entire workspaceCodeCleanup: Cleanup Current File- Remove unused elements in current fileCodeCleanup: Cleanup Workspace- Remove unused elements in entire workspaceCodeCleanup: Remove Commented Code- Remove commented-out code blocks
Visual Studio Extension Commands
Tools > Code Cleanup > Analyze SolutionTools > Code Cleanup > Analyze ProjectTools > Code Cleanup > Cleanup SolutionTools > Code Cleanup > Cleanup ProjectTools > Code Cleanup > Remove Commented Code
Output Format
Console Output
Analyzing c:\Project\MainViewModel.cs...
Found 0 unused methods
Found 0 unused fields
Found 0 unused properties
Found 0 unused variables
Found 0 commented-out code blocks
Analyzing c:\Project\WindGaugeWindow.xaml.cs...
Found 2 unused methods:
- UpdateTimeInputFromLapTime (line 395)
- ComboBox_SelectionChanged (line 825)
Found 2 unused fields:
- _firstLapCounted (line 45)
- _skipNextTimerUpdate (line 52)
Total candidates for removal: 4 methods, 2 fields, 0 properties, 0 variables, 0 comments
Estimated lines of code reduction: 45
JSON Output (for CI/CD)
{
"summary": {
"filesAnalyzed": 15,
"unusedMethods": 2,
"unusedFields": 2,
"unusedProperties": 0,
"unusedVariables": 0,
"commentedCodeBlocks": 0,
"linesReduced": 45
},
"changes": [
{
"file": "WindGaugeWindow.xaml.cs",
"type": "method",
"name": "UpdateTimeInputFromLapTime",
"line": 395,
"lines": 30
}
]
}
Safety Considerations
- Never remove code automatically without user confirmation
- Always create backups before modification
- Validate compilation after each change
- Respect version control - warn if files have uncommitted changes
- Handle special cases:
- Reflection-based calls
- Serialization
- Data binding (WPF/MAUI)
- Event handlers in XAML
- Test frameworks (xUnit, NUnit, MSTest)
Conclusion
Looking at developing this tool at some stage. Also a call to a Lint tool here might be useful.
| Topic | Subtopic | |
| This Category Links | ||
| Category: | Artificial Intelligence Index: | Artificial Intelligence |
| < Prev: | Blazor App | From enum properties to SQL tabled properties Part 2 |