Dead Code

Hi Everyone, in this post I will be describing about dead code. I have heard about dead code only recently and thought of putting it in blog. 'Dead code' as it name suggests is a piece of code which is dead or has got no chance of it being executed. In other words dead code is a type of code which is unreachable.

I will provide an example which will helps in understanding more about dead code. Consider a piece of code in Java.

String fullName = employee.getFirstName() + " " + employee.getLastName();

if(fullName!=null)
{
fullName = "Sujith";
}

In the above example the code in the 'if' loop would never be executed. The reason is string 'fullName' cannot be empty because of the space (" ") we are adding between employee.getFirstName() and employee.getLastName().  It doesn't matter even if both employee.getFirstName() and employee.getLastName() be null, "fullName" would not be null.

Some IDE's like eclipse and visual studio have the ability to locate the dead code. Dead code elimination is a compiler optimization to remove code which does not affect program results.

Here are some wiki links about dead code and dead code elimination :-

1) Dead Code.

2) Dead Code Elimination.


Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++