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.

image

Identify Complex Functions

Many overly-complex functions can be refactored into smaller, more maintainable units. Knowing the McCabe Cyclomatic complexity can help you quickly identify functions and methods that are overly complicated and may be good candidates for refactoring. You can quickly find the complexity of every function in an Understand project by selecting Metrics –> Export Metrics. If you’re just looking for a quick list of complex functions, this plugin will show you the most complex functions in your project.

Download it from our Plugin page and you’ll see a list of the most complex functions in your project.

image

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:

image

into this:

image

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).

image

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
image