Home > Code Analysis > Code Issues of the dead code type for members and blocks

Code Issues of the dead code type for members and blocks

October 18th, 2012

Here are several of the code issues, which highlight redundant and unnecessary code blocks that may be safely removed for improving code readability.

Empty event handler

Cause:

An event is the mechanism for a class that notifies its clients when something happens. Events provide add additional methods (event handlers) to be triggered upon an external event. As an empty event handler does nothing, there is no need for it. An empty handler can be safely removed, which will improve code readability.

Sample:

CodeRush Empty Event Handler Preview

How to fix:

  • Apply the Remove Empty Handler code provider:

CodeRush Remove Empty Handler Fix

Empty finally block

Cause:

A code inside a finally block is executed when a control leaves a try statement and is guaranteed to be executed no matter what happens, including exceptions and return statements. An empty finally block does not make sense, because nothing will happen when it receives a control. Thus, an empty finally block is redundant and can be safely removed, which will improve code readability.

Sample:

CodeRush Empty Event Handler Preview

How to fix:

  • Remove an empty finally block (including a try block if there is no catch block):

CodeRush Empty Finally Block Fix

Redundant private setter

Cause:

You cannot modify the value of a backing field inside the private setter outside of the current class. Also, if there are no assignments to the property in the current class, it doesn’t make sense to declare a private setter at all. In this case, you can safely remove the private setter to improve code readability.

Sample:

CodeRush Redundant Private Setter Sample

How to fix:

  • Apply the Remove Private Setter refactoring:

CodeRush Remove Private Setter Fix Preview

Unused member

Cause:

A member which is not referenced anywhere (unused) in the code is useless and can be safely removed to clean up the code and improve readability.

Sample:

CodeRush Unused Member Preview

How to fix:

  • Remove the unused member.

Unused declaration

Cause:

Similar to the Unused Member code issue, this code issue highlights variables that are not used in the code. Removing such variables will improve code readability.

Sample:

CodeRush Unused Declaration Preview

How to fix:

  • Remove the variable its assignments, if any.
—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Oct/18/2012
ID: C200

Similar Posts: