What is Refactoring?

Refactorings are changes to a program that improve its internal design but do not change its behavior. These include minor, coding style changes (like using IMPLICIT NONE statements), code readability improvements (like replacing a variable named N with one called NUM_POINTS), performance improvements (like interchanging loops under certain conditions), and even larger-scale design changes (like moving a procedure from one module to another). Although these types of changes can be done by hand, making them is often tedious and error-prone. Photran automates many such refactorings. For example, the Refactor > Rename command can automatically locate the declaration(s) and uses of a particular subroutine, and change its name in all of those locations. It is "smart," too; if there is a subroutine named d and a variable named d in a different context, it won't confuse the two. Moreover, before making such a change, Photran will attempt to verify that the change is safe to make. For example, a subroutine A cannot be renamed to B if there is already a variable named B in a context where that subroutine is called. For more information on refactoring, see M. Fowler, Refactoring: Improving the Design of Existing Code, Addison-Wesley, 1999.

Refactoring in Photran

Most refactorings can be accessed via the Refactor menu in the menu bar, as described below. However, the most common refactorings also have hotkeys (e.g., Alt+Shift+R for Rename; hotkeys are listed in the Refactoring menu next to each command). Also, most refactorings can be accessed by right-clicking in an editor and choosing Refactor from the popup menu. Some refactorings (such as Introduce Implicit None and Replace Obsolete Operators) can be applied to several files at once. As described below, this involves selecting one or more files in the Fortran Projects view, then right-clicking on any of the selected filenames and choosing Refactor from the popup menu.

Caution: Photran can only refactor free-format Fortran source code. It is not possible to refactor fixed-form code. Make sure that only free-form Fortran files are selected. The Refactor menu may not be available if any of the files are fixed-form or non-Fortran files.


1. Dummy subprogram arguments cannot be renamed
2. Components of derived types cannot be renamed
3. Intrinsic subprograms and type-bound procedures (Fortran 2003) cannot be renamed

Extract Procedure

Extract Local Variable

Subprogram Refactorings

Add Subroutine Parameter

Permute Subprogram Arguments

Safe Delete

Module Refactorings

Make Private Entity Public

Example 2 of make private entity public refactoring.

Encapsulate Variable

1. If a variable is used as a parameter to a function/subroutine call, and that function changes the value of the variable as a side-effect, that change will not be preserved.

Use Statement Refactorings

Add ONLY Clause to USE Statement

Minimize ONLY List

Common Block Refactorings

Make Common Block Variable Names Consistent

Move Saved Variables to Common Block

Loop Refactorings

Interchange Loops

1. In order for refactoring to work correctly, there must be no statements before the second loop. If such statements exist, correctness of the refactoring is not guaranteed.

Fuse Loops

1. The refactoring will find the next listed do-loop in your code (even with other lines of code in between them).

Reverse Loop

Tile Loop

Unroll Loop

Refactorings to Remove Obsolete Language Features

Remove Arithmetic If

Remove Assigned Goto

Remove Branch to End If

Replace Character* with Character(len=)

Remove Computed Goto

Replace Obsolete Operators

Replace Old-Style Do Loops

Remove Pause Statement

Remove Real/Double Precision Loop Counter

Refactorings to Improve Coding Style

Add Identifier to End Statement

Change Keyword Case

Convert Between If Statement and If Construct

Convert Data Statement to Parameter Statements

Introduce Implicit None

Make Save Attributes Explicit

Remove Unreferenced Labels

Remove Unused Local Variables

Standardize Statements