Code Refactoring

Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a ‘refactoring’) does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it’s less likely to go wrong. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring. — Martin Fowler http://www.refactoring.com/

In other words, refactoring is making your code cleaner, safer, and easier to use. Understand can be a very powerful tool in your re-factoring arsenal. Here are a few ways you can use Understand in your refactoring process.

Quickly Identify References
In the long list of refactorings, many of the suggested refactorings involve replacing one data type with another. In many cases that can involve changing the calls to that data type as well. Understand can instantly show you everywhere that that entity is used, called, referenced, modified or typed. Just right click on the entity, select View Information, and expand the references section.

refactor_01

Metrics Summary
You can quickly find a summary of the metrics for your project in Metrics -> Metrics Summary.

refactor_02

Reformat your code
If you have ugly code, you can beautify it quickly with the the tip reported in this entry:

Tip – Beautify your code

With one button press, turn a file that looks like this:

refactor_03

into this:

refactor_04

Rename Entities
Sometimes renaming an entity can go a long way towards making the code readable, perhaps a missed name class or method.

Download rename for a user tool to quickly rename entities in Understand – it goes through the references and replaces each instance of the class or object with the new name.

Save the .pl script to your hard drive, and then in Understand under Tools | User Tools| Configure select Import to load the .ini file. Change the path to the location of the .pl file. Then to use it, right click on any entity in Understand and select User Tools | Replace. You’ll be prompted for the new text, and then all references to that entity will be renamed. You’ll want to reparse the changed files after each rename so that the database stays up to date (Ctrl+R by default).

refactor_05

Follow Dependencies
Understand’s new Dependency features can help you to quickly get a handle on what parts of the code are dependent on other parts of the code, letting you define what logical lines to divide your code into.
Dependency Browser
Dependency Graphs
Exporting Dependencies