Skip to main content

Refactoring and Code Organization

Level: Intermediate

ℹ️ What You'll Learn
  • Rename refactoring across project
  • Extract methods and classes
  • Organize using statements
  • Move code between files
  • Quick actions and suggestions

Rename Refactoring

Right-click identifier → Rename or Ctrl + R, R:

// Before
public class StudentServ // Cursor here, Rename
{
public List<Student> GetStud() { }
}

// After (automatic throughout project)
public class StudentService
{
public List<Student> GetStudents() { }
}

All references update automatically.

Extract Method

Select code → Ctrl + . → Extract Method:

// Before
public decimal CalculateTotalFees(int studentId)
{
var student = GetStudent(studentId);
var classRate = GetClassFeeRate(student.ClassName);
var discount = GetDiscount(student.Status);
return classRate - discount; // Select this
}

// After (extracted)
public decimal CalculateTotalFees(int studentId)
{
return CalculateFeesForStudent(studentId);
}

private decimal CalculateFeesForStudent(int studentId)
{
var student = GetStudent(studentId);
var classRate = GetClassFeeRate(student.ClassName);
var discount = GetDiscount(student.Status);
return classRate - discount;
}

Extract logical blocks into reusable methods.

Organize Imports

Ctrl + R, G — Remove unused using statements:

// Before
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Data.SqlClient; // Not used
using Newtonsoft.Json; // Not used

public class StudentService { }

// After (Organize Usings)
using System;
using System.Collections.Generic;
using System.Linq;

public class StudentService { }

Also alphabetizes remaining imports.

Quick Actions

Ctrl + . when cursor on identifier:

// Problem: Unused variable
var examResult = GetExamResult(studentId); // Gray highlight

// Quick Actions:
// - Remove unused variable
// - Convert to discard (_)
// - Add to unused field

Change Method Signature

Right-click method → Quick Actions → Change Signature:

// Original
public Student GetStudent(int id)

// Add parameter for logging
public Student GetStudent(int id, bool includeDetails = false)

// Refactoring updates all calls automatically
var student = GetStudent(101); // Still works
var student = GetStudent(101, true); // New calls

Move Code to New File

Right-click class → Move → Create new file:

// Before (all in StudentService.cs)
public class StudentService { }
public class FeeCalculator { }
public class AttendanceTracker { }

// After (Move to new files)
// StudentService.cs
public class StudentService { }

// FeeCalculator.cs
public class FeeCalculator { }

// AttendanceTracker.cs
public class AttendanceTracker { }

Organize by responsibility.

Convert Anonymous Type to Class

// Before
var student = new { Name = "Ravi", Class = "10-A" };

// Quick Action: Convert to class
public class StudentInfo
{
public string Name { get; set; }
public string Class { get; set; }
}

var student = new StudentInfo { Name = "Ravi", Class = "10-A" };

Key Takeaways

  • Rename = bulk update throughout project
  • Extract = break into smaller methods
  • Organize = clean imports
  • Quick actions = automated improvements
💡 Refactoring Pro Tip

Refactor small, frequently. Large refactors = higher risk of bugs.

🤖Use AI to Learn Faster

Use ChatGPT, Claude, or Copilot to go deeper on VS Refactoring. Try these prompts:

  • "How do I safely rename across a project?"
  • "When should I extract a method?"
  • "Quiz me on refactoring"

💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.

nexcoding.in