Refactoring tools

Refactoring is a technique to restructure code in a disciplined way. This article suggests code analysis and visualization tools to help in the refactoring process.

Refactoring means modifying existing source code in a structured and incremental way. In refactoring, the behavior of the code is not radically changed. Refactoring promotes reuse as the refactored code is potentially easier to use in another context or for another purpose.

Refactoring is a generic programming technique that is not tied to a specific implementation language. We refer to the Refactoring web sitePopup link of Martin Fowler for the various refactoring techniques.

Use of Project Analyzer to refactor VB code

Project Analyzer is a tool that helps refactor existing Visual Basic code. It is useful in two ways.

Firstly, Project Analyzer helps to identify code that would potentially benefit from the refactoring techniques.

  • Hide Method. Project Analyzer detects excess method scope and suggests making a method Private where possible.
  • Hiding is also possible for variables that are not required outside of their class/module. It may also possible to make a variable local to a procedure.
  • Replace Nested Conditional with Guard Clauses. Project Analyzer can detect excessive conditional nesting, which is detrimental to understanding how the execution proceeds. It is often possible to replace nesting with guard clauses, that is, a sequence of non-nested If statements, which are easier to read.

See also the full set of code review rulesPopup link supported by Project Analyzer.

Secondly, Project Analyzer can also automatically perform a part of the refactoring. The auto-fix feature of Project Analyzer Enterprise Edition is able to automatically modify source code according to predefined rules. At this time, Project Analyzer supports automatic encapsulation of class variables as property Get/Set/Let.

In most cases, refactoring is best done manually, because code changes need human consideration. Automated changes are meant for routine tasks only.

Visustin - Refactoring with flow charts

Another tool that is beneficial in the refactoring process is Visustin. This utility displays a flow chart for a given piece of executable code, be it in Visual Basic, C/C++, C#, Java, Pascal, COBOL or a number of other major programming languages.

Logic errors easily appear in code that contains nested conditionals, loops and jumps. A flow chart displays program logic in a more comprehensible manner, not tied to the order that the underlying code is written in. By reading source code and flow charts side by side you can find logic flaws that could be left undetected by looking at the code only.

Besides error detection, a flow chart helps in finding ways to refactor code in a manner that is potentially more readable or more optimized. Visual interpretation of code can reveal patterns that are not easy to see in the source.

  • A long procedure may contain the same line of code in several locations. It is probable that you can change the logic to eliminate the duplicate lines.
  • A logical structure may be duplicated in two or more procedures (possibly via copy & paste coding). When detected, this logic is best moved to a new function and called from the other functions.
  • A complex algorithm is usually best to split in several functions. A flow chart is useful for finding the blocks from which to form new functions.
  • Decompose Conditional is a refactoring technique where a complex conditional expression is removed (and rewritten as a function). Complex expressions are easy to detect in a flow chart. It may also be possible to rewrite the logic in a simpler way.
  • Reverse Conditional means reversing the logic of a conditional statement by the use of the Not operator. In many cases, removing the Not is the most readable way. Flow charts often reveal the use of reverse logic which is rather straightforward to refactor.
©Aivosto Oy -