Remove dead code is another simple refactoring. Dead code is simply code that will never be executed—it might be commented out, it might be in a conditional block that will never be reached, etc. Dead code only confuses other programmers who then need to figure out why there is dead code in the first place. Dead code adds zero value and only makes the code less readable.
I often see commented out code, and it drives me crazy. Any good programmer is using source control, and the whole point of source control is to keep a history of all the changes to a file. Delete code instead of commenting it out! You will not lose anything because your source control has the code in its history…that’s its job.
Commented Out Code
Dead code can show up in a few ways, but the easiest dead code to spot is code that is commented out.
Models.UserAssessment userAssessment = new UserAssessment()
{
AssessmentAssignmentId = assessment.Id,
//AssessmentAssignmentUniqueId = nextAssessment. //Currently Not Available and Assigned
CurrentPageCount = pageNumber,
TotalPageCount = assessment.MaxPageNumber,
CurrentPage = userAssessmentPage,
//HasNextAssessment = hasNextAssessment,
//HasPreviousAssessment = hasPreviousAssessment,
PreviousAssessmentAssignment = GetPreviousAssessmentAssignment(assessments, assessment.SequenceNumber),
NextAssessmentAssignment = GetNextAssessmentAssignment(assessments, assessment.SequenceNumber)
};
All the commented out code just forces me to look at code that has zero value. It also makes me wonder why it is present at all—did the programmer do something else? Is the logic wrong? Were those fields removed from the UserAssessment
class? It’s just a bunch of noise that ultimately makes the code less readable. This is dead code and it should be removed.
Models.UserAssessment userAssessment = new UserAssessment()
{
AssessmentAssignmentId = assessment.Id,
CurrentPageCount = pageNumber,
TotalPageCount = assessment.MaxPageNumber,
CurrentPage = userAssessmentPage,
PreviousAssessmentAssignment = GetPreviousAssessmentAssignment(assessments, assessment.SequenceNumber),
NextAssessmentAssignment = GetNextAssessmentAssignment(assessments, assessment.SequenceNumber)
};
That is simpler, and all the code is actually doing something.
Unreachable Code
There are a few ways code can be unreachable—it could be in an if
statement that is always false
, it could be in an else
branch that will never be executed, it could be a loop that will never execute, etc. Unreachable code can be trickier to spot, but Visual Studio does have some tools that can help you spot it. Unreachable code is dead code and it should be removed for all the reasons we mentioned.
Let’s look at an example where there is unreachable code in an if
statement.
int age;
Console.WriteLine("What is your age? ");
age = Convert.ToInt32(Console.ReadLine());
age = age < 0 ? 0 : age;
if (age < 0) {
Console.WriteLine("Illegal age value entered.");
} else {
if (age < 18) {
Console.WriteLine("You are too young to vote.");
} else {
Console.WriteLine("You are old enough to vote.");
}
}
Line 4 will force age
to be 0 if it is less than 0, otherwise it allows whatever value it had. So after line 4 we know that age
is 0 or greater. This means that line 6 and its block is unreachable, so it can be removed.
int age;
Console.WriteLine("What is your age? ");
age = Convert.ToInt32(Console.ReadLine());
age = age < 0 ? 0 : age;
if (age < 18) {
Console.WriteLine("You are too young to vote.");
} else {
Console.WriteLine("You are old enough to vote.");
}
Sometimes you see programmers effectively comment out blocks of code by wrapping them in an if
statement that will never execute. Let’s say that in the example above we didn’t want to print out the messages about voting. We could disable that code by doing something like:
int age;
Console.WriteLine("What is your age? ");
age = Convert.ToInt32(Console.ReadLine());
age = age < 0 ? 0 : age;
if (false) {
if (age < 18) {
Console.WriteLine("You are too young to vote.");
} else {
Console.WriteLine("You are old enough to vote.");
}
}
Lines 6 – 12 would all be dead code in this scenario and should be removed.
References
- Fowler, M. et al. Refactoring: Improving the Design of Existing Code. Pearson Education, 2012. Web. Addison-Wesley Object Technology Series.